Summary of NUMPY - Explained with examples
NUMPY is a Python package. It stands for 'Numerical Python'. It is a library consisting of multidimensional array objects and a collection of routines for processing of array.
Why Numpy is used when we have lists?
- It's because , Numpy is faster than compare to list etc.
- It takes less storage comparative to other.
- It is convinent to use.
So, we will see overall Numpy summary with example.
creating 1D array ([ ])¶
import numpy as np
my_arr = np.array([2,1,3,4,5], np.int64)
my_arr[1]
1
creating 2D array ([ [ ] ])¶
my_arr = np.array([[1,2,3,4,5]], np.int64)
my_arr[0,2]
3
my_arr = np.array([[1,2,3,4,5]], np.int64)
my_arr.shape
(1, 5)
Data types¶
my_arr = np.array([[1,2,3,4,5]], np.int64)
my_arr.dtype
dtype('int64')
In this it means integer of 64 bits¶
Replacing array¶
my_arr = np.array([[1,2,3,4,5]])
my_arr[0,1] = 50
my_arr
array([[ 1, 50, 3, 4, 5]])
element of 0th row and 1st column replaced by 50¶
Reshaping an array¶
my_arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
newarr = my_arr.reshape(4, 3)
newarr
array([[ 1, 2, 3], [ 4, 5, 6], [ 7, 8, 9], [10, 11, 12]])
Iterating 2-D Arrays¶
my_arr = np.array([[1, 2, 3], [4, 5, 6]])
for x in my_arr:
print(x)
[1 2 3] [4 5 6]
NumPy Joining Array¶
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
arr = np.concatenate((arr1, arr2))
print(arr)
[1 2 3 4 5 6]
creating arrays¶
1. Intuitive approach: Using numpy.array()¶
import numpy as np
numpy_arr = np.array([5,4,2,6,1,3])
numpy_arr
array([5, 4, 2, 6, 1, 3])
numpy_arr = np.arange(0,11)
numpy_arr
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
Created an array in the range on 0-10¶
OR also can be created increasing with a step of 2¶
numpy_arr = np.arange(0,11,2)
numpy_arr
array([ 0, 2, 4, 6, 8, 10])
3. using linspace() function¶
numpy_arr = np.linspace(1,5,12)
numpy_arr
array([1. , 1.36363636, 1.72727273, 2.09090909, 2.45454545, 2.81818182, 3.18181818, 3.54545455, 3.90909091, 4.27272727, 4.63636364, 5. ])
3. using linspace() function¶
numpy_arr = np.linspace(1,5,10)
numpy_arr
array([1. , 1.44444444, 1.88888889, 2.33333333, 2.77777778, 3.22222222, 3.66666667, 4.11111111, 4.55555556, 5. ])
4. Using empty() function¶
Gives an array of 4 rows and 6 columns with any random values¶
numpy_arr = np.empty((4,6))
numpy_arr
array([[2.48273508e-312, 2.37663529e-312, 2.01589600e-312, 2.41907520e-312, 6.79038654e-313, 6.79038653e-313], [2.37663529e-312, 2.05833592e-312, 2.41907520e-312, 2.56761491e-312, 1.93101617e-312, 9.33678148e-313], [9.33678148e-313, 9.33678148e-313, 9.33678148e-313, 9.33678148e-313, 1.97345609e-312, 2.12199579e-313], [2.48273508e-312, 2.37663529e-312, 2.01589600e-312, 2.41907520e-312, 8.45610230e-307, 2.18568966e-312]])
5. Special Numpy Array using ones(), zeros(), identity() etc functions¶
i. Zeros¶
zeros = np.zeros((2,5))
zeros
array([[0., 0., 0., 0., 0.], [0., 0., 0., 0., 0.]])
Creates an array with 2 rows and 5 columns and fills that with 0s¶
ii. ones¶
Ones = np.ones((2,5))
Ones
array([[1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.]])
Creates an array with 2 rows and 5 columns and fills that with 0s¶
iii. identity¶
ide = np.identity(45)
ide
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.]])
Gives an identity matrix of 45x45¶
eye = np.eye(5)
eye
array([[1., 0., 0., 0., 0.], [0., 1., 0., 0., 0.], [0., 0., 1., 0., 0.], [0., 0., 0., 1., 0.], [0., 0., 0., 0., 1.]])
The eye tool returns a 2-D array with 1’s as the diagonal and 0’s elsewhere¶
Numpy axis¶
created an 2D array
x = [[1,2,3], [4,5,6], [7,1,0]]
arr = np.array(x)
arr
array([[1, 2, 3], [4, 5, 6], [7, 1, 0]])
arr.sum(axis = 0)
array([12, 8, 9])
It adds the sum of all the elements row wise and adds a new column to it
x1 = [ [1,2,3 ,6], [4,5,6, 15], [7,1,0,8]]
arr1 = np.array(x1)
arr1
array([[ 1, 2, 3, 6], [ 4, 5, 6, 15], [ 7, 1, 0, 8]])
Attributes & functions in Numpy¶
import numpy as np
x = [[1,2,3], [4,5,6], [7,1,0]]
arr = np.array(x)
arr
array([[1, 2, 3], [4, 5, 6], [7, 1, 0]])
we are going to use this array (arr) for examples
arr.T¶
arr.T
array([[1, 4, 7], [2, 5, 1], [3, 6, 0]])
It Transposes the matrix i.e row becomes column and column as row
arr.flat¶
arr.flat
<numpy.flatiter at 0x2db83fb8dd0>
for items in arr.flat:
print(items)
1 2 3 4 5 6 7 1 0
It is used as a 1D iterator over N-dimensional arrays.
arr.ndim¶
arr.ndim
2
gives number of dimension of the array
arr.size¶
arr.size
9
gives number of elements in array
arr.nbytes¶
arr.nbytes
36
total number of bytes consumed by array
arr.argmax()¶
arr.argmax()
6
Gives the index where the max value is. Here at index 6 there is value 7.
arr.argmax(axis=0)
array([2, 1, 1], dtype=int64)
with axis 0 : Gives index of max value columnwise.
arr.argmax(axis=1)
array([2, 2, 0], dtype=int64)
with axis 1 : Gives index of min value row wise.
arr.argmin()¶
arr.argmin()
8
Gives the index where the min value is. Here at index 8 there is value 0.
arr.argmin(axis=0)
array([0, 2, 2], dtype=int64)
with axis 0 : Gives index of min value column wise.
arr.argmin(axis=1)
array([0, 0, 2], dtype=int64)
with axis 1 : Gives index of min value row wise.
arr.sort()¶
arr.argsort()
array([[0, 1, 2], [0, 1, 2], [2, 1, 0]], dtype=int64)
It shows the indices(indicate the position of the element for the sorted array) and dtype.
arr.argsort(axis=0)
array([[0, 2, 2], [1, 0, 0], [2, 1, 1]], dtype=int64)
with axis 0 : Gives index column wise with sorted elements.
arr.argsort(axis=1)
array([[0, 1, 2], [0, 1, 2], [2, 1, 0]], dtype=int64)
with axis 1 : Gives index row wise with sorted elements.
arr.ravel¶
arr.ravel()
array([1, 2, 3, 4, 5, 6, 7, 1, 0])
s used to change a 2-dimensional array or a multi-dimensional array into a contiguous flattened array
Mathematical operators in Numpy array¶
import numpy as np
arr1 = np.array([[1,2,3],
[4,5,6],
[7,1,0]])
arr1
array([[1, 2, 3], [4, 5, 6], [7, 1, 0]])
arr2 = np.array([[1,2,1],
[4,0,6],
[8,1,0]])
arr2
array([[1, 2, 1], [4, 0, 6], [8, 1, 0]])
we have created this two arrays arr1 and arr2. we will use this for eg.
addition of two arrays¶
arr1 + arr2
array([[ 2, 4, 4], [ 8, 5, 12], [15, 2, 0]])
subtracting of two arrays¶
arr1 - arr2
array([[ 0, 0, 2], [ 0, 5, 0], [-1, 0, 0]])
multiplication of two arrays¶
arr1 * arr2
array([[ 1, 4, 3], [16, 0, 36], [56, 1, 0]])
finding square root of an array¶
np.sqrt(arr1)
array([[1. , 1.41421356, 1.73205081], [2. , 2.23606798, 2.44948974], [2.64575131, 1. , 0. ]])
arr1.sum()¶
arr1.sum()
29
sums all the elements of array
arr1.max()¶
arr1.max()
7
Gives max value from arr1
arr1.min()¶
arr1.min()
0
gives min value from arr1
count_nonzero(array)¶
np.count_nonzero(arr1)
8
gives the count of non-zero elements in an array
tolist()¶
arr1.tolist()
[[1, 2, 3], [4, 5, 6], [7, 1, 0]]
converts array to a list
where() condition¶
b = np.where(arr1 > 5)
b
(array([1, 2], dtype=int64), array([2, 0], dtype=int64))
Return elements chosen from x or y depending on condition
arr1[b]
array([6, 7])
here, arr1[b] gives the elements from the array (arr1) greater than 5
Squaring an array¶
np.square(arr1)
array([[ 1, 4, 9], [16, 25, 36], [49, 1, 0]], dtype=int32)
logical AND¶
np.logical_and(arr1, arr2)
array([[ True, True, True], [ True, False, True], [ True, True, False]])
logical OR¶
np.logical_or(arr1, arr2)
array([[ True, True, True], [ True, True, True], [ True, True, False]])