# 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 eig`Now, we will define arrays and matrices using python and linear algebra. `>>>import numpy as np1`
`             >>>a=numpy.array([1,2],[3,4])`
`             >>>a`
Output:
`array([1,2],[3,4])`
`             >>>a=np.array([1,2],[3,4)`
`             >>>a`
Output:` array([[1,2],],dtype=object)`
`             >>>a=np.array([[1,2],[3,4]])`
`             >>>a`
Output: `array([[1,2],[3,4]]`
`            >>>b=np.array([[11,12],[13,14]])`
`            >>>b`
Output: `array([[11,12],[13,14]])`
`           >>>A=np.matrix([[1,1],[3,4],[5,6]])`
`           >>>A`
Output: `matrix([[1,1],[3,4],[5,6]])`
Working with matrices
MATLAB Style of defining Matrix
>>>`B=np.matrix(“1,2;3,4;6,8”)`
`>>> B`
Output: `matrix ([[1,2],[3,4],[6,8]])`
#To find transpose of a matrix
`>>>B.T`
`>>>C=B.T -----Storing it in C`
#to find inverse
`>>>D=np.matrix(“2,1;4,5”)`
`>>>D`
`Matrix ([[2,1],[5,1]])`
`>>>from numpy.linalg import inv`
`>>>inv(D)`
Output: `matrix ([[0.833333333, -0.1666666666], [-0.6666666667,0.333333331])`
`>>>D*inv(D)`
#for dot operator that does row by column multiplication
`>>>A=np.matrix(“2,4;5,6”)`
`>>>B=np.matrix(“4,5;6,8”)`
`>>>np.dot(A,B)`
`>>> matrix([[32,42],[56,73]]) -------- This will do column by row`
`>>>np.inner(A,B)`
`>>>matrix([[28,44],[50,78]])`
`>>> A*B -------- will also work`
`>>> A.shape (2,2)`
`>>>B.shape (2,2)`
`>>>D`
`matrix ([[2,1],[4,5]])`
`>>>D.shape`
`(2,2)`
Solving Equations
Indexing of Matrix
>>> A
matrix([[2,4])
>>>A
matrix([[5,6]])
>>>A[0,0]
2
>>>a[0,1]
2
>>>A[0,1]
4
>>>A[1,0]
5
>>>A[1,1]
6
>>>A
matrix([[2,4],[5,6]])
>>>np.rank(A)
2
>>>A=np.ones((4,3))
>>>A
array([[ 1., 1., 1.],
[1.,   1., 1.],
[1.,   1., 1.],
[1.,   1., 1.]])
>>>np.rank(A)
2
For solving particular equations–
4x+5y=23
6x-3y=3
>>> from numpy.linalg import solve
>>> A=np.array([[4,5],[6,-3]])
>>>A
array ([[4, 5], [6, -3]])
>>>b=np.array([23,3])
>>>b
array([23,3])
>>>x=solve(A,b)
>>>x=solve(A,b)
>>>x
array([2.,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 