Num Py
Num Py
Num Py
NumPy stands for 'Numerical Python'. It is a Python library used for working with arrays. It is a library consisting of multidimensional array objects.
It is useful in linear algebra, random number capability etc. NumPy array can also be used as an efficient multi-dimensional container for generic
data.
Numpy array is a powerful N-dimensional array object which is in the form of rows and columns. We can initialize NumPy arrays from nested Python
lists and access it elements.
Installation of NumPy
!pip install numpy
[1 2 3 4 5]
<class 'numpy.ndarray'>
type(): This built-in Python function tells us the type of the object passed to it. Like in above code it shows that arr is numpy.ndarray type.
To create an ndarray, we can pass a list, tuple or any array-like object into the array() method, and it will be converted into an ndarray:
[1 2 3 4 5]
Dimensions in Arrays
A dimension in arrays is one level of array depth (nested arrays). nested array: are arrays that have arrays as their elements.
0-D Arrays
0-D arrays, or Scalars, are the elements in an array. Each value in an array is a 0-D array.
In [7]: 1 # Example
2
3 arr = np.array(42)
4 print(arr)
42
1-D Arrays
An array that has 0-D arrays as its elements is called uni-dimensional or 1-D array. These are the most common and basic arrays.
In [10]: 1 # Example
2
3 arr = np.array([1, 2, 3, 4, 5])
4 print(arr)
5
[1 2 3 4 5]
2-D Arrays
An array that has 1-D arrays as its elements is called a 2-D array.
In [12]: 1 # Example
2
3 arr = np.array([[1, 2, 3], [4, 5, 6]])
4 print(arr)
5
[[1 2 3]
[4 5 6]]
3-D arrays
An array that has 2-D arrays (matrices) as its elements is called 3-D array. These are often used to represent a 3rd order tensor.
In [13]: 1 # Example
2
3 arr = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])
4 print(arr)
5
[[[1 2 3]
[4 5 6]]
[[1 2 3]
[4 5 6]]]
In [14]: 1 # Example
2
3 a = np.array(42)
4 b = np.array([1, 2, 3, 4, 5])
5 c = np.array([[1, 2, 3], [4, 5, 6]])
6 d = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])
7
8 print(a.ndim)
9 print(b.ndim)
10 print(c.ndim)
11 print(d.ndim)
12
0
1
2
3
[[[[[1 2 3 4]]]]]
number of dimensions : 5
In [25]: 1 arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
2 print(arr[0, 1, 2])
6
Negative Indexing
Use negative indexing to access an array from the end.
In [27]: 1 #Example
2 arr = np.array([1, 2, 3, 4, 5, 6, 7])
3 print(arr[1:5])
[2 3 4 5]
[5 6 7]
Negative Slicing
Use the minus operator to refer to an index from the end:
In [29]: 1 #Example
2 #Slice from the index 3 from the end to index 1 from the end: import numpy as np
3 arr = np.array([1, 2, 3, 4, 5, 6, 7])
4 print(arr[-3:-1])
5
[5 6]
In [31]: 1 # Example
2 arr = np.array([1, 2, 3, 4, 5, 6, 7])
3 print(arr[1:5:2])
[2 4]
[1 3 5 7]
[7 8 9]
In [34]: 1 # From both elements, slice index 1 to index 4 (not included), this will return a 2-D array:
2
3 arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
4 print(arr[0:2, 1:4])
5
[[2 3 4]
[7 8 9]]
int32
[1 2 3]
int32
NumPy arrays have an attribute called shape that returns a tuple with each index having the number of corresponding elements.
In [43]: 1 #Example
2
3 arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
4 print(arr.shape)
(2, 4)
[[[[[1 1 3 4]]]]]
shape of array : (1, 1, 1, 1, 4)
By reshaping we can add or remove dimensions or change number of elements in each dimension
Convert the following 1-D array with 12 elements into a 2-D array. The outermost dimension will have 4 arrays, each with 3 elements:
In [46]: 1 arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
2 newarr = arr.reshape(4, 3)
3 print(newarr)
4
[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]
Convert the following 1-D array with 12 elements into a 3-D array.The outermost dimension will have 2 arrays that contains 3 arrays, each with 2
elements:
[[[ 1 2]
[ 3 4]
[ 5 6]]
[[ 7 8]
[ 9 10]
[11 12]]]
TRY BY YOURSELF
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])
newarr = arr.reshape(3, 3)
print(newarr)
Flattening the arrays
Flattening array means converting a multidimensional array into a 1D array. We can use reshape(-1) to do this.
Example
[1 2 3 4 5 6]
1
2
3
[1 2 3]
[4 5 6]
1
2
3
4
5
6
In [54]: 1 arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
2
3 for x in arr:
4 print(x)
[[1 2 3]
[4 5 6]]
[[ 7 8 9]
[10 11 12]]
In [55]: 1 for x in arr:
2 for y in x:
3 for z in y:
4 print(z)
1
2
3
4
5
6
7
8
9
10
11
12
In basic for loops, iterating through each scalar of an array we need to use n for loops which can be difficult to write for arrays with very high
dimensionality. Example
In [56]: 1 arr = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
2 for x in np.nditer(arr):
3 print(x)
4
1
2
3
4
5
6
7
8
Example
1
4
5
8
We pass a sequence of arrays that we want to join to the concatenate() function, along with the axis. If axis is not explicitly passed, it is taken as 0.
We pass a sequence of arrays that we want to join to the concatenate() function, along with the axis. If axis is not explicitly passed, it is taken as 0.
[1 2 3 4 5 6]
[[1 2 5 6]
[3 4 7 8]]
We can concatenate two 1-D arrays along the second axis which would result in putting them one over the other, ie. stacking.
[[1 4]
[2 5]
[3 6]]
[1 2 3 4 5 6]
[[1 2 3]
[4 5 6]]
[[[1 4]
[2 5]
[3 6]]]
[1 2]
[3 4]
[5 6]
In [70]: 1 arr = np.array([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12]])
2 newarr = np.array_split(arr, 3)
3 print(newarr)
[array([[1, 2],
[3, 4]]), array([[5, 6],
[7, 8]]), array([[ 9, 10],
[11, 12]])]
In [72]: 1 arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18]])
2 newarr = np.array_split(arr, 3, axis=1)
3 print(newarr)
4
[array([[ 1],
[ 4],
[ 7],
[10],
[13],
[16]]), array([[ 2],
[ 5],
[ 8],
[11],
[14],
[17]]), array([[ 3],
[ 6],
[ 9],
[12],
[15],
[18]])]
In [73]: 1 arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18]])
2 newarr = np.hsplit(arr, 3)
3 print(newarr)
[array([[ 1],
[ 4],
[ 7],
[10],
[13],
[16]]), array([[ 2],
[ 5],
[ 8],
[11],
[14],
[17]]), array([[ 3],
[ 6],
[ 9],
[12],
[15],
[18]])]
Search Sorted
There is a method called searchsorted() which performs a binary search in the array, and returns the index where the specified value would be
inserted to maintain the search order. The searchsorted() method is assumed to be used on sorted arrays.
[1 2 3]
[41 43]
[[3 5]
[2 0]
[4 1]]
Numpy Matrix
In [92]: 1 # make a matrix with numpy
2
3 g = np.matrix('[1, 2, 3; 4, 0, 6; 7, 8, -9]')
4
5 print(g)
[[ 1 2 3]
[ 4 0 6]
[ 7 8 -9]]
-9
Transpose of matrix :
[[3 5]
[2 0]
[4 1]]
Inverse of matrix :
In [66]: 1 A = np.array([[6, 1, 1],
2 [4, -2, 5],
3 [2, 8, 7]])
4 print("\nInverse of A:\n", np.linalg.inv(A))
Inverse of A:
[[ 0.17647059 -0.00326797 -0.02287582]
[ 0.05882353 -0.13071895 0.08496732]
[-0.11764706 0.1503268 0.05228758]]
Trace of Matrix :
[[ 6 1 1]
[ 4 -2 5]
[ 2 8 7]]
Trace of A: 11
Determinant of matrix:
In [73]: 1 n_array = np.array([[50, 29], [30, 44]])
2 # Displaying the Matrix
3 print("Numpy Matrix is:", n_array)
4
5 # calculating the determinant of matrix
6 det = np.linalg.det(n_array)
7
8 print("\nDeterminant of given 2X2 matrix:", int(det))
[-0.37228132 5.37228132]