Numpy
Numpy
For working with numpy, we should first import numpy module into our
Python program.
import numpy or
1. 1D Array
2. 2D Array
3. N-Dimension Array
1D ARRAY
1D Array- One dimensional array contains elements only in one dimension.
In other words, the shape of the numpy array should contain only
one value in the tuple.
.
5 6 9 4 3 1
Example 1
import numpy
a = numpy.array([10,20,30,40,50])
print(a)
Output: [10,20,30,40,50]
import numpy as np
a = np.array([10,20,30,40,50])
print(a)
a = array([10, 20,30,40,50])
print(a)
Using this function we can create array of any data type, but if not
data types is mentioned the default data type will be the "int"
For e.g :
from numpy import *
Arr=array([10,20,30,40,50],int) is similar to
arr = array([10,20,30,40,50])
While creating array if one of the values in the specified list belongs
to float then all the values will be converted to float by default.
a = array([10,30,40.5, 50,100])
print(a)
Output : = [10.0,30.0,40.5,50.0,100.0]
2. linspace() Function
print(a)
Output :[ 1. 2. 3. 4. 5. 6. 7. 8. 9. 10.]
3. arange() Function
Syntax-
arange(start,stop,stepsize)
Example
import numpy as np
a = np.arange(10)
b = np.arange(5,10)
c = np.arange(10,1,-1)
print(a)
print(b)
print(c)
Output-
[0,1,2,3,4,5,6,7,8,9]
[5,6,7,8,9]
[10, 9, 8, 7, 6, 5, 4, 3, 2]
4. Creating array using ones() and zeros() functions
We can use zeros() function to create an array with all zeros. The
ones() function will is useful to create an array with all 1s. They are
written in the following format-
zeros(n,datatype
ones(n,datatype)
Note : if datatype is missing then the default value will be float.
Example 1
Example
import numpy as np
K = np.zeros(5)
R = np.ones(5)
print(K)
print(R)
Output :
[0.,0.,0.,0.,0.]
[1.,1.,1.,1.,1.]
Mathematical Operations on Arrays
Example
import numpy as np
K = np.array([10, 20, 30, 40,50])
K = K+5 Add 5 to the Array
print(k)
K = K-5 Subtract 5 from each value of Array
print(k)
K = K*5 Multiply array by 5
print(k)
K = K/5 divide Array by 5
print(k)
Output-
[15 25 35 45 55]
[10 20 30 40 50]
[ 50 100 150 200 250]
[10. 20. 30. 40. 50.]
Aliasing the Arrays
Aliasing in arrays does not make any new copy of the array defined
earlier. It means new array created only reference to the array k.
import numpy as np
print(k)
print(k)
k[0] = 45
print(h)
print(k)
Output-
[3 5 6 7 8]
[3 5 6 7 8]
[3 5 6 7 8]
[45 5 6 7 8]
[45 5 6 7 8]
copy() method
The copy() method is used to copy the contents of one array to
another. The following function demonstrates the use of the copy
method.
import numpy as np
k = np.array([3,5,6,7,8])
print(k)
print(h)
print(k)
k[0] = 45
print(h)
print(k)
Output-
[3 5 6 7 8]
[3 5 6 7 8]
[3 5 6 7 8]
[3 5 6 7 8]
[45 5 6 7 8]
2-Dimensional Arrays in Numpy
2D Array-The dimension of an array represents the arrangement of
elements in the array. If the elements are arranged horizontally, it is
called the row and if the elements are arranged vertically, then it is
called the column. When they contain only one row and one column of
elements, it is called the Single dimensional array or one dimensional
array. When an array contains more than one row and more than one
column of elements, it is called the two dimensional array or 2-D array.
The following example is used demonstrate how to declare the two
dimensional array in using numpy.
Indexes in 2d Array
2D Array structure
Let us create a 2D array in numpy-
import numpy as np
Print(x)
Output-
[ [2 4 6]
[6 8 10] ]
ndim Attribute
ndim attribute is used to represent the number of dimensions of axes
of the array. The number of dimensions is also known as 'rank'. The
following example demonstrate the use of the ndim attribute
import numpy as np
A = np.array([5,6,7,8])
R = np.array([[4,5,6],[7,8,9]])
Answer :
2
shape attribute
The 'shape' attribute gives the shape of an array. The shape is tuple
listing the number of elements along each dimension. A dimension is
called an axis. For one dimensional array it will display a single value and
for two-dimensional array it will display two values separated by
commas represent rows and columns.
For Example
import numpy as np
k = np.array([1,2,3,4,5])
d = np.array([[5,6,7],[7,8,9]])
(5,)
(2, 3)
size Attribute
The size attributes gives the total number of elements in the array.
For e.g.
import numpy as np
a1 = np.array([1,2,3,4,5])
import numpy as np
k = np.array([[5,6,7],[7,8,9]])
import numpy as np
a1 = np.array([1, 2,3,4,5])
Output-
reshape() Method
The reshape() method is useful to change the shape of an array. The
new array should have the same number of elements as in the original
array. For e.g.
import numpy as np
d =np.array([[4,5,6,7],[5,6,7,8],[7,8,9,6]])
print(d)
print(d)
Output-
[[4 5 6 7]
[5 6 7 8]
[7 8 9 6]]
[[4 5]
[6 7]
[5 6]
[7 8]
[7 8]
[9 6]]
[[4 5 6 7 5 6 7 8 7 8 9 6]]
[[4]
[5]
[6]
[7]
[5]
[6]
[7]
[8]
[7]
[8]
[9]
[6]]
empty() function
This function is used to create the empty array or an uninitialized
array of specified data types and shape.
For e.g.
import numpy as np
print(x)
print(y)
Output-
[[0 0]
[0 0]
[0 0]]
[[6.23042070e-307 4.67296746e-307 1.69121096e-306 8.45593934e-307]
[6.23058028e-307 2.22522597e-306 1.33511969e-306 1.37962320e-306]
[9.34604358e-307 9.79101082e-307 1.78020576e-306 1.69119873e-306]
[2.22522868e-306 1.24611809e-306 8.06632139e-308 2.29178686e-312]]
Indexing in 2-D dimension array
array can be accessed by specifying the location number of the row and
A[0][0] => represents 0th row and 0th column element in array A
A[1][3] => represents 1st row and 3rd column element in the array A
0 1 2
0 1 2 3
4 5 6
1
7 8 9
2
Slicing in 1D Array
Syntax-
Arrayname[start:stop:stepsize]
-5 -4 -3 -2 -1
6 7 8 9 23
0 1 2 3 4
A[:5] will give [6 7 8 9 23]
A[::2] will give [6 8 23]
A[-1:-5:-1] will give [23 9 8 7]
A[2:-2] will give [8]
A[0:2, 0:3]
Or 11 2 3 56 14
A[:2, :3]
40 52 16 12 20
2Nd row to 3RD row,
A[2:4, 3:]
18 30 17 44 49
25 55 66 78 82
A[0:2, 2:]
Or 11 2 3 56 14
A[:2, 2:]
40 52 16 12 20
A[2:, 3:] 18 30 17 44 49
25 55 66 78 82
0th row and 0th column
element 11 2 3 56 14
A[0:1, 0:1]
40 52 16 12 20
A[2:3, 1:2]
18 30 17 44 49
25 55 66 78 82
0th row and 4th row as( 0+4=4), 0th column and 3rd column as (0+3=3)
40 52 16 12 20
70 8 9 32 22
18 30 17 44 49
25 55 66 78 82
Negative
Index -5 -4 -3 -2 -1
-5 11 2 3 56 14
(-2th row ), -4 40 52 16 12 20
(-5th column
and -3rd -3 70 8 9 32 22
column, -1st
column) -2 18 30 17 44 49
A[-2:-3, -5::2]
-1 25 55 66 78 82
eye() or identity() Function
The eye() function creates a 2D array and fills the elements in the
diagonal with 1s.
Syntax-eye(n, dtype=datatype)
This function will create an array with n rows and n columns with
diagonal elements as 1s.The default data type is float.
e-g-
import numpy
a=numpy.eye(3)
print(a)
output-
[ [ 1. 0. 0.]
[ 0. 1. 0.]
[0. 0. 1.] ]
zeros() function in 2D array
This function is used to create two dimensional array with the 0 as
default value and default data type is float.
import numpy
Q = numpy.zeros([3,2], dtype = int)
print(Q)
print(Z)
Output-
[[0 0]
[0 0]
[0 0]]
[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]
ones() function in 2D array
This function will be used to create the array with 1 as default value
for each of individual defined element.
import numpy
Q = numpy.ones([3,2], dtype = int)
print(Q)
print(Z)
Output-
[[1 1]
[1 1]
[1 1]]
[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]
Joins in Array
We can join array in numpy by following method-
1. Concatenate()
2. hstack()
3. vstack()
1. concatenate()
concatenate()- is used to join more than one array
e.g.-
import numpy as np
Output-
a=np.array([2,3,4,50])
[2 3 4 50 8 9 10 11 15]
b=np.array([8, 9,10,11,15])
c=np.concatenate([a,b])
print (c)
import numpy as np
a=np.array([[2,3,4],[4,5,6],[7,8,9]])
[ [2 3 4 2 3 4]
[4 5 6 4 5 6]
[7 8 9 7 8 9] ]
a=np.array([[2,3,4],[4,5,6],[7,8,9]])
[4 5 6]
[7 8 9]
[2 3 4]
[4 5 6]
[7 8 9] ]
2. hstack()
hstack() – It is used to join more than one array
e.g.-
import numpy as np
a=np.array([1,2,3])
Output-
b=np.array([10,11,12])
[1 2 3 10 11 12]
c=np.hstack((a,b))
print (c)
3. vstack()
vstack() – It is used to join more than one array
e.g.-
import numpy as np
Output-
a=np.array([1,2,3])
[[1 2 3 ]
b=np.array([10,11,12])
[10 11 12]]
c=np.vstack((a,b))
print (c)
Array subsets
1. split()
2. hsplit()
3. vsplit()
split()
import numpy as np
x1, x2, x3 = np.split(x, [3, 5]) split array into 3 subsets like-
[0 : 3], [3 : 5] and [5 : ]
1 2 3
0 1 2 99 99 3 2 1
3 4 5 6 7
hsplit()
horizontally.
import numpy as np
a= np.arange(16).reshape((4, 4))
print( a)
Output-
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
e.g.-
print(left) 4 5 6 7
print(right) 8 9 10 11
12 13 14 15
Output-
[[ 0 1]
[ 4 5]
[ 8 9]
[12 13]]
[[ 2 3]
[ 6 7]
[10 11]
[14 15]]
vsplit()
vertically.
Example:-
import nump as np
a= np.arange(16).reshape((4, 4))
print (a)
Output-
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
e.g.-
print(top) 4 5 6 7
print(bottom) 8 9 10 11
12 13 14 15
Output-
[[0 1 2 3]
[4 5 6 7]]
[[ 8 9 10 11]
[12 13 14 15]]
Statistical Function in Numpy
Covariance– Covariance is a measure of how two variables vary togethe
r (like the height of a person and the weight of a person in a population
).
e.g.-
importnumpyas np
x =np.array([0,1,2])
y =np.array([2,1,0])
print("\nOriginal array1:")
print(x)
print("\nOriginal array2:")
print(y)
Original array1:
[0 1 2]
Original array2:
[2 1 0]
[ [ 1. - 1. ]
[ -1. 1. ] ]
Basic Statistical Method for Understanding Data
Mean / Average
Mean or Average is a central tendency of the data i.e. a
number around which a whole data is spread out. In a way, it is
a single number which can estimate the value of whole data
set.
Median
Median is the value which divides the data in 2 equal parts i.e.
number of terms on right side of it is same as number of
terms on left side of it when data is arranged in
either ascending or descending order.
Mode
Mode is the term appearing maximum time in data set i.e. term
that has highest frequency.
Standard deviation
Standard deviation is the measurement of average distance
between each quantity and mean. That is, how data is spread
out from mean. A low standard deviation indicates that the
data points tend to be close to the mean of the data set, while
a high standard deviation indicates that the data points are
spread out over a wider range of values.
Variance
Variance is a square of average distance between each
quantity and mean. That is it is square of standard deviation.