vertopal.com_12_Numpy
vertopal.com_12_Numpy
1D Array
import numpy as np
array_1 = np.array([1,2,3])
print(array_1)
[1 2 3]
import numpy as np
array_2 = np.array((1,2,3))
print(array_2)
[1 2 3]
import pandas as pd
print(pd.__version__)
2.2.3
import numpy as np
print(np.__version__)
2.2.0
2D Array
import numpy as np
[[1 2 3]
[4 5 6]
[7 8 9]]
np.array([1,2,3,4,5], dtype="complex")
array1= np.array([1,2,3])
array2= np.array([30,40,50])
print(array1+array2)
[31 42 53]
# import numpy as np
list1 = [1,2,3]
list2 = [30,40,50]
list_ans=[]
print(list_ans)
---> NumPy arrays have fixed size of array creation but it is not the case when we talk about
lists.
---> The element in the NumPy array are all required to be of same data type.
---> NumPy takes very less time to perform atithmetic activities between arrays and list
takes more time.
---> NumPy takes less space as compared to that of lists.
arr1 = np.array([1,2,3,4,50])
arr1.ndim
import numpy as np
arr2 = np.array([[1,2,3],[4,5,6],[7,8,9]])
arr2.ndim
a1 = np.array([1,2,3,4,5])
a1.shape
(5,)
import numpy as np
a2 = np.array([[1,2,3],[4,5,6],[7,8,9]])
a2.shape
(3, 3)
a1 = np.array([1,2,3,4,5])
a1.size
import numpy as np
a2 = np.array([[1,2,3],[4,5,6],[7,8,9]])
a2.size
array01 = np.arange(10)
print(array01)
[0 1 2 3 4 5 6 7 8 9]
import numpy as np
print(array02)
[ 0. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11.]
a1 = np.arange(10).astype(np.int32)
a1.itemsize
import numpy as np
a3 = np.arange(10).astype(np.int64)
a3.itemsize
import numpy as np
a5 = np.arange(12).astype(float)
a5.itemsize
a1 = np.arange(12, dtype="float").reshape(4,3)
print(a1)
How check the Data Type of An Array
import numpy as np
a1 = np.arange(12)
a2 = np.arange(10, dtype=float)
print(a1.dtype)
print(a2.dtype)
int64
float64
a1 = np.arange(12, dtype="float").reshape(4,3)
a1.astype(np.int64)
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11]])
a1 = np.arange(12, dtype="float").reshape(4,3)
for i in nditer(a1):
prin(i)
np.zeros(10)
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
np.ones(10)
array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])
Create array element using Random
import numpy as np
np.random.random(12).reshape(4,3)
Array Operation
Scaler Operations Arithmetic
import numpy as np
a1 = np.arange(12).reshape(3,4)
a2 = np.arange(12,24).reshape(3,4)
print(a1)
print("\n")
print(a2)
print("\n")
#scaler operation
print(a1*2)
print("\n")
print(a2*3)
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]
[[ 0 2 4 6]
[ 8 10 12 14]
[16 18 20 22]]
[[36 39 42 45]
[48 51 54 57]
[60 63 66 69]]
Vector Operation Artithmetic
import numpy as np
a1 = np.arange(12).reshape(3,4)
a2 = np.arange(12,24).reshape(3,4)
print(a1+a2)
print("\n")
print(a2-a1)
[[12 14 16 18]
[20 22 24 26]
[28 30 32 34]]
[[12 12 12 12]
[12 12 12 12]
[12 12 12 12]]
Array Function
round()
import numpy as np
a = np.random.random((3,3))
print(a)
print("\n")
b = np.round(a*100)
print(b)
[[ 9. 84. 42.]
[62. 92. 15.]
[13. 45. 85.]]
a = np.random.random((3,3))
b = np.round(a*100)
print(b)
print("min =",np.min(b))
print("max =",np.max(b))
print("sum =",np.sum(b))
print("prod =",np.prod(b))
print("mean =",np.mean(b))
print("mean of row =",np.mean(b, axis=1)) # row mean
print("mean of col =",np.mean(b, axis=0)) # column mean
print("std =",np.std(b))
print("var =",np.var(b))
print("sin =")
print(np.sin(b))
print("cos =")
print(np.cos(b))
a = np.arange(1,13).reshape(3,4)
b = np.arange(12,24).reshape(4,3)
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
X
[[12 13 14]
[15 16 17]
[18 19 20]
[21 22 23]]
||
[[180 190 200]
[444 470 496]
[708 750 792]]
a = np.arange(1,13).reshape(4,3)
print(np.log(a))
print("\n")
print(np.exp(a))
a = np.random.random((3,3))*100
print(a)
print("round =")
print(np.round(a))
print("floor =")
print(np.floor(a))
print("ceil =")
print(np.ceil(a))
# create a 1D array
array1 = np.array([1, 3, 5, 7, 8, 9, 2, 4, 6])
import numpy as np
import numpy as np
# create a 2D array
array1 = np.array([[1, 3, 5, 7],
[9, 11, 13, 15],
[2, 4, 6, 8]])
# slice the array to get the first two rows and columns
subarray1 = array1[:2, :2]
# slice the array to get the last two rows and columns
subarray2 = array1[1:3, 2:4]
import numpy as np
a = np.arange(10)
b = np.arange(12).reshape(3,4) #reshape(a,b)=> aXb matrix a = row
, b = column
c = np.arange(8).reshape(2,2,2) #reshape(a,b,c)=> a= number of
matrix , b= no of rows in matrix , c= no of col in matrix
print(b)
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
print(c)
[[[0 1]
[2 3]]
[[4 5]
[6 7]]]
7
5
1
import numpy as np
a = np.arange(10)
b = np.arange(12).reshape(3,4) #reshape(a,b)=> aXb matrix a = row
, b = column
c = np.arange(8).reshape(2,2,2) #reshape(a,b,c)=> a= number of
matrix , b= no of rows in matrix , c= no of col in matrix
print(a)
[0 1 2 3 4 5 6 7 8 9]
#find out 3 to 7
print(a[3:8])
[3 4 5 6 7]
[3 5 7]
print(b)
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
Iterating
import numpy as np
a = np.arange(10)
b = np.arange(12).reshape(3,4)
c = np.arange(27).reshape(3,3,3)
for i in a:
print(i)
0
1
2
3
4
5
6
7
8
9
for i in b:
print(b)
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
for i in c:
print(c)
[[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]]
[[ 9 10 11]
[12 13 14]
[15 16 17]]
[[18 19 20]
[21 22 23]
[24 25 26]]]
[[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]]
[[ 9 10 11]
[12 13 14]
[15 16 17]]
[[18 19 20]
[21 22 23]
[24 25 26]]]
[[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]]
[[ 9 10 11]
[12 13 14]
[15 16 17]]
[[18 19 20]
[21 22 23]
[24 25 26]]]
0
1
2
3
4
5
6
7
8
9
10
11
for i in np.nditer(c):
print(i)
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
Transpose Array
import numpy as np
a = np.arange(12).reshape(3,4)
print(a)
print("\n")
print(np.transpose(a))
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[ 0 4 8]
[ 1 5 9]
[ 2 6 10]
[ 3 7 11]]
a = np.arange(12).reshape(3,4)
print(a)
print("\n")
print(np.ravel(a))
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[ 0 1 2 3 4 5 6 7 8 9 10 11]
Stacking
it is used to joining multiple arrays.it joins array along a new axis.
import numpy as np
a = np.arange(12).reshape(3,4)
b = np.arange(12,24).reshape(3,4)
horizontal stacking
np.hstack((a,b))
array([[ 0, 1, 2, 3, 0, 1, 2, 3],
[ 4, 5, 6, 7, 4, 5, 6, 7],
[ 8, 9, 10, 11, 8, 9, 10, 11]])
vertical stacking
np.vstack((a,b))
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
Splitting
Cutting should be equal position
import numpy as np
a = np.arange(12).reshape(3,4)
[array([[0, 1],
[4, 5],
[8, 9]]),
array([[ 2, 3],
[ 6, 7],
[10, 11]])]
[array([[0],
[4],
[8]]),
array([[1],
[5],
[9]]),
array([[ 2],
[ 6],
[10]]),
array([[ 3],
[ 7],
[11]])]