Numpy Tutorial - Numpy Tutorial Python - Numpy Tutorial For Beginners | asingleneuron

numpy tutorial for beginners python, numpy tutorial, numpy tutorial python

Let's start our journey with numpy. NUMPY is a python library, capable of doing array processing with an ease.
Numpy is capable of performing any kind of array operation with a lightening speed. This is much needed tool to accomplish day to day task which requires multi-dimensional array.  Numpy is also useful for linear algebra. 

As a data scientist we daily deal with arrays :)

In this journey we will see useful function what makes you day to day task easy.

Prerequisite: you should be familiar with Python.






Load numpy library:

First thing first, load the numpy library by importing it.  

import numpy as np #  for arrays and linear algebra



1D Array or Vector:

we can create numpy array by using np.array. 

arr = np.array([1,2,3])
print(type(arr))
print(arr)
<class 'numpy.ndarray'>
[1 2 3]
Above example is creation of numpy array with the list. 
Class of numpy array is ndarray. 

print("Elemment type: ",arr.dtype)
Elemment type:  int64
dtype describes the type of elements in the array. For integers elements, int64 is default type. 


print("Shape of an array:",arr.shape)
Shape of an array: (3,)
arr.shape returns a tuple (# of rows, # of cols). In above example it returns a tuple of (3,) it means it has 3 rows and 1 column.


print("Size of an array:",arr.size)
Size of an array: 3
arr.size returns the total number of elements in the array. In above example we have only 3 elements.


2D Array or Matrix:

matrix = np.array([[1,2,3], [4,5,6]])
print(matrix)
[[1 2 3]
 [4 5 6]]

print("Element type:",matrix.dtype)
print("Number of dimension:",matrix.ndim)
print("Shape of matrix:",matrix.shape)
print("Size of matrix:",matrix.size)
Element type: int64
Number of dimension: 2
Shape of matrix: (2, 3)
Size of matrix: 6

Float / Char / String Matrix:

We can also create matrix or vector of different datatype like float, char and string.

f_matrix = np.array([[1.0,2.0], [3.0, 4.0]])
print(f_matrix.dtype)
float64
c_matrix = np.array([['a','b'], ['c', 'd']])
print(c_matrix.dtype.name)
str32
s_matrix = np.array([["hello", "Hi"], ["Google", "Facebook"]])
print(s_matrix.dtype.name)
str256



Mixed datatype:

Can we create a numpy array which contains mixed datatype ?

mix = [1, 2.0]
print(mix)
[1, 2.0]
Above list contains two elements one is integer and another is float. 
In python, a list can have any object. Let's try to create a numpy array with the above list.

print(np.array(mix))
[1. 2.]
All elements of a numpy array must have same datatype.
So when numpy founds one element integer and another float. It converts integer into float and creates an array.


mix = [1, 2.0, '3']
print(mix)
print(np.array(mix))
[1, 2.0, '3']
['1' '2.0' '3']
In this example, we have three different datatype mixed together.
So numpy converts integer and float into string and creates an array.



Smart ways to create ndarray:

np.ones:

np.ones : np.ones function creates an array of given shape and fills all the element with 1.

only_ones = np.ones((2,3))
print(only_ones)
print(only_ones.dtype.name)
[[1. 1. 1.]
 [1. 1. 1.]]
float64
In the above example, we have created an 2D array which contains 2 rows and 3 columns  filled with 1. Default dtype for np.ones is float64.


only_ones_of_integer = np.ones((2,3), dtype=np.int16)
print(only_ones_of_integer)
print(only_ones_of_integer.dtype.name)
[[1 1 1]
 [1 1 1]]
int16
specify datatype : we can specify the datatype of numpy array by using dtype. In above example, we have specified the datatype int16.


np.zeros:

np.zeros : np.zeros function creates an array of given shape and fills all the element with 0.

only_zeros = np.zeros((2,3))
print(only_zeros)
[[0. 0. 0.]
 [0. 0. 0.]]
In above example, we created a 2D array (2rows, 3colums) filled with zeros.



np.random:

np.random.random : np.random.random function creates an array of given shape and fills all the element with random values from [0, 1].

random_arr = np.random.random((2,3))
print(random_arr)
[[0.88313848 0.49060132 0.2518497 ]
 [0.59298466 0.8202635  0.19777593]]



np.arange:

np.arange: np.arange generates a sequence of numbers and returns a single dimensional array. It takes stepsize to generate next number.

seq = np.arange(5)
print(seq)
[0 1 2 3 4]
np.arange generates the values including start and excluding stop by using step to generate next number.
By default start is 0, step is 1.
So for np.arange(5) , it generated numbers by including 0 and excluding 5 by adding 1 as a step.

np.arange(10,50,5)

array([10, 15, 20, 25, 30, 35, 40, 45])
In the above example, start is 10 and end is 50 and step is 5.
np.arange generated an array starting from 10 , excluding 50 by incrementing 5.


Can we generate numbers from 5 to 1 by using np.arange ?

np.arange(5,0,-1)

array([5, 4, 3, 2, 1])
In the above example, we are trying to generate numbers in reverse order from 5 to 1.
To achive this, we have given start as 5, end as 0 and step is -1. So after generating 5 , it will add the step to get the next number. But here we are using the -1, so 5 -1 = 4.
One more thing to notice here is we use 0 as an end, because np.arange excludes the end.


np.linspace:

np.linespace: np.linespace generates evenly spaced numbers over a specified interval.

seq = np.linspace(0,10)

print("Number of elements:",seq.size)
print()
print(seq)
Number of elements: 50

[ 0.          0.20408163  0.40816327  0.6122449   0.81632653  1.02040816
  1.2244898   1.42857143  1.63265306  1.83673469  2.04081633  2.24489796
  2.44897959  2.65306122  2.85714286  3.06122449  3.26530612  3.46938776
  3.67346939  3.87755102  4.08163265  4.28571429  4.48979592  4.69387755
  4.89795918  5.10204082  5.30612245  5.51020408  5.71428571  5.91836735
  6.12244898  6.32653061  6.53061224  6.73469388  6.93877551  7.14285714
  7.34693878  7.55102041  7.75510204  7.95918367  8.16326531  8.36734694
  8.57142857  8.7755102   8.97959184  9.18367347  9.3877551   9.59183673
  9.79591837 10.        ]
np.linspace generates evenly spaced numbers over specified Start and End as input.
Number of elements to be generated is 50 by default.


start = 5
end = 10
num_of_elements = 10
np.linspace(start, end, num_of_elements)

array([ 5.        ,  5.55555556,  6.11111111,  6.66666667,  7.22222222,
        7.77777778,  8.33333333,  8.88888889,  9.44444444, 10.        ])
In above example, we are trying to generate 10 evenly spaced numbers from 5 to 10 .



Play with the SHAPE of array:

In this section we will see the tricks of array re-shaping.
For example: convert 1D array to 2D array or convert 2D array to 1D.

vector = np.random.random((10,1))
print(vector.shape)
print(vector)
(10, 1)
[[0.68495229]
 [0.30307281]
 [0.24237511]
 [0.49922158]
 [0.0077114 ]
 [0.21583584]
 [0.15053899]
 [0.272561  ]
 [0.47318984]
 [0.35029513]]
vector is a random 1D array, that has a shape of 10 rows and 1 column.



matrix = vector.reshape(2,5)
print(matrix.shape)
(2, 5)
Re-shaped vector into matrix by using reshape() function. Specifying the new shape of 2 rows and 5 columns.

print(matrix)
[[0.68495229 0.30307281 0.24237511 0.49922158 0.0077114 ]
 [0.21583584 0.15053899 0.272561   0.47318984 0.35029513]]


matrix = vector.reshape(2,-1)
print(matrix.shape)
(2, 5)
reshape() function also works with -1, we can pass -1 if we are not sure about that dimension.

For exammple : if we are not sure about how many columns, than we can pass -1.

           So reshape(2,-1) convert vector into matrix that has 2 rows, reshape() equally divides the number of elements into 2 chunks and figures out the number of columns.

print(matrix)
[[0.68495229 0.30307281 0.24237511 0.49922158 0.0077114 ]
 [0.21583584 0.15053899 0.272561   0.47318984 0.35029513]]



print(matrix.reshape(10,1))
[[0.68495229]
 [0.30307281]
 [0.24237511]
 [0.49922158]
 [0.0077114 ]
 [0.21583584]
 [0.15053899]
 [0.272561  ]
 [0.47318984]
 [0.35029513]]
With same trick we can convert matrix(2D array) to vector(1D array).

As we know, matrix has 2 rows and 5 columns, so 2 * 5 = 10 , passing 10 rows and 1 columns as an input to reshape() function.

print(matrix.reshape(-1,1))
[[0.68495229]
 [0.30307281]
 [0.24237511]
 [0.49922158]
 [0.0077114 ]
 [0.21583584]
 [0.15053899]
 [0.272561  ]
 [0.47318984]
 [0.35029513]]
We can also use the -1 trick to convert multi-dimensional to 1D array.
We know that 1D array has only 1 column, we can fix that and pass rows as -1.


flatten_array = matrix.reshape(-1)
print(flatten_array.shape)
(10,)


Another way to flatten the multi-dimensional array is reshape(-1).

print(flatten_array)
[0.68495229 0.30307281 0.24237511 0.49922158 0.0077114  0.21583584
 0.15053899 0.272561   0.47318984 0.35029513]




Array indexing Tricks:

Just a reminder , array index starts from 0 (zero).

arr = np.arange(1,11)
arr

array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10])
print("Access the first element of an array :",arr[0])
print("Access the fifth element of an array :",arr[4])
print()
print("Access the last element of an array: ",arr[-1])
print("Access the second element of an array: ",arr[-2])
Access the first element of an array : 1
Access the fifth element of an array : 5

