Numpy
Numpy
[1 2 3 4 5]
[ ]: # 2- Array
a = np.array([[1,2,3],[4,5,6],[7,8,9]])
print(a)
[[1 2 3]
[4 5 6]
[7 8 9]]
[ ]: # 3- Array
a = np.array([[[1,2,3],[4,5,6]],[[7,8,9],[1,2,3]]])
print(a)
[[[1 2 3]
[4 5 6]]
[[7 8 9]
[1 2 3]]]
dtype use for converting from one datatype to another datatype
[ ]: # dtype
np.array([1,2,3],dtype = float)
numpy.arange() function is used to create an array of evenly spaced values within a given interval
1
[ ]: # np.arange
print(np.arange(1,11,4))
print(np.arange(2,21,2))
[1 5 9]
[ 2 4 6 8 10 12 14 16 18 20]
reshape() function in NumPy is used to change the shape of a NumPy array without altering its
data
[ ]: # with reshape
np.arange(1,13).reshape(3,4)
[ ]: array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12]])
numpy.ones() function returns a new array of given shape and type, with ones.
[ ]: # np.ones and
np.ones((3,4),dtype = int)
[ ]: array([[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]])
numpy.zeroes() function returns a new array of given shape and type, with zeroes.
[ ]: # np.zeroes
np.zeros((3,4),dtype = int)
[ ]: array([[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]])
[ ]: # np.random
np.random.random((3,3))
linspace function from NumPy creates an array of evenly spaced numbers over a specified inter-
val,useful when needing a specific number of points within a range, unlike arange which relies on a
step size
[ ]: # np.linspace ----> (b/w two point distance is always same)
np.linspace(-10,10,20)
2
[ ]: array([-10. , -8.94736842, -7.89473684, -6.84210526,
-5.78947368, -4.73684211, -3.68421053, -2.63157895,
-1.57894737, -0.52631579, 0.52631579, 1.57894737,
2.63157895, 3.68421053, 4.73684211, 5.78947368,
6.84210526, 7.89473684, 8.94736842, 10. ])
[ ]: # np.identity
np.identity((3),dtype = int)
[ ]: array([[1, 0, 0],
[0, 1, 0],
[0, 0, 1]])
[ ]:
[ ]:
1 Array Attribute
[ ]: a1 = np.arange(10) # ---> vector
a2 = np.arange(12,dtype = float).reshape(3,4) # ---> Matrix
a3 = np.arange(8,dtype = np.int64).reshape(2,2,2) # ---> tensor
1
2
3
shape, reutrn how many column and row in the datasets
[ ]: # shape
a2.shape
[ ]: (3, 4)
3
[ ]: # Size
print(a2.size)
12
[ ]: # itemsize
a3.itemsize
[ ]: 8
[ ]: # dtype
print(a1.dtype)
print(a2.dtype)
print(a3.dtype)
int32
float64
int64
[ ]:
[ ]:
2 Changing Datatype
astype() method returns a new DataFrame where the data types has been changed to the specified
type
[ ]: # astype
a3.astype(np.int32)
[ ]: array([[[0, 1],
[2, 3]],
[[4, 5],
[6, 7]]])
[ ]:
3 Array Operation
[ ]: a1 = np.arange(12).reshape(3,4)
a2 = np.arange(12,24).reshape(3,4)
[ ]: # scalar operation
4
# arithmetic
a1 ** 2
[ ]: array([[ 0, 1, 4, 9],
[ 16, 25, 36, 49],
[ 64, 81, 100, 121]])
[ ]: # Relational
a1 > 5
[ ]: # Vector operation
# Arithmetic
a1 + a2
[ ]:
[ ]:
4 Array Functions
[ ]: a1 = np.random.random((3,3))
a1 = np.round(a1*100)
a1
[ ]: # min/max/sum/prod
# 0 --> Columns and 1 --> Rows
print(np.min(a1,axis = 1))
print(np.max(a1))
print(np.sum(a1))
print(np.prod(a1))
5
492.0
400984279065216.0
[ ]: # mean/median/std/var
print(np.mean(a1,axis=1))
print(np.median(a1,axis=1))
print(np.std(a1,axis=1))
print(np.var(a1,axis=1))
[ ]: # trigonometric function
np.sin(a1)
[ ]: # dot product ( fist matrix columns should be equal to second matrix row)
a2 = np.arange(12).reshape(3,4)
a3 = np.arange(12,24).reshape(4,3)
[ ]: np.dot(a2,a3)
[ ]: # round/floor/ceil
print(np.round(np.random.random((2,3))*100))
print()
print(np.floor(np.random.random((2,3))*100))
print()
print(np.ceil(np.random.random((2,3))*100))
6
[[55. 30. 90.]
[21. 47. 87.]]
[[54. 7. 60.]
[60. 63. 35.]]
[ ]:
[ ]:
[ ]: a1
[ ]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
[ ]: 9
[ ]: a2
[ ]: array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
[ ]: a2[1,2]
[ ]: 6
[ ]: a3
[ ]: array([[[0, 1],
[2, 3]],
[[4, 5],
[6, 7]]])
[ ]: a3[0,0,1]
[ ]: 1
7
[ ]: # slicing
a1[2:6]
[ ]: array([2, 3, 4, 5])
[ ]: a2
[ ]: array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
[ ]: a2[2,:]
[ ]: a2[:,3]
[ ]: array([ 3, 7, 11])
[ ]: a2[::2,::3]
[ ]: array([[ 0, 3],
[ 8, 11]])
[ ]: a2[::2,1::2]
[ ]: array([[ 1, 3],
[ 9, 11]])
[ ]: a2[0:2,1:]
[ ]: array([[1, 2, 3],
[5, 6, 7]])
0
1
2
3
4
5
6
7
[ ]:
8
[ ]: # Transpose
np.transpose(a2)
a2.T
[ ]: array([[ 0, 4, 8],
[ 1, 5, 9],
[ 2, 6, 10],
[ 3, 7, 11]])
[ ]: # ravel
a2.ravel()
[ ]:
6 Stacking
[ ]: a4 = np.arange(12).reshape(3,4)
a5 = np.arange(12,24).reshape(3,4)
[ ]: # horizontal stacking
np.hstack((a4,a5))
[ ]: # vertical stacking
np.vstack((a4,a5))
[ ]: array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]])
[ ]:
9
7 Splitting
[ ]: # horizontal splitting
np.hsplit(a4,2)
[ ]: [array([[0, 1],
[4, 5],
[8, 9]]),
array([[ 2, 3],
[ 6, 7],
[10, 11]])]
[ ]: # vertical splitting
np.hsplit(a5,4)
[ ]: [array([[12],
[16],
[20]]),
array([[13],
[17],
[21]]),
array([[14],
[18],
[22]]),
array([[15],
[19],
[23]])]
[ ]:
[ ]:
[ ]:
10
2.9204180240631104
[ ]: # numpy
a = np.arange(10000000)
b = np.arange(10000000,20000000)
start = time.time()
c = a+b
print(time.time()-start)
0.14705801010131836
[ ]: 2.9204180240631104/0.14705801010131836
[ ]: 19.858952409671762
[ ]: # memory
[ ]: 89095160
[ ]: 10000112
[ ]:
[ ]:
[ ]:
9 Advanced Indexing
[ ]: a = np.arange(12).reshape(4,3)
a
[ ]: array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11]])
11
[ ]: # fancy indexing
a[[0,2,3]]
[ ]: array([[ 0, 1, 2],
[ 6, 7, 8],
[ 9, 10, 11]])
[ ]: a[:,[1,2]]
[ ]: array([[ 1, 2],
[ 4, 5],
[ 7, 8],
[10, 11]])
[ ]: array([92, 60, 91, 82, 61, 84, 58, 84, 93, 81, 77])
[ ]: array([92, 24, 10, 60, 44, 6, 82, 84, 58, 48, 84, 50])
[ ]: array([92, 24, 10, 60, 44, 6, 82, 61, 13, 15, 58, 48, 93, 50, 25, 81, 39,
27])
[ ]:
12
[ ]:
10 Broadcasting
The term broadcasting how numpy treat array with different shapes during arithmatic operations.
The smaller array is “broadcast” across the larger array so that they have compatible shapes.
[ ]: # same shape
a = np.arange(6).reshape(2,3)
b = np.arange(6,12).reshape(2,3)
print(a)
print("*"*50)
print(b)
print("*"*50)
print(a+b)
[[0 1 2]
[3 4 5]]
**************************************************
[[ 6 7 8]
[ 9 10 11]]
**************************************************
[[ 6 8 10]
[12 14 16]]
[ ]: # diff shape
a = np.arange(6).reshape(2,3)
b = np.arange(3).reshape(1,3)
print(a)
print("*"*50)
print(b)
print("*"*50)
print(a+b)
[[0 1 2]
[3 4 5]]
**************************************************
[[0 1 2]]
**************************************************
[[0 2 4]
[3 5 7]]
[ ]:
11 Broadcasting Rules
1. Make the two arrays have the same number of dimensions.
13
If the numbers of dimensions of the two arrays are different, add new dimensions with size 1
to the head of the array with the smaller dimension.
2. Make each dimension of the two arrays the same size.
If the sizes of each dimension of the two arrays do not match, dimensions with size 1 are
stretched to the size of the other array. If there is a dimension whose size is not 1 in either
of the two arrays, it cannot be broadcasted, and an error is raised.
[ ]: # More examples
a = np.arange(12).reshape(4,3)
b = np.arange(3)
print(a)
print(b)
print(a+b)
[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]
[ 9 10 11]]
[0 1 2]
[[ 0 2 4]
[ 3 5 7]
[ 6 8 10]
[ 9 11 13]]
[ ]: a = np.arange(3).reshape(1,3)
b = np.arange(3).reshape(3,1)
print(a)
print(b)
print(a+b)
[[0 1 2]]
[[0]
[1]
[2]]
[[0 1 2]
[1 2 3]
[2 3 4]]
[ ]: a = np.arange(3).reshape(1,3)
b = np.arange(4).reshape(4,1)
print(a)
14
print(b)
print(a + b)
[[0 1 2]]
[[0]
[1]
[2]
[3]]
[[0 1 2]
[1 2 3]
[2 3 4]
[3 4 5]]
[ ]: a = np.array([1])
# shape -> (1,1)
b = np.arange(4).reshape(2,2)
# shape -> (2,2)
print(a)
print(b)
print(a+b)
[1]
[[0 1]
[2 3]]
[[1 2]
[3 4]]
[ ]:
[ ]:
15
a = np.arange(10)
sigmoid(a)
[ ]: predicted
[ ]: array([24, 22, 7, 33, 29, 34, 37, 48, 20, 41, 13, 41, 47, 33, 22, 36, 8,
18, 27, 19, 2, 36, 9, 15, 16])
[ ]: actual
[ ]: array([11, 43, 36, 45, 35, 19, 11, 35, 43, 47, 25, 25, 35, 7, 41, 17, 33,
7, 6, 22, 49, 47, 12, 11, 39])
[ ]: def mse(actual,predicted):
return np.mean((actual - predicted)**2)
mse(actual,predicted)
[ ]: 371.52
[ ]:
[ ]: a[~np.isnan(a)]
[ ]: np.isnan(a).sum()
[ ]: 1
[ ]:
16
[ ]:
[ ]: # sorting
a = np.random.randint(1,100,12)
np.sort(a)
[ ]: array([ 9, 11, 12, 17, 17, 18, 26, 36, 69, 88, 94, 95])
[ ]: array([ 17, 17, 12, 26, 9, 69, 94, 18, 88, 36, 11, 95, 200])
[ ]: np.append(b,np.random.random((b.shape[0],1)),axis=1)
17
7.51249861e-01]])
[ ]: # concatenate
[ ]:
18