Data Handling Using NumPy
Data Handling Using NumPy
com
Purpose of 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
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.
NUMPY
import numpy or
import numpy as
<<name>>
1. 1D Array
2. 2D Array
3. N-Dimension Array
1D ARRAY
5 6 9 4 3 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)
IMPLEMENTATION
Creating array OFbe
in numpy can 1D done
ARRAYinINseveral
NUMPY ways.
Some of the important ways are-
For e.g :
a = array([10,30,40.5, 50,100])
print(a)
Output : = [10.0,30.0,40.5,50.0,100.0]
2. linspace() Function
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]
zeros(n,datatype)
ones(n,datatype)
Example 1
Example
import numpy as
np K =
np.zeros(5)
R = np.ones(5)
print(K
print(R
Output :
CREATED BY: SACHIN BHARDWAJ, PGT(CS) KV NO.1 TEZPUR, MR. VINOD
[0.,0.,0.,0.,0.]
KUMAR VERMA, PGT (CS) KV OEF KANPUR
[1.,1.,1.,1.,1.]
For More Updates Visit: www.python4csip.com
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.]
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]
Indexes in 2d Array
2D Array structure
x =np.array([[2,4,6],[6,8,10]] 2DArray
elements 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
:1
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]])
itemsize Attribute
The itemsize attributes gives the memory size of array
elements in bytes. For e.g.
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)
d= Reshape array d to 6 rows and 2
d.reshape(6,2) columns
CREATED BY: SACHIN BHARDWAJ, PGT(CS) KV NO.1 TEZPUR, MR. VINOD
KUMAR VERMA, PGT (CS) KV OEF KANPUR
For More Updates Visit: www.python4csip.com
print(d)
d = d.reshape(1,12) Reshape array d to 1row and 12
columns print(d)
d = d.reshape(12,1) Reshape array d to 12rows and 1
column 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
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]]
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
1 4 5 6
2 7 8 9
Slicing in 1D Array
Syntax-
Arrayname[start:stop:stepsize]
The default value of stepsize is “1"
-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]
11 2 3 56 14
Or
A[:2, :3]
40 52 16 12 20
2Nd row to 3RD row,
column to
3RD TH 70 8 9 32 22
A[2:4,
4 3:]
column
18 30 17 44 49
25 55 66 78 82
11 2 3 56 14
Or
A[:2, 2:]
40 52 16 12 20
A[2:, 3:]
18 30 17 44 49
25 55 66 78 82
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
Negativ
e Index
-5 -4 -3 -2 -1
-5 11 2 3 56 14
(-2th row ), -4 40 52 16 12 20
th
(-5 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
e-g-
import numpy
a=numpy.eye(3)
print(a)
output-
[[1 0. 0.]
.
[ 0. 1. 0.]
[0. 0. 1.] ]
import numpy
Q = numpy.zeros([3,2], dtype =
int) Z = numpy.zeros([4,4], dtype
= float) 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.]]
import numpy
Q = numpy.ones([3,2], dtype =
int) Z = numpy.ones([4,4],
dtype = float) 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 but the array must be of the same
shape.
e.g.-
import numpy as np
a=np.array([2,3,4,50]) Output-
b=np.array([8, [2 3 4 50 8 9 10 11 15]
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]])
b=np.concatenate([a,a],axis=1) concatenate array a
print Output-
(b)
[ [2 3 4 2 3 4]
[4 5 6 4 5 6]
[7 8 9 7 8 9] ]
GT(CS) KV NO.1 TEZPUR, MR. VINOD
CREATED BY: SACHIN BHARDWAJ, P
KUMAR VERMA, PGT (CS) KV OEF KANPUR
For More Updates Visit: www.python4csip.com
E.g-2
import numpy as np
a=np.array([[2,3,4],[4,5,6],[7,8,9]])
b=np.concatenate([a,a],axis=0)
concatenate array
a
with array a row wise
print Output-
(b) [ [2 3 4]
[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
horizontally or row wise.
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
vertically or column wise.
e.g.-
import numpy as np
a=np.array([1,2,3]) Output-
[[1 2 3 ]
b=np.array([10,11,12])
c=np.vstack((a,b)) [10 11 12]]
print (c)
Array subsets
split()
import numpy as np
[0 : 3], [3 : 5] and [5 : ]
print(x1, x2,
x3)
Output-[1 2 3] [99 99] [3 2 1]
1 2 3
0 1 2 99 99 3 2 1
3 4 5 6 7
hsplit()
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],
Output
- [[ 0 1]
[ 4 5]
[ 8 9]
[12 13]]
[[ 2 3]
[ 6 7]
[10 11]
[14 15]]
vsplit()
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],
[12, 13, 14, 15]])
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]]
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)
Output-
Original
array1:
[0 1 2]
Original array2:
[2 1 0]
[ -1. 1. ] ]