Access the last element of an array:  10
Access the second element of an array:  9
First element start with index 0, second with 1 and so on.
-ve index starts from the last element by starting -1, -2 and so on.



start_index = 3
end_index = 7

print(arr[ start_index : end_index])
[4 5 6 7]
arr[3:7] is a way of selecting sub part of an array.
We need to specify the start and end (excluding) index. By default index steps by 1.


start_index = 0
end_index = 10
step_by = 2
print(arr[ start_index : end_index: step_by])
[1 3 5 7 9]
In above example we are finding the subset of array by passing starting index 0 and end index 10 and by stepping 2 index. So index will be 0, 2, 4, 6, 8.

matrix = np.arange(1,17).reshape(4,4)
print(matrix)
[[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]
 [13 14 15 16]]
print("Element at first row and first column of matrix:",matrix[0,0])
print("Element at last row and last column of matrix:",matrix[3,3])
Element at first row and first column of matrix: 1
Element at last row and last column of matrix: 16

print("Second row of the matrix:")
matrix[1]
Second row of the matrix:

array([5, 6, 7, 8])
print("First to third rows of the matrix:")
matrix[0:3]
First to third rows of the matrix:

array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12]])

print("Second column of the matrix:")
matrix[:,1]
Second column of the matrix:

array([ 2,  6, 10, 14])
print("First to Third columns of the matrix:")
matrix[:,0:3]
First to Third columns of the matrix:

