Numpy Notes - Page, Read and Delete
Numpy Notes - Page, Read and Delete
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[4]: 1
In [6]: my_array2[0,2]
Out[6]: 3
In [7]: my_array2[0]
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 [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)
Out[13]: (1, 1, 1, 3)
Out[14]: dtype('int8')
In [15]: my_array2
Change on array
In [17]: my_array2
In [19]: listarray
In [20]: listarray.shape
Out[20]: (3, 3)
In [21]: listarray.size
Out[21]: 9
In [22]: listarray.dtype
Out[22]: dtype('int32')
In [24]: zeros
In [26]: rng
In [29]: ide
In [30]: ide.shape
Re shape array
In [32]: arr.reshape(33,3) #Reshaping
In [34]: arr.shape
Out[34]: (99,)
In [36]: ar
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).
In [40]: ar
Check dimention
In [41]: ar.ndim #dimension of array
Out[41]: 2
Make it Iterable
In [42]: ar.flat #made it Iterable
1
2
3
4
5
6
7
1
0
Out[44]: 36
Out[46]: 2
In [47]: arr.argmax() #returns the index (or indices) corresponding to the largest element
Out[47]: 3
In [48]: ar
Out[49]: 8
In [50]: ar.argmax()
Out[50]: 6
In [51]: ar.argmin(axis=1)
In [52]: ar
Use where
In [53]: np.where(ar>5) # use of where
In [54]: ar[1,2]
Out[54]: 6
In [55]: ar[2,0]
Out[55]: 7
In [56]: ar
Out[57]: 8
In [58]: ar
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]
In [63]: sys.getsizeof(1)* len(py_array) #check how much memomy normal python array is consuming
Out[63]: 112
Out[64]: 16
Operations in matrix
In [66]: aa1
In [67]: aa2
In [68]: aa1+aa2
In [70]: np.sqrt(aa1)
In [71]: np.sqrt(aa2)
In [73]: np.sort(aa3, axis = 1) #the sorting operation is performed along the column for each rows.
In [74]: np.sort(aa3, axis = 0) #the sorting operation is performed along the rows for each column.
In [75]: aa3