6/21/2021 DAN LAB 10-NUMPY Part2 - Jupyter Notebook
In [3]: import numpy as np
In [8]: mx_1s = np.ones((3,4), dtype = bool)
print(mx_1s)
[[ True True True True]
[ True True True True]
[ True True True True]]
In [7]: mx_os = np.zeros((4,6), dtype = bool)
print(mx_os)
[[False False False False False False]
[False False False False False False]
[False False False False False False]
[False False False False False False]]
NUMPY FUNCTION
Arange()
In [10]: #arange(start, end, steps)
ar_1d = np.arange(1,13,2)
print(ar_1d)
[ 1 3 5 7 9 11]
linear()
localhost:8888/notebooks/DAN LAB 10-NUMPY Part2.ipynb 1/7
6/21/2021 DAN LAB 10-NUMPY Part2 - Jupyter Notebook
In [14]: #linear(start, end, no.of elemnts)
li = np.linear(1,10)
print(li)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-14-495d86692b36> in <module>
1 #linear(start, end, no.of elemnts)
----> 2 li = np.linear(1,10)
3 print(li)
~\anaconda3\lib\site-packages\numpy\__init__.py in __getattr__(attr)
212 return Tester
213 else:
--> 214 raise AttributeError("module {!r} has no attribute "
215 "{!r}".format(__name__, attr))
216
AttributeError: module 'numpy' has no attribute 'linear'
RESHAPE()
In [15]: # converrting 1 d array to 2d
ar_1d
Out[15]: array([ 1, 3, 5, 7, 9, 11])
In [16]: arr_1d= np.arange(1,13)
print(arr_1d)
[ 1 2 3 4 5 6 7 8 9 10 11 12]
In [17]: arr_2d = arr_1d.reshape(2,6)
print(arr_2d)
[[ 1 2 3 4 5 6]
[ 7 8 9 10 11 12]]
In [18]: arr_2d = arr_1d.reshape(3,4)
print(arr_2d)
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
In [30]: dummpy_arr= np.arange(1,15)
print(dummpy_arr)
[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14]
localhost:8888/notebooks/DAN LAB 10-NUMPY Part2.ipynb 2/7
6/21/2021 DAN LAB 10-NUMPY Part2 - Jupyter Notebook
In [31]: arr_dum = dummpy_arr.reshape(2,7)
print(arr_dum)
[[ 1 2 3 4 5 6 7]
[ 8 9 10 11 12 13 14]]
In [36]: arr_3d = arr_1d.reshape(2,3,2) #(2,3,2) #(4,3,1)
print(arr_3d)
[[[ 1 2]
[ 3 4]
[ 5 6]]
[[ 7 8]
[ 9 10]
[11 12]]]
ravel()
In [38]: #convert multidimensional array to 1d
ar= np.arange(1,13).reshape(2,6)
print(ar)
[[ 1 2 3 4 5 6]
[ 7 8 9 10 11 12]]
In [40]: ar.ravel()
Out[40]: array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
flatten()
In [42]: ar.flatten()
Out[42]: array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
transpose()
In [43]: #convert rows into columns
ar.transpose()
Out[43]: array([[ 1, 7],
[ 2, 8],
[ 3, 9],
[ 4, 10],
[ 5, 11],
[ 6, 12]])
localhost:8888/notebooks/DAN LAB 10-NUMPY Part2.ipynb 3/7
6/21/2021 DAN LAB 10-NUMPY Part2 - Jupyter Notebook
In [44]: ar
Out[44]: array([[ 1, 2, 3, 4, 5, 6],
[ 7, 8, 9, 10, 11, 12]])
Mathematics operation
In [45]: array1 = np.arange(1,10)
array2 = np.arange(1,10)
print(array1)
print(array2)
[1 2 3 4 5 6 7 8 9]
[1 2 3 4 5 6 7 8 9]
Addition of two matrix
In [46]: array1+array2
Out[46]: array([ 2, 4, 6, 8, 10, 12, 14, 16, 18])
In [47]: np.add(array1,array2)
Out[47]: array([ 2, 4, 6, 8, 10, 12, 14, 16, 18])
Subtraction
In [48]: array1-array2
Out[48]: array([0, 0, 0, 0, 0, 0, 0, 0, 0])
In [49]: np.subtract(array1,array2)
Out[49]: array([0, 0, 0, 0, 0, 0, 0, 0, 0])
Divide
In [50]: array1/array2
Out[50]: array([1., 1., 1., 1., 1., 1., 1., 1., 1.])
In [51]: np.divide(array1,array2)
Out[51]: array([1., 1., 1., 1., 1., 1., 1., 1., 1.])
localhost:8888/notebooks/DAN LAB 10-NUMPY Part2.ipynb 4/7
6/21/2021 DAN LAB 10-NUMPY Part2 - Jupyter Notebook
Multipliation
In [52]: array1*array2
Out[52]: array([ 1, 4, 9, 16, 25, 36, 49, 64, 81])
In [53]: np.multiply(array1,array2)
Out[53]: array([ 1, 4, 9, 16, 25, 36, 49, 64, 81])
In [54]: #rows and coloumn
array1 @ array2
Out[54]: 285
In [55]: array1.dot(array2)
Out[55]: 285
For max value
In [56]: array1
Out[56]: array([1, 2, 3, 4, 5, 6, 7, 8, 9])
In [57]: array1.max()
Out[57]: 9
In [58]: #for checking index
array1.argmax()
Out[58]: 8
In [59]: array1.max(axis = 0)
Out[59]: 9
In [60]: ar
Out[60]: array([[ 1, 2, 3, 4, 5, 6],
[ 7, 8, 9, 10, 11, 12]])
In [65]: ar.max(axis=1)
Out[65]: array([ 6, 12])
localhost:8888/notebooks/DAN LAB 10-NUMPY Part2.ipynb 5/7
6/21/2021 DAN LAB 10-NUMPY Part2 - Jupyter Notebook
In [67]: ar.min(axis = 0)
Out[67]: array([1, 2, 3, 4, 5, 6])
In [68]: ar.min(axis = 1)
Out[68]: array([1, 7])
In [70]: ar.min()
Out[70]: 1
SUM OF ARRAY /MATRIX
In [74]: ad = np.sum(ar)
print(ad)
78
In [75]: ar
Out[75]: array([[ 1, 2, 3, 4, 5, 6],
[ 7, 8, 9, 10, 11, 12]])
average
In [76]: array1
Out[76]: array([1, 2, 3, 4, 5, 6, 7, 8, 9])
In [77]: np.mean(array1)
Out[77]: 5.0
In [78]: np.std(array1)
Out[78]: 2.581988897471611
In [79]: np.exp(array1)
Out[79]: array([2.71828183e+00, 7.38905610e+00, 2.00855369e+01, 5.45981500e+01,
1.48413159e+02, 4.03428793e+02, 1.09663316e+03, 2.98095799e+03,
8.10308393e+03])
In [80]: np.log(array1)
Out[80]: array([0. , 0.69314718, 1.09861229, 1.38629436, 1.60943791,
1.79175947, 1.94591015, 2.07944154, 2.19722458])
localhost:8888/notebooks/DAN LAB 10-NUMPY Part2.ipynb 6/7
6/21/2021 DAN LAB 10-NUMPY Part2 - Jupyter Notebook
In [ ]:
localhost:8888/notebooks/DAN LAB 10-NUMPY Part2.ipynb 7/7