Numpy Mathlib
Numpy Mathlib
ipynb - Colaboratory
import numpy as np
a=np.array([1,2,3,4])
print(a)
b=np.array([1,2.3,4,5])
print(b)
a1=np.array([[1,2],[1,2]])
print(a1)
print(a.shape)
print(a.ndim)
print(a.dtype.name)
print(a.itemsize)
print(a.size)
print(type(a))
print(np.ndarray)
[1 2 3 4]
[1. 2.3 4. 5. ]
[[1 2]
[1 2]]
(4,)
1
int64
8
4
<class 'numpy.ndarray'>
<class 'numpy.ndarray'>
import numpy as np
e=np.ones(5)
f=np.zeros(5)
print(e,f)
a=np.array([1,2,3,4])
a=a+5
print(a)
a=a-5
print(a)
a=a*5
print(a)
a=a/5
print(a)
b=np.array([2,3,4,5,6])
h=b
b=b+4
print(b)
print(h)
k=np.copy(b)
k[0]=0
print(k)
print(b)
a=np.linspace(1,10,6)
print(a)
https://colab.research.google.com/drive/13dAeT5w4IADkZ1LART9AxGEls6qfzKW5#printMode=true 1/9
1/28/24, 5:22 PM Untitled0.ipynb - Colaboratory
import numpy as np
b=np.arange(10) # 0 to 9
print(b)
c=np.arange(5,10)
d=np.arange(10,1,-1)
print(c)
print(d)
[0 1 2 3 4 5 6 7 8 9]
[5 6 7 8 9]
[10 9 8 7 6 5 4 3 2]
import numpy as np
a=np.array([1,2,3,4])
b=np.array([5,6,7,8])
c=np.concatenate([a,b])
print(c)
[1 2 3 4 5 6 7 8]
import numpy as np
a=np.array([[1,2,3,4],[2,3,4,5]])
print(a)
a=a+4 #+,-,*,/
print(a)
print(a.ndim)
print(a.shape)
print(a.size)
print(a.itemsize)
print(a.reshape(8,1))
print(a.reshape(2,4))
[[1 2 3 4]
[2 3 4 5]]
[[5 6 7 8]
[6 7 8 9]]
2
(2, 4)
8
8
[[5]
[6]
[7]
[8]
[6]
[7]
[8]
[9]]
[[5 6 7 8]
[6 7 8 9]]
b=np.empty([2,3])
print(b)
b=np.empty([3,3],dtype=int)
print(b)
b=np.empty([1,3],dtype=float)
print(b)
[[ 1. 2.8 4.6]
[ 6.4 8.2 10. ]]
[[ 4607182418800017408 0 0]
[-9223372036854775808 -4616189618054758400 -9223372036854775808]
[ 0 0 4607182418800017408]]
[[0. 0.99999869 1. ]]
import numpy as np
a=np.array([1,2,3,4,5,6,7,8])
b=np.array_split(a,[3,5])
print(b)
c=np.array([[1,2,3,4],[2,3,4,5]])
d=np.array_split(c,4)
print(d)
sp=np.hsplit(c,2)
print(sp)
sp=np.vsplit(c,2)
print(sp)
https://colab.research.google.com/drive/13dAeT5w4IADkZ1LART9AxGEls6qfzKW5#printMode=true 2/9
1/28/24, 5:22 PM Untitled0.ipynb - Colaboratory
import numpy as np
a=np.array([[1,2,3,4],[2,3,4,5],[4,5,6,7]])
print(a[0:2,:])
print(a[:,1:3])
b=np.array([1,2,3,4,5,6]) #indexing
print(b[2:-2])
print(b[0:1])
print(b[0:5:2])
c=np.zeros([2,2],dtype=int)
print(c)
c=np.zeros([3,2],dtype=float)
print(c)
d=np.eye(4)
print(d)
[[1 2 3 4]
[2 3 4 5]]
[[2 3]
[3 4]
[5 6]]
[3 4]
[1]
[1 3 5]
[[0 0]
[0 0]]
[[0. 0.]
[0. 0.]
[0. 0.]]
[[1. 0. 0. 0.]
[0. 1. 0. 0.]
[0. 0. 1. 0.]
[0. 0. 0. 1.]]
A=np.ones([2,2],dtype=int)
print(A)
A=np.ones([3,1],dtype=float)
print(A)
[[1 1]
[1 1]]
[[1.]
[1.]
[1.]]
import numpy as np
a=np.array([[1,2,3,4],[2,3,4,5]])
b=np.empty([3,4],dtype=int)
print(b)
[[93953841706394 0 0 0]
[ 0 0 0 0]
[ 0 0 0 0]]
import numpy as np
a=np.array([[1,2,3,4],[2,3,4,5]])
d=np.concatenate([a,a],axis=0)
print(d)
x=np.array([1,2,3,4,5,6])
b=np.hstack(x)
c=np.vstack(x)
print(b)
print(c)
[[1 2 3 4]
[2 3 4 5]
[1 2 3 4]
https://colab.research.google.com/drive/13dAeT5w4IADkZ1LART9AxGEls6qfzKW5#printMode=true 3/9
1/28/24, 5:22 PM Untitled0.ipynb - Colaboratory
[2 3 4 5]]
[1 2 3 4 5 6]
[[1]
[2]
[3]
[4]
[5]
[6]]
import numpy as np
a=np.array([[1,2,3,4],[2,3,4,5]])
a=a.reshape(4,2)
print(a)
a=([1,2,3,4,5])
b=([6,7,7,8,5])
c=np.hstack((a,b)) #np.vsplit and np.hsplit
d=np.vstack((a,b))
print(c)
print(d)
[[1 2]
[3 4]
[2 3]
[4 5]]
[1 2 3 4 5 6 7 7 8 5]
[[1 2 3 4 5]
[6 7 7 8 5]]
x=([1,2,3])
y=([2,4,6])
plt.plot(x,y)
plt.xlabel("Score")
plt.ylabel("Match")
plt.title("Day-1")
plt.show()
https://colab.research.google.com/drive/13dAeT5w4IADkZ1LART9AxGEls6qfzKW5#printMode=true 4/9
1/28/24, 5:22 PM Untitled0.ipynb - Colaboratory
x = [0,2,3,4,5,6,7]
y1=[5,7,9,0,-1,6,8]
y2=[0,6,-2,-9,3,1,9]
plt.plot(x,y1,label="Day",linewidth=4,linestyle='dashed')
plt.plot(x,y2,label="Score")
plt.legend(loc=2)
plt.show()
https://colab.research.google.com/drive/13dAeT5w4IADkZ1LART9AxGEls6qfzKW5#printMode=true 5/9
1/28/24, 5:22 PM Untitled0.ipynb - Colaboratory
x=np.arange(1,11)
y=x*3-2
plt.plot(x,y,marker='+',markeredgecolor='red',markersize='10')
plt.show()
https://colab.research.google.com/drive/13dAeT5w4IADkZ1LART9AxGEls6qfzKW5#printMode=true 6/9
1/28/24, 5:22 PM Untitled0.ipynb - Colaboratory
a=np.array([3,6,9,1])
b=np.array([9,8,3,3])
plt.bar(a,b,width=0.6,color='pink')
plt.show()
https://colab.research.google.com/drive/13dAeT5w4IADkZ1LART9AxGEls6qfzKW5#printMode=true 7/9
1/28/24, 5:22 PM Untitled0.ipynb - Colaboratory
city=np.array(['Morbi','Rajkot','Halvad','Delhi'])
temp=np.array([43,34,29,45])
plt.barh(city,temp)
plt.show()
https://colab.research.google.com/drive/13dAeT5w4IADkZ1LART9AxGEls6qfzKW5#printMode=true 8/9
1/28/24, 5:22 PM Untitled0.ipynb - Colaboratory
https://colab.research.google.com/drive/13dAeT5w4IADkZ1LART9AxGEls6qfzKW5#printMode=true 9/9