array([[ 1,  2,  3],
       [ 5,  6,  7],
       [ 9, 10, 11],
       [13, 14, 15]])
print("Find elements of second and third column and second and third row of the matrix")
matrix[1:3, 1:3]
Find elements of second and third column and second and third row of the matrix

array([[ 6,  7],
       [10, 11]])


Boolean indexing or Masking:

matrix

array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12],
       [13, 14, 15, 16]])
mask = matrix > 10
mask

array([[False, False, False, False],
       [False, False, False, False],
       [False, False,  True,  True],
       [ True,  True,  True,  True]])
In above operation, we are executing a logical condition which returns True if element of matrix is greater than 10 else False. This condition will be executed for all the array elements. 
As a result , we got a boolean array contains True / False. 


matrix[mask]

array([11, 12, 13, 14, 15, 16])
matrix[mask] is the way of boolean indexing. It returns a sub-array of elements which satisfies the condition. So if mask array has True than the element of that index will be picked, else will drop from the resulting array.

One liner to achieve the same results:

matrix[matrix > 10]

array([11, 12, 13, 14, 15, 16])


Another Example:
        Boolean indexing to find the sub-array of all the even numbers.
even_element_mask = matrix % 2 == 0
even_element_mask

array([[False,  True, False,  True],
       [False,  True, False,  True],
       [False,  True, False,  True],
       [False,  True, False,  True]])
matrix[even_element_mask]

array([ 2,  4,  6,  8, 10, 12, 14, 16])




Matrix Operations:

1. Transpose:

Matrix transpose is changing rows to columns and columns to rows.

matrix = np.arange(1,11).reshape(2,-1)
matrix

array([[ 1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10]])
transposed_matrix = matrix.T
transposed_matrix

array([[ 1,  6],
       [ 2,  7],
       [ 3,  8],
       [ 4,  9],
       [ 5, 10]])

print("Matrix:",matrix.shape)
print("Transposed matrix: ",transposed_matrix.shape)
Matrix: (2, 5)
Transposed matrix:  (5, 2)

As we can see that rows of matrix becomes columns and columns becomes rows after applying transpose.


2. Element wise operations:

Element wise operations are those operations that gets applied to every element of the array. 

matrix + 10

array([[11, 12, 13, 14, 15],
       [16, 17, 18, 19, 20]])
matrix - 10

array([[-9, -8, -7, -6, -5],
       [-4, -3, -2, -1,  0]])
matrix * matrix

array([[  1,   4,   9,  16,  25],
       [ 36,  49,  64,  81, 100]])
All of the above examples are element-wise operations. 

3. Dot product:

Matrices should satisfies the following condition:
1.  Number of columns in the 1st matrix must be equal to the number of rows in 2nd matrix.

2. Dot product of M x N matrix with N x K matrix results M x K matrix. 

print("Matrix:",matrix.shape)
print("Transposed matrix: ",transposed_matrix.shape)

matrix.dot(transposed_matrix)
Matrix: (2, 5)
Transposed matrix:  (5, 2)

array([[ 55, 130],
       [130, 330]])


4. inverse of matrix:

dummy= np.array([[1,2],[3,4]])
dummy

array([[1, 2],
       [3, 4]])
np.linalg.inv(dummy)

array([[-2. ,  1. ],
       [ 1.5, -0.5]])
We can find the inverse of the Matrix by using np.linalg.inv(). 



Stacking and Splitting: 


1. Horizontal Stacking:

Stacks array horizontally (column wise). 
h_stacked = np.hstack([matrix, matrix])
h_stacked

array([[ 1,  2,  3,  4,  5,  1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10,  6,  7,  8,  9, 10]])
In the above example, we stacked two matrix column wise. As a results we can see increased #of columns.


2. Vertical Stacking:

Stacks array vertically ( row wise).

v_stacked = np.vstack([matrix, matrix])
v_stacked

array([[ 1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10],
       [ 1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10]])
In the above example , we stacked two matrix row wise. As a result we can see increased #of rows.


3. Horizontal Splitting:

Splits array into multiple sub-arrays column wise (horizontally). We also need to specify the number of splits. 

arr1, arr2 = np.hsplit(h_stacked, 2)
print("Array1:")
print(arr1)

print("\nArray2:")
print(arr2)
Array1:
[[ 1  2  3  4  5]
 [ 6  7  8  9 10]]

Array2:
[[ 1  2  3  4  5]
 [ 6  7  8  9 10]]
In the above example, we are splitting array column-wise into 2 sub-arrays. 


4. Vertical Splitting:

Splits array into multiple sub-array row-wise (vertically). We also need to specify number of splits.

arr1, arr2 = np.vsplit(v_stacked, 2)
print("Array1:")
print(arr1)

print("\nArray2:")
print(arr2)
Array1:
[[ 1  2  3  4  5]
 [ 6  7  8  9 10]]

Array2:
[[ 1  2  3  4  5]
 [ 6  7  8  9 10]]
In above example, we are splitting an array into 2 sub-arrays row-wise. 



Query ?

arr = np.array([[1,1,0,0],[1,1,0,0],[1,1,0,0],[1,1,0,0] ])
print(arr)
[[1 1 0 0]
 [1 1 0 0]
 [1 1 0 0]
 [1 1 0 0]]

1. np.all :

np.all() test if all of the elements of any array along with given axis evaluates to True returns True, else False. It like logical AND operation. 
By default axis is None, it will evaluate the condition on all the elements of the array. 

np.all(arr==0)

False
In the above example , np.all() evaluates the condition arr==0 on all the elements of the array. As a result it returns False because not all the elements satisfies the condition. 


np.all(arr==0, axis=0)

array([False, False,  True,  True])
Now we have specified the axis=0 (columns wise) along with the condition. As a result we got False for columns 1 and 2 and True for column 3 and 4 because all the elements of column 3 and 4 has zero. 


np.all(arr==0, axis=1)

array([False, False, False, False])
Same query but now the axis=1 (row wise). 


2. np.any :

np.any() test if any of the array elements of any array along with given axis evaluates to True returns True, else False. It like logical OR operation.
By default axis is None, it will evaluate the condition on all the elements of the array. 

np.any(arr==0)

True


np.any(arr==0, axis=0)

array([False, False,  True,  True])


np.any(arr==0, axis=1)

