Programming A Driverless Car Chapter 2: Linear Algebra for Machine Learning

Linear Algebra for Machine learning with Python
Python has certain libraries like scipy, numpy and many more they help us to know us the kernel methods of machine learning. Linear Algebra is a mathematical method which contains vector spaces and linear mapping between those spaces. It is study of lines, sub spaces. It is more of a representation of linear data conforming to certain notions and practices. Machine learning algorithms are not efficient enough specially to deal with large data therefore we need linear algebra to deal with it. As we know, representation of large form of data in the form of matrices helps us better visualise the data.
One more advantage of matrices is that all the calculation and processes performed on the data is in the form of matrices or batches ie. we have thousand of data examples which we don’t represent individually. Matrices has all form of data represented in the form of single entity. That is why Linear algebra is very important in machine learning and robotics. For ex. we have seen so many 3-D and animation movies. We have seen so many characters walk on screen and all this has been made possible only with the help of linear algebra.
Linear algebra is best represented in the form of linear equations.We will learn here how to use linear algebra for machine learning using Python. We will discuss these topics:

  • Defining arrays and matrices
  • Working with matrices
  • Solving Equations

Define your arrays, inverse or transpose.
Defining Arrays and Matrices:
We will talk about linear algebra using Python using numpy library. Contents:

  • Defining array and matrices
  • Transpose, inverse and basic operations
  • Eigen values & Eigen Vectors
  • Solving Equations
  1. First, how to install a particular library or module in python. For ex. Numpy.
    Go to command prompt and write
    Pip install numpy
    This will install numpy online. Once it is installed check it by writing
    >>> import numpy
    (if no error, that means it has been installed)We can also import a package as some other name. Like
    Install numpy as np
  2. So, whenever you need to refer numpy, we will use np.>>>import numpy
    >>>import numpy as np
    >>>from numpy.linalg import eigNow, we will define arrays and matrices using python and linear algebra.

>>>import numpy as np1
Output: array([[1,2],[3]],dtype=object)
Output: array([[1,2],[3,4]]
Output: array([[11,12],[13,14]])
Output: matrix([[1,1],[3,4],[5,6]])
Working with matrices
MATLAB Style of defining Matrix
>>> B
Output: matrix ([[1,2],[3,4],[6,8]])
#To find transpose of a matrix
>>>C=B.T -----Storing it in C
#to find inverse
Matrix ([[2,1],[5,1]])
>>>from numpy.linalg import inv
Output: matrix ([[0.833333333, -0.1666666666], [-0.6666666667,0.333333331])
#for dot operator that does row by column multiplication
>>> matrix([[32,42],[56,73]]) -------- This will do column by row
>>> A*B -------- will also work
>>> A.shape


matrix ([[2,1],[4,5]])
Solving Equations
Indexing of Matrix
>>> A[0]
array([[ 1., 1., 1.],
         [1.,   1., 1.],
          [1.,   1., 1.],
          [1.,   1., 1.]])
For solving particular equations–
>>> from numpy.linalg import solve
>>> A=np.array([[4,5],[6,-3]])
array ([[4, 5], [6, -3]])
Eigen values & Eigen Vectors
The eig returns two tuples: the first one is the eigenvalues and the second one is a matrix whose columns are the two eigenvectors.

Recommended Reading:  Chapter 1 Getting Started with Python
Next: Chapter 3 Introduction to Artificial Intelligence
To start reading from a topic of your choice, you can go back to the Table of Contents here
This course evolves with your active feedback. Do let us know your feedback in the comments section below.

Looking for jobs in Artificial Intelligence? Check here


Don't worry we don't spam

      Enable registration in settings - general
      Compare items
      • Total (0)