Hi and welcome
This is the 3rd post of my series on making sense of the NumPy array. If this is your first read on this series , then i will advice you go through part II and Part I of this series to make complete sense of this , however if you're already familiar with NumPy , you can just continue the journey. This post will be a short one , and it is going to be based on two main areas of study under the NumPy library, Statistics and Linear algebra.
NumPy is reach in functions used for statistical computations. In this section , i will be introducing us to most of them , coupled with different ways of generating arrays using numpy methods.
1 import numpy as np 2 x=np.random.randint(2,20,7) #randomly generates 7 integers between 2 and 20. Note that your result may be different from this 3 #x=[2,3,16,7,13,12,14] is the result of the random selection 4 print(x.sum()) # Returns the sum of the digits 5 print(x.mean()) # Gives the arithmetic mean of all items of x 6 print(x.std()) # Returns the population standard deviation of the array 7 print(x.var()) #Returns the population variance of the array 8 print(x.min()) #Returns the minimum value of the array 9 print(x.max()) #Returns the maximum value of the array 10 print(x.argmax()) #Returns the index of the maximum value of the array 11 print(x.argmin()) #Returns the index of the minimum value of the array
This methods can also work for multi- dimensional arrays. In such case , the array is flattened , then the usual operation is performed. This is illustrated below.
1 y=np.array([[3,5],[7,4]]) 2 print(y.sum())# returns the sum of the flat array of y 3 print(y.mean()) # returns the mean of the flat array of y 4 print(y.max()) # returns the maximum of the flat array of y 5 print(y.min())# returns the minimum of the flat array of y #This same operation is applicable to all other methods.
Linear algebra is one area of mathematics that is strongly applied in the field of data science/machine learning. In this section , i will introduce you to operations based on linear algebra.
Let A and B be two matrices , then the following operations are can be performed on A and B.
1 A=np.array([[4,5],[1,0]]) 2 B=np.array([[1,2],[-1,1]]) 3 print(A+B) # Returns the sum of the two matrices, element-wise 4 print(A*B) # Returns element-wise multiplication of the matrices 5 print(A@B) # Returns the matrix product of the two matrices 6 print(A.dot(B)) # Returns the matrix product of the two matrices(same as line 5) 7 print(A.T)# Return the transpose of matrix , i.e each row becomes a column , vice versa 8 print(A.transpose())# Return the transpose of matrix , i.e each row becomes a column , vice versa 9 print(A.trace())# Returns the trace of the matrix, i.e the sum of the elements in the leading diagonal
To obtain the determinant and the inverse of a matrix , we need to use a sub module(.linalg), which contains the method required for these operations and more. This is as shown below
1 print(np.linalg.det(A)) # This returns the determinant of the matrix A 2 print(np.linalg.inv(A)) # This returns the inverse of the matrix #The inverse will produce an error if the determinant of the matrix is zero, i.e if the matrix is a singular matrix.
I believe you're enjoying NumPy? We have being learning the tiny piece of NumPy, which forms the foundation for bigger and fantastic projects. Every machine Learning model in the machine Learning Library Scikit-learn is based on this library. In parts to come , we shall hopefully build one or two of this model from scratch. See you in the next post. Cheers!!