In this post lets see about Numpy module it is same as list. We are going to use it in data science.Numpy is used for mathematical calculations. Install Numpy by
pip install numpy
Type this in your cmd and hit enter.
To declare Numpy:
import numpy as np
x = [[1,2,5,3],[1,2,5,3],[1,2,5,3],[1,2,5,3]]
print(x)
y = np.array(x)
print(y)
Output:
[[1, 2, 5, 3], [1, 2, 5, 3], [1, 2, 5, 3], [1, 2, 5, 3]]
[[1 2 5 3]
[1 2 5 3]
[1 2 5 3]
[1 2 5 3]]
This is how the numpy works. Numpy works both on single dimension and 2D array.
Methods for Numpy:
import numpy as np
x=[1,2,3,4]
print(x)
print('----------------------------')
y =[[1,2,3],[4,5,6],[7,8,9]]
z=np.array(y)
print(z)#print in 2 dimension
print('----------------------------')
# to print zero matrix
print(np.zeros(2))
print('-------------------------')
#with two rows and two column
print(np.zeros((2,2)))
# add operation
print("add",np.zeros((2,2))+5)
print('--------------------------')
print("mul",np.zeros((2,2))*5)
print('--------------------------')
print(np.ones((3,4)))
print('--------------------------')
print(np.eye(3))#diagonal matrix
these are some basic methods used in numpy
Output:
[1, 2, 3, 4]
----------------------------
[[1 2 3]
[4 5 6]
[7 8 9]]
----------------------------
[0. 0.]
-------------------------
[[0. 0.]
[0. 0.]]
add [[5. 5.]
[5. 5.]]
--------------------------
mul [[0. 0.]
[0. 0.]]
--------------------------
[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]
--------------------------
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
To print 2D matrix:
import numpy as np
a= np.arange(10)
print(a)
a= np.arange(10).reshape(2,5)
print(a)
[0 1 2 3 4 5 6 7 8 9]
[[0 1 2 3 4]
[5 6 7 8 9]]
To print random number:
-rand to print random floating point
-randb to print random -ve num also
-randint to print integers
import numpy as np
print(np.random.rand(4))
print(np.random.randn(4))
print(np.random.randint(10))
[0.24395678 0.85441042 0.20545385 0.31506932]
[-0.59030679 0.66762296 0.17482717 1.988936 ]
8
To print linear element:
If you want to print 100 element with Linear space that is the difference between each number should be same. Then we use linspace method
import numpy as np
b = np.linspace(0,10)
print(b)
b = np.linspace(0,10,5)
print(b)
[ 0. 0.20408163 0.40816327 0.6122449 0.81632653 1.02040816
1.2244898 1.42857143 1.63265306 1.83673469 2.04081633 2.24489796
2.44897959 2.65306122 2.85714286 3.06122449 3.26530612 3.46938776
3.67346939 3.87755102 4.08163265 4.28571429 4.48979592 4.69387755
4.89795918 5.10204082 5.30612245 5.51020408 5.71428571 5.91836735
6.12244898 6.32653061 6.53061224 6.73469388 6.93877551 7.14285714
7.34693878 7.55102041 7.75510204 7.95918367 8.16326531 8.36734694
8.57142857 8.7755102 8.97959184 9.18367347 9.3877551 9.59183673
9.79591837 10. ]
[ 0. 2.5 5. 7.5 10. ]
Indexing and slicing:
import numpy as np
y = np.arange(1,20)
print(y)
print(y[0])
print(y[7])
print(y[10:])
print(y[:10])
print(y[3:8])
print(y[-5])
print(y[-5:-1])
print(y[-1])
x = y[8:12]=30
print(y)
z= y.copy()
print("y copyed to z")
print(z)
Output:
[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]
1
8
[11 12 13 14 15 16 17 18 19]
[ 1 2 3 4 5 6 7 8 9 10]
[4 5 6 7 8]
15
[15 16 17 18]
19
[ 1 2 3 4 5 6 7 8 30 30 30 30 13 14 15 16 17 18 19]
y copyed to z
[ 1 2 3 4 5 6 7 8 30 30 30 30 13 14 15 16 17 18 19]
Hope you understand the program and try yourself for more clear view ,feel free to comment your thoughts!!
Comments
Post a Comment