In [2]:
import numpy as np
In [4]:
l=[1,2,3,4]
l=np.array(l)
l
Out[4]:
In [6]:
a=[1,2,3]
b=[3,4,6]
c=np.array([a,b])
In [10]:
help(np.shape)
In [11]:
c.shape
Out[11]:
In [19]:
l=np.arange(2,24,4)
print(l)
In [21]:
l.reshape(3,2)
Out[21]:
In [22]:
l.reshape(2,3)
Out[22]:
In [26]:
#linspace(a,b,c)
#linspace gives a list back in range (a,b) with c intervals
o=np.linspace(0,1,9)
print(o)
In [27]:
o.reshape(3,3)
Out[27]:
In [30]:
print(np.zeros(3))
In [31]:
print(np.ones(3))
In [34]:
print(np.eye(5))
In [55]:
np.diag(o.reshape(3,3))
Out[55]:
In [56]:
np.diag([4,5,6])
Out[56]:
In [57]:
np.array([1,2,3]*3)
Out[57]:
In [58]:
np.repeat([1,2,3],3)
Out[58]:
In [63]:
#Uncomment the below line comment the line below it and see the difference
#a=[1,2,4]
a=np.array([1,2,4])
np.vstack([a ,2*a])
Out[63]:
In [64]:
np.hstack([a ,2*a])
Out[64]:
In [91]:
x=np.array([1,2,3])
y=np.array([[1,2,2],[1,2,4]])
y.dot(x)
Out[91]:
In [81]:
y
Out[81]:
In [82]:
y.T
Out[82]:
In [85]:
##Type of array
x.dtype
Out[85]:
In [87]:
##Casting array as another type
x.astype('f')
Out[87]:
In [94]:
#some math functions
a=np.array([1,3,4,-6,7,4,6,11,2])
a.sum()
Out[94]:
In [95]:
a.min()
Out[95]:
In [96]:
a.mean()
Out[96]:
In [97]:
a.std()
Out[97]:
In [98]:
a.argmax()
Out[98]:
In [101]:
b=np.arange(1,18,2)**2
In [104]:
b[5]
Out[104]:
In [122]:
b[1:4]
Out[122]:
In [136]:
r=np.arange(0,36)
r.resize(6,6)
In [121]:
##some operations
r[r>30]
Out[121]:
In [127]:
r[-1,-5:-2]
Out[127]:
In [128]:
r[r>30]=30
In [129]:
r
Out[129]:
In [130]:
r2=r[:,:]
In [135]:
r2
Out[135]:
In [134]:
##Changimg r2 changes r
#Uncommemt thebelow lines to observe it
#r2[:]=0
#r
Out[134]:
In [133]:
#s0 to copy use r copy
Out[133]:
In [141]:
##so to copy we should use rcopy
r_copy=r.copy()
r_copy
Out[141]:
In [143]:
##now changing r copy
r_copy[r_copy>10]=-3
r
Out[143]:
In [144]:
r_copy
Out[144]:
In [152]:
r[0,:6]
Out[152]:
In [153]:
old = np.array([[1, 1, 1],
[1, 1, 1]])
new = old
new[0, :2] = 0
print(old)
In [163]:
rnd=np.random.randint(1,30,(4,3))
In [164]:
rnd
Out[164]:
In [167]:
for row in range(len(rnd)):
print(rnd[row])
In [168]:
for i , row in enumerate(rnd):
print(i," ",row)
In [169]:
for i in zip(rnd):
print(i)
In [170]:
for i,j in zip(rnd,rnd**2):
print(i+j)
In [ ]: