1/7/24, 8:42 PM NUMPY Notes by Amar Sharma - Jupyter Notebook
NUMPY Complete notes by Amar Sharma
Import numpy library
In [1]: import numpy as np #import library
array creation
In [2]: my_array1 = np.array([1,5,3], np.int8) #The 8-bit integer has a range from -128 to 127 [-2^(7) to 2^
In [3]: my_array1
Out[3]: array([1, 5, 3], dtype=int8)
In [4]: my_array1[0] #indexing operation
Out[4]: 1
In [5]: my_array2 = np.array([[1,5,3]])
In [6]: my_array2[0,2]
Out[6]: 3
In [7]: my_array2[0]
Out[7]: array([1, 5, 3])
In [8]: my_array2[0,1]
Out[8]: 5
Array shape
In [9]: my_array2.shape #This indicates that the array is 2-dimensional with a shape of 1 row and 3 columns
Out[9]: (1, 3)
In [10]: my_array1.shape #This indicates that the array is 1-dimensional with a length of 3.
Out[10]: (3,)
In [11]: my_array3 = np.array([[[1,5,3]]])
In [12]: my_array3.shape #This indicates that the array is 3-dimensional with a shape of 1 depth, 1 row, and
Out[12]: (1, 1, 3)
In [13]: my_array4 = np.array([[[[1,5,3]]]])
my_array4.shape #4-dimensional with a shape of 1 depth, 1 row, 1 column, and 3 elements along the
Out[13]: (1, 1, 1, 3)
Array data type
In [14]: my_array1.dtype #Data rype
Out[14]: dtype('int8')
127.0.0.1:8889/notebooks/NUMPY Notes by Amar Sharma.ipynb#array-creation 1/8
1/7/24, 8:42 PM NUMPY Notes by Amar Sharma - Jupyter Notebook
In [15]: my_array2
Out[15]: array([[1, 5, 3]])
Change on array
In [16]: my_array2[0,1] = 45 #changing value
In [17]: my_array2
Out[17]: array([[ 1, 45, 3]])
Array creation : conversion from other python structures
In [18]: listarray = np.array([[1,2,3],[5,8,5],[0,3,5]])
In [19]: listarray
Out[19]: array([[1, 2, 3],
[5, 8, 5],
[0, 3, 5]])
In [20]: listarray.shape
Out[20]: (3, 3)
Check array size
In [21]: listarray.size
Out[21]: 9
In [22]: listarray.dtype
Out[22]: dtype('int32')
Make array by zeros
In [23]: zeros = np.zeros((2,5))
In [24]: zeros
Out[24]: array([[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.]])
Make array by arange
In [25]: rng = np.arange(15)
In [26]: rng
Out[26]: array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])
127.0.0.1:8889/notebooks/NUMPY Notes by Amar Sharma.ipynb#array-creation 2/8
1/7/24, 8:42 PM NUMPY Notes by Amar Sharma - Jupyter Notebook
divide equally spaced
In [27]: lspace = np.linspace(1,10,4) #4 elements from 1 to 10 with eaually spaced
lspace
Out[27]: array([ 1., 4., 7., 10.])
Make identity matrix
In [28]: ide = np.identity(45) #formed identity array of row column 45
In [29]: ide
Out[29]: array([[1., 0., 0., ..., 0., 0., 0.],
[0., 1., 0., ..., 0., 0., 0.],
[0., 0., 1., ..., 0., 0., 0.],
...,
[0., 0., 0., ..., 1., 0., 0.],
[0., 0., 0., ..., 0., 1., 0.],
[0., 0., 0., ..., 0., 0., 1.]])
In [30]: ide.shape
Out[30]: (45, 45)
In [31]: arr = np.arange(99)
arr
Out[31]: array([ 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, 27, 28, 29, 30, 31, 32, 33,
34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67,
68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84,
85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98])
127.0.0.1:8889/notebooks/NUMPY Notes by Amar Sharma.ipynb#array-creation 3/8
1/7/24, 8:42 PM NUMPY Notes by Amar Sharma - Jupyter Notebook
Re shape array
In [32]: arr.reshape(33,3) #Reshaping
Out[32]: array([[ 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],
[27, 28, 29],
[30, 31, 32],
[33, 34, 35],
[36, 37, 38],
[39, 40, 41],
[42, 43, 44],
[45, 46, 47],
[48, 49, 50],
[51, 52, 53],
[54, 55, 56],
[57, 58, 59],
[60, 61, 62],
[63, 64, 65],
[66, 67, 68],
[69, 70, 71],
[72, 73, 74],
[75, 76, 77],
[78, 79, 80],
[81, 82, 83],
[84, 85, 86],
[87, 88, 89],
[90, 91, 92],
[93, 94, 95],
[96, 97, 98]])
In [33]: arr.ravel() #back to 1D array
Out[33]: array([ 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, 27, 28, 29, 30, 31, 32, 33,
34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67,
68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84,
85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98])
In [34]: arr.shape
Out[34]: (99,)
Numpy Array Axis
In [35]: ar = np.array([[1,2,3],[4,5,6],[7,1,0]])
In [36]: ar
Out[36]: array([[1, 2, 3],
[4, 5, 6],
[7, 1, 0]])
axis=0: Operations along the rows (sum along each column in a 2D array).
axis=1: Operations along the columns (sum along each row in a 2D array).
127.0.0.1:8889/notebooks/NUMPY Notes by Amar Sharma.ipynb#array-creation 4/8
1/7/24, 8:42 PM NUMPY Notes by Amar Sharma - Jupyter Notebook
In [37]: ar.sum(axis=0) #1+4+7,2+5+1,3+6+0
Out[37]: array([12, 8, 9])
In [38]: ar.sum(axis=1) #1+2+3,4+5+6,7+1+0
Out[38]: array([ 6, 15, 8])
In [39]: ar.T #Transpose
Out[39]: array([[1, 4, 7],
[2, 5, 1],
[3, 6, 0]])
In [40]: ar
Out[40]: array([[1, 2, 3],
[4, 5, 6],
[7, 1, 0]])
Check dimention
In [41]: ar.ndim #dimension of array
Out[41]: 2
Make it Iterable
In [42]: ar.flat #made it Iterable
Out[42]: <numpy.flatiter at 0x1adf961bc80>
In [43]: for i in ar.flat:
print(i)
1
2
3
4
5
6
7
1
0
Check memory captured
In [44]: ar.nbytes #bytes consume by that array
Out[44]: 36
In [45]: arr = np.array([8,55,2,75])
index of smallest and largest
In [46]: arr.argmin() #returns the index (or indices) corresponding to the smallest element
Out[46]: 2
In [47]: arr.argmax() #returns the index (or indices) corresponding to the largest element
Out[47]: 3
127.0.0.1:8889/notebooks/NUMPY Notes by Amar Sharma.ipynb#array-creation 5/8
1/7/24, 8:42 PM NUMPY Notes by Amar Sharma - Jupyter Notebook
In [48]: ar
Out[48]: array([[1, 2, 3],
[4, 5, 6],
[7, 1, 0]])
In [49]: ar.argmin() #1,2,3,4,5,6,7,1,0
Out[49]: 8
In [50]: ar.argmax()
Out[50]: 6
In [51]: ar.argmin(axis=1)
Out[51]: array([0, 0, 2], dtype=int64)
In [52]: ar
Out[52]: array([[1, 2, 3],
[4, 5, 6],
[7, 1, 0]])
Use where
In [53]: np.where(ar>5) # use of where
Out[53]: (array([1, 2], dtype=int64), array([2, 0], dtype=int64))
In [54]: ar[1,2]
Out[54]: 6
In [55]: ar[2,0]
Out[55]: 7
In [56]: ar
Out[56]: array([[1, 2, 3],
[4, 5, 6],
[7, 1, 0]])
Count non zero
In [57]: np.count_nonzero(ar)
Out[57]: 8
In [58]: ar
Out[58]: array([[1, 2, 3],
[4, 5, 6],
[7, 1, 0]])
In [59]: np.nonzero(ar) #0,0 & 0,1 & 0,2 & 1,0 & 1,1 & 1,2 & 2,0 & 2,1 are non zero, except ar[2,2]
Out[59]: (array([0, 0, 0, 1, 1, 1, 2, 2], dtype=int64),
array([0, 1, 2, 0, 1, 2, 0, 1], dtype=int64))
127.0.0.1:8889/notebooks/NUMPY Notes by Amar Sharma.ipynb#array-creation 6/8
1/7/24, 8:42 PM NUMPY Notes by Amar Sharma - Jupyter Notebook
check how much memomy normal python array is consuming
In [60]: import sys
In [61]: py_array = [5,2,3,4]
In [62]: np_array = np.array(py_array)
In [63]: sys.getsizeof(1)* len(py_array) #check how much memomy normal python array is consuming
Out[63]: 112
In [64]: np_array.itemsize * np_array.size #how much numpy array is consuming
Out[64]: 16
Operations in matrix
In [65]: aa1 = np.array([[1,2,3],[4,5,6],[7,8,9]])
aa2 = np.array([[5,4,6],[2,5,8],[2,4,6]])
In [66]: aa1
Out[66]: array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
In [67]: aa2
Out[67]: array([[5, 4, 6],
[2, 5, 8],
[2, 4, 6]])
In [68]: aa1+aa2
Out[68]: array([[ 6, 6, 9],
[ 6, 10, 14],
[ 9, 12, 15]])
In [69]: aa1*aa2 # not possible in python list
Out[69]: array([[ 5, 8, 18],
[ 8, 25, 48],
[14, 32, 54]])
In [70]: np.sqrt(aa1)
Out[70]: array([[1. , 1.41421356, 1.73205081],
[2. , 2.23606798, 2.44948974],
[2.64575131, 2.82842712, 3. ]])
In [71]: np.sqrt(aa2)
Out[71]: array([[2.23606798, 2. , 2.44948974],
[1.41421356, 2.23606798, 2.82842712],
[1.41421356, 2. , 2.44948974]])
127.0.0.1:8889/notebooks/NUMPY Notes by Amar Sharma.ipynb#array-creation 7/8
1/7/24, 8:42 PM NUMPY Notes by Amar Sharma - Jupyter Notebook
Sort & argsort
In [72]: aa3 = np.array([[8,2,3],[4,6,6],[7,4,9]])
aa3
Out[72]: array([[8, 2, 3],
[4, 6, 6],
[7, 4, 9]])
In [73]: np.sort(aa3, axis = 1) #the sorting operation is performed along the column for each rows.
Out[73]: array([[2, 3, 8],
[4, 6, 6],
[4, 7, 9]])
In [74]: np.sort(aa3, axis = 0) #the sorting operation is performed along the rows for each column.
Out[74]: array([[4, 2, 3],
[7, 4, 6],
[8, 6, 9]])
In [75]: aa3
Out[75]: array([[8, 2, 3],
[4, 6, 6],
[7, 4, 9]])
In [76]: np.argsort(aa3, axis = 1) #sort of indices
Out[76]: array([[1, 2, 0],
[0, 1, 2],
[1, 0, 2]], dtype=int64)
In [77]: np.argsort(aa3, axis = 0)
Out[77]: array([[1, 0, 0],
[2, 2, 1],
[0, 1, 2]], dtype=int64)
All methods and attributes check it from here
https://docs.scipy.org/doc/numpy-1.6.0/reference/generated/numpy.ndarray.html (https://docs.scipy.org/doc/numpy-
1.6.0/reference/generated/numpy.ndarray.html)
127.0.0.1:8889/notebooks/NUMPY Notes by Amar Sharma.ipynb#array-creation 8/8