Monday, April 19, 2021

Using Vectorisation Effectively in Python and R – a revision example

 


    ========================================================

     Using Vectorisation Effectively in Python and R – instance example

    ========================================================


·        While performing Matrix Operations, such as Vector Multiplication , its very hard to consider that the computer does all the forms of hard work .  What does one need to do while working on numbers in a Vectorised form?

 ·        So , while one is working upon numbers , one needs to put the numbers in the form of matrices , vectors and constants and then one can sum , subtract , divide and then multiply the numbers .

 ·        These operations can be performed upon any number which is in the form of Vectors .

 ·        So , when one deals with these vectorised form of numbers in Machine Learning , then one should also have a good ( good is a relative value that lies in between novice and expert ) understanding and know how on how to feed the vectorised data to the algorithm over which the encoded function is going to work upon , which would help the programmer to know and understand the results obtained after the processing of the data is done .

 ·        In order to obtain a correct result , one may need to feed the right data to the right algorihtm .

 ·        In Python, Numpy package offers all the functionality needed to create and manipulate the matrices . The "ndarray" objects allow fast creation of an array , such as a multidimensional matrix by starting with data which are queued into the lists

 ·        The term "ndarray" means "n-dimensional array" which implies that one can create arrays of multiple dimensions like one-dimensional array , 2 dimensional array , 3 dimensional array and so on .

 

 

=========================================

Using a simple Vector for creation of a Numpy List

=========================================

 

import numpy as np

y = np.array([43, 45,47,49,51])

print(y)

print(y.shape)

 

Output

[43,45,47,49,51]

(5,)

 

==================================

==================================

 

* The method "shape" can promptly inform someone about the shape of a matrix . In the above given example , one can see that the shape of the matrix is just a one-dimensional entity which reports only three rows and no columns which means that the object is a vector

 * In order to create matrices made of rows and columns , one can make use of lists of list items .

 * The contents of the list inside the main list are the rows of one's matrix

 

==================================

==================================

import numpy as np

X = np.array([1.1,1,545,1],[4.6,0,345,2],[7.2,1,754,3])

print(X)

 

==================================

==================================

 * The output of the following operation would yield a vector which one can reshape into any desired form of rows and columns .

 * Here , the numbers are filled into the new matrix stacked into it in the form of row data which is occupied from the top most to the lowermost elements of the array which are stored in the output object "X" which would store in it all the elements in an iterative manner containing all the elements in i,j manner . Elements in an iterative manner containing all the elements in i,j manner .

 * Operations with addition and subtractions with scalars using the numpy "ndarray" is the best method of performing any operation .

 

* One can do the following operation - sum , subtract , multiply or divide using the severall standard operators applicable over Python language in the given manner . Lets take two data-array objects .. a and b

 ==================================

a = np.array([[1,1],[1,0]])

b = np.array([[1,0],[0,1]])

print(a - b)

[[ 0 1] [1 -1]]

==================================

a = np.array([[0,1],[1,-1]])

print(a * -2)

[[ 0 -2 ]

[ 2 -2 ]]

==================================

 * In order to perform multiplication on vectors and matrices , one needs to use the "np.dot" function . The input for this function is two arrays which are of compatible sizes to multiply according to the given order

 ==================================

X = np.array([[4,5],[2,4],[3,3]])

b = np.array([3,-2])

print(np.dot(X,b))

[ 2 -2  3 ]

B = np.array([3,-2],[-2,5])

print(np.dot(X,B))

 ==================================


 * The same form of multi-dimensional array output can be also produced by an array which one can find in the below set of code . One does not need any additional set of libraries while using R , R performs the tasks using standard functions as given in the below set of code .

 

* One can define the dimensions of one's vector using the "length()" function . But one can use the dis() function instead for the matrices , because applying the length() function to a matrix shows the output to carry only some number of elements in it .

 


Ref : Machine Learning in R and Python - 

No comments:

Post a Comment