array([ True,  True,  True,  True])


3. np.nonzero :

np.nonzero() retuns the number of indices of the elements that are non-zero. Returns two arrays, one for row indices another for column indices.

We can also specify the condition. 


row_index, col_index = np.nonzero(arr)
print(row_index, col_index)
[0 0 1 1 2 2 3 3] [0 1 0 1 0 1 0 1]
In above example, we have passed the array arr to np.nonzero(). It will check if an element is non-zero or not. If yes, returns the row and column indices of all the elements who satisfies.
Try to print the elements of returned indices to verify. 

for r, c in zip(row_index, col_index):
    print("arr[{0}][{1}] = {2}".format(r,c, arr[r][c]))
arr[0][0] = 1
arr[0][1] = 1
arr[1][0] = 1
arr[1][1] = 1
arr[2][0] = 1
arr[2][1] = 1
arr[3][0] = 1
arr[3][1] = 1


We can also pass any condition to np.nonzero(). Try to find all the zero elements. 

row_index, col_index = np.nonzero(arr==0)
print(row_index, col_index)
[0 0 1 1 2 2 3 3] [2 3 2 3 2 3 2 3]

for r, c in zip(row_index, col_index):
    print("arr[{0}][{1}] = {2}".format(r,c, arr[r][c]))
arr[0][2] = 0
arr[0][3] = 0
arr[1][2] = 0
arr[1][3] = 0
arr[2][2] = 0
arr[2][3] = 0
arr[3][2] = 0
arr[3][3] = 0


4. np.where:

np.where() returns elements choosen from X or Y after evaluating the given condition.  If X or Y is not given results will be same as np.nonzero().


np.where(arr==0)

(array([0, 0, 1, 1, 2, 2, 3, 3]), array([2, 3, 2, 3, 2, 3, 2, 3]))
In the above example we want to find the indices of all the elements which has a value zero. Above results are same as np.nonzero()


Let's try to use X or Y values. 

np.where(arr==0, "zero", "non_zero")

array([['non_zero', 'non_zero', 'zero', 'zero'],
       ['non_zero', 'non_zero', 'zero', 'zero'],
       ['non_zero', 'non_zero', 'zero', 'zero'],
       ['non_zero', 'non_zero', 'zero', 'zero']], dtype='<U8')
Now are using "zero" and "non_zero" conditional values along with the condition. As a result we got a new array filled with specified values. 
Values X ("zero") will be chosen for those elements which satisfies the condition , else value Y ("non-zero") will. 

Not only scaler values, we can pass arrays also in place of X or Y. In that case element will be chosen from the same index. 

np.where(arr==0, arr, -1)

array([[-1, -1,  0,  0],
       [-1, -1,  0,  0],
       [-1, -1,  0,  0],
       [-1, -1,  0,  0]])


Ordering:

data =[[11, 0, 3, 4], [34, 5, 1, 9], [-9, 5, 3, 6]]
X = np.array(data)
X

array([[11,  0,  3,  4],
       [34,  5,  1,  9],
       [-9,  5,  3,  6]])


1. max:

**np.max()** returns the element of the array that has the maximum value. We can also specify the axis.

X.max() # maximum element of the array

34
X.max(axis=0)  # Column-wise maximum elements 

array([34,  5,  3,  9])
X.max(axis=1) # Row-wise maximum elements

array([11, 34,  6])


2. min:

**np.min()** returns the element of the array that has the minimum value. We can also specify the axis.

X.min() # miniumn element of the array

-9
X.min(axis=0) # Column-wise minimum elements of the array

array([-9,  0,  1,  4])
X.min(axis=1) # Row-wise minimum elements of the array

array([ 0,  1, -9])

3. np.sort:

np.sort() returns the sorted array in ascending order. We can perform the sorting along with axis also.

X

array([[11,  0,  3,  4],
       [34,  5,  1,  9],
       [-9,  5,  3,  6]])
np.sort(X) # sort array elements in ascending order. By default it's sort the array row-wise

array([[ 0,  3,  4, 11],
       [ 1,  5,  9, 34],
       [-9,  3,  5,  6]])

Trick to sort the elements in descending order. 

-np.sort(-X) # sort array element in descending order.

array([[11,  4,  3,  0],
       [34,  9,  5,  1],
       [ 6,  5,  3, -9]])
Trick is first make all positive elements negative and all negative elements positive. np.sort() will sort all elements in ascending order. Finally reverse the sign of the sorted array. 


np.sort(X, axis=0) # sort elements column-wise

array([[-9,  0,  1,  4],
       [11,  5,  3,  6],
       [34,  5,  3,  9]])
np.sort(X, axis=1) #Sort elements row-wise

array([[ 0,  3,  4, 11],
       [ 1,  5,  9, 34],
       [-9,  3,  5,  6]])


4. np.argmax:

np.argmax() returns the index of the maximum element.

X

array([[11,  0,  3,  4],
       [34,  5,  1,  9],
       [-9,  5,  3,  6]])
X.reshape(-1) # Flattened array

array([11,  0,  3,  4, 34,  5,  1,  9, -9,  5,  3,  6])
np.argmax(X) # returns the index of the maximum element of the array. If axis is not given , it flattens the array first and finds the index. 

4
np.argmax(X, axis=0) # indices of the maximum elements of the array column-wise

array([1, 1, 0, 1])
np.argmax(X, axis=1) # indices of the maximum elements of the array row-wise

array([0, 0, 3])

5. np.argmin:

np.argmin() returns the index of the minimum elememt.

X

array([[11,  0,  3,  4],
       [34,  5,  1,  9],
       [-9,  5,  3,  6]])
X.reshape(-1) # Flattened array

array([11,  0,  3,  4, 34,  5,  1,  9, -9,  5,  3,  6])
np.argmin(X) # returns the index of the minimum element of the array. If axis is not given , it flattens the array first and finds the index. 

8

np.argmin(X, axis=0) # returns the indices of the minimum elements of the array column-wise

array([2, 0, 1, 0])
np.argmin(X, axis=1) # returns the indices of the minimum elements of the array row-wise

array([1, 2, 0])

6. np.argsort:

np.argsort() returns the array of indices, after sorting it by ascending order.

X

array([[11,  0,  3,  4],
       [34,  5,  1,  9],
       [-9,  5,  3,  6]])
np.argsort(X) # returns the array of indices after sorting the elements in ascending order. 

array([[1, 2, 3, 0],
       [2, 1, 3, 0], 
[0, 2, 1, 3]]) 


Statistics :

X = np.arange(1, 11).reshape(5,2)
X

array([[ 1,  2],
       [ 3,  4],
       [ 5,  6],
       [ 7,  8],
       [ 9, 10]])

1. mean: 

np.mean() computes the mean/average along with the specified axis. By default for the flattened array.
Formula :
numpy mean formula, numpy mean, np.mean, numpy.mean

np.mean(X) # returns mean of the elements on flattened array

5.5
np.mean(X, axis=0) # returns the mean of the elements column-wise

array([5., 6.])
np.mean(X, axis=1) # returns the mean of the elements row-wise

array([1.5, 3.5, 5.5, 7.5, 9.5])


2. median:

np.median() computes the median along with the specified axis. By default for the flattened array.

To find median, sort the array in ascending order first. Then return the middle element if array has odd number of elements. In case of even number of elements, return the average of two middle elements.

np.median(X) # returns median of the elements on flattened array

5.5
np.median(X, axis=0) # returns the median of the elements column-wise

array([5., 6.])
np.median(X, axis=1) # returns the median of the elements row-wise

array([1.5, 3.5, 5.5, 7.5, 9.5])

3. variance:

np.var() computes the variance along with the specified axis. By default if axis is not given, variance is calculated for the flattened array.

Formula:
numpy variance , numpy variance formula
np.var(X) # returns variance of the elements on flattened array

8.25
np.var(X, axis=0) # returns the variance of the elements column-wise

array([8., 8.])
np.var(X, axis=1) # returns the variance of the elements row-wise

array([0.25, 0.25, 0.25, 0.25, 0.25])

4. standard-deviation:

np.std() computes the stardard deviation along with the specified axis. By default if axis is not given, standard deviation is calculated for the flattened array.

Formula:
numpy standard deviation formula
np.std(X) # returns standard-deviation of the elements on flattened array

2.8722813232690143
np.std(X, axis=0) # returns standard-deviation of the elements column-wise

array([2.82842712, 2.82842712])
np.std(X, axis=1) # returns standard-deviation of the elements row-wise

array([0.5, 0.5, 0.5, 0.5, 0.5])



You can download the code and make you hands dirty by using kaggle kernel .











Comments

Popular posts from this blog

Ericsson ML Challenge Winning Solution | asingleneuron

HDFC Bank Machine Learning Challenge Solution