Numpy Array11
Numpy Array11
Numpy Array11
syllabus
2021-22
Chapter 11
Numpy
Array
Informatics Practices
Class XI ( As per CBSE Board)
Visit : python.mykvs.in for regular updates
NUMPY - ARRAY
NumPy stands for Numerical Python.It is the core library for scientific computing
in Python. It consist of multidimensional array objects, and tools for working with
these arrays.
Numpy Array is a grid of values with same type, and is indexed by a tuple of
nonnegative integers. The number of dimensions of it ,is the rank of the array; the
shape of an array depends upon a tuple of integers giving the size of the array along
each dimension.
Note:- Befor numpy based programming ,it must be installed. It can be installed using >pip
install numpy command at command prompt
• Vectorized operations
• Boolean selection
• Sliceability
1 D ARRAY
Any arrays can be single or multidimensional. The number of subscript/index determines
dimensions of the array. An array of one dimension is known as a one-dimensional array or
1-D array
In above diagram num is an array ,it’s first element is at 0 index position ,next element is at
1 and so on till last element at n-1 index position.At 0 index position value is 2 and at 1
index position value is 5.
1 D ARRAY
Creation of 1D array
One dimension array can be created using array method with list object with
one dimensional elements.
e.g.program
import numpy as np
a = np.array([500, 200, 300]) # Create a 1D Array
print(type(a)) # Prints "<class 'numpy.ndarray'>"
print(a.shape) # Prints "(3,)" means dimension of array
print(a[0], a[1], a[2]) # Prints "500 200 300"
a[0] = 150 # Change an element of the array
print(a)
1 D ARRAY
Create 1D from string
import numpy as np
data =np.fromstring('1 2', dtype=int, sep=' ')
print(data)
1 D ARRAY
Create 1D from array
Copy function is used to create the copy of the existing array.
e.g.program
import numpy as np
x = np.array([1, 2, 3])
y=x
z = np.copy(x)
x[0] = 10
print(x)
print(y)
print(z)
1 D ARRAY SLICES
Slicing of numpy array elements is just similar to slicing of list
elements.
e.g.program
import numpy as np
data = np.array([5,2,7,3,9])
print (data[:]) #print [5 2 7 3 9]
print(data[1:3]) #print [2 7]
print(data[:2]) #print [5 2]
print(data[-2:]) #print [3 9]
1 D ARRAY JOINING
Joining of two or more one dimensional array is possible
with the help of concatenate() function of numpy object.
e.g.program
import numpy as np
a = np.array([1, 2, 3])
b = np.array([5, 6])
c=np.concatenate([a,b,a])
print(c) #print [1 2 3 5 6 1 2 3]
2 D ARRAY
Creation of 2D array
Two dimension array can be created using array method with list object with two
dimensional elements.
e.g.program
import numpy as np
a = np.array([[3, 2, 1],[1, 2, 3]]) # Create a 2D Array
print(type(a)) # Prints "<class 'numpy.ndarray'>"
print(a.shape) # Prints (2, 3)
print(a[0][1]) # Prints 2
a[0][1] = 150 # Change an element of the array
print(a) # prints [[ 3 150 1] [ 1 2 3]]
2D ARRAY
Creation of 2D array from 1D array
We can create 2D array from 1d array using reshape() function.
e.g. program
import numpy as np
A = np.array([1,2,3,4,5,6])
B = np.reshape(A, (2, 3))
print(B)
OUTPUT
[[1 2 3]
[4 5 6]]
2 D ARRAY SLICES
Slicing of numpy 2d array elements is just similar to slicing of
list elements with 2 dimension.
e.g.program
import numpy as np
A = np.array([[7, 5, 9, 4],
[ 7, 6, 8, 8],
[ 1, 6, 7, 7]])
print(A[:2, :3]) #print elements of 0,1 rows and 0,1,2 columns
print(A[:3, ::2]) #print elements of 0,1,2 rows and alternate column position
print(A[::-1, ::-1]) #print elements in reverse order
print(A[:, 0]) #print all elements of 0 column
print(A[0, :]) #print all elements of 0 rows
print(A[0]) #print all elements of 0 row
import numpy as np
A = np.array([[7, 5],
[1, 6]]) OUTPUT
# concatenate along the first axis [[7 5]
print(np.concatenate([A, A])) [1 6]
# concatenate along the second axis [7 5]
(zero-indexed) [1 6]]
[[7 5 7 5]
print(np.concatenate([A, A], axis=1)) [1 6 1 6]]
x = np.array([1, 2])
# vertically stack the arrays [[1 2]
print(np.vstack([x, A])) [7 5]
# horizontally stack the arrays [1 6]]
y = np.array([[99],
[99]]) [[ 7 5 99]
print(np.hstack([A, y])) [ 1 6 99]]
Visit : python.mykvs.in for regular updates
NUMPY - ARRAY
2 D ARRAY – ARITHMATIC OPERATION
Arithmetic operation over 2d array is possible with add,substract,multiply,divide ()
functions.
E.G.PROGRAM
import numpy as np
a = np.array([[7, 5, 9],
OUTPUT
[ 2, 6, 8]])
[[7 5 9]
print(a) [2 6 8]]
b = np.array([10,10,10])
c=np.add(a,b) # c=a+b, similar [[17 15 19]
print(c) [12 16 18]]
c=np.subtract(a,b) # c=a-b, similar
print(c) [[-3 -5 -1]
c=np.multiply(a,b) # c=a*b, similar [-8 -4 -2]]
print(c)
[[70 50 90]
c=np.divide(a,b) # c=a/b, similar [20 60 80]]
print(c) [[0.7 0.5 0.9]
Note:- [0.2 0.6 0.8]]
1. if both 2d arrays are with same dimension[matrix form] then one to one arithmetic
operation will be performed.
2. No of elements of a dimension must match otherwise error message thrown
Visit : python.mykvs.in for regular updates
NUMPY - ARRAY
2 D ARRAY – ARITHMATIC OPERATION
Arithmetic operation over 2d array can be done with single value also.
E.G.PROGRAM
import numpy as np
a = np.array([[7, 5, 9],
[ 2, 6, 8]]) OUTPUT
print(a) [[7 5 9]
c=np.add(a,2) [2 6 8]]
print(c)
c=np.subtract(a,2) [[ 9 7 11]
[ 4 8 10]]
print(c)
[[5 3 7]
c=np.multiply(a,2) [0 4 6]]
print(c)
[[14 10 18]
c=np.divide(a,2) [ 4 12 16]]
print(c)
[[3.5 2.5 4.5]
[1. 3. 4. ]]
E.G.PROGRAM
import numpy as np
a = np.array([[7.333, 5.223],
[ 2.572, 6.119]]) OUTPUT
print(np.power(a,2)) [[53.772889 27.279729]
[ 6.615184 37.442161]]