Why Vectors Enhance AI Performance in Linear Regression

Vectors in AI plays a very crucial role for fundamental operations for different AI Based applications. Most of the time the underline calculation are hidden to the naked eyes and has been done using python bases libraries.

Let’s try to understand the concept of vectors in simple regression problem.

Dataset :https://www.kaggle.com/datasets/abhishek14398/salary-dataset-simple-linear-regression/data

Consist of three columns , ID, YearsExperience,Salary

IDYearsExperienceSalary
01.239344.0
11.446206.0
21.637732.0
32.143526.0
42.339892.0

Now we need to predict the salary  based on years of experience

ID Column does not make any contribution for predicting the salary so we are going to drop it , now the  input data looks like below 

YearsExperienceSalary
1.239344.0
1.446206.0
1.637732.0
2.143526.0
2.339892.0

Where YearsExperience I.e X is  input feature and Salary I.e  is Output feature

Here is a way how Y is related to X , and relations seems linear in nature I.e. with year of experience the salary is increasing.

Also lets now check statistically how the X and Y are related to each other

As we can see there is a strong correlation between  X and Y , so this problem is linear problem.

A linear problem can be solved using linear equation 

y=ax+b

Where,

y=salary

x=year of experience 

a=slope I.e. change in value of y ,when 1 unit of X changes 

b=intercept I.e. the value of y when x=0  

The same equation for Machine learning and deep learning can be written as 

y=Wx+b

We need to find a or W and b ,  which can be find is  least square method  without using  vectors 

 As per our dataset , if we put the values  W comes out as -109.91 and b comes out as 41529.04 which graphically comes as 

Lets now calculate Y for some value of X is 2.3

y= 41529.04*2.3+-109.91 

y=41,501.06

Vectors

Lets try to solve the same problem using vectorised form 

Understand what would be the calculation points involved here , lets revisit our equation 

y=wx+b

Where  w is the weight 

And b is intercept 

Let’s see now how can we put below data into linearised equation and then into vector forms.

YearsExperienceSalary
1.239344.0
1.446206.0
1.637732.0
2.143526.0
2.339892.0

39344=w*1.2+b

46206=w*1.4+b

————————

39892=w*2.3+b

Writing the above equation in  matrix form 

[39344]=[1.2]*w+[1]*b

[46206]=[1.4]*w+[1]*b

[37732]=[1.6]*w+[1]*b

[43526]=[2.1]*w+[1]*b

[39892]=[2.3]*w+[1]*b

Putting in matrix form containing vectors 

Now lets calculate  a I.e W and b

The formula  is  

Putting the values in place  a and b comes as -109.91. and b=41529.04

Lets try to calculate Y for x 2.3

Y=2.3*(-109.91)+41529.04

y≈41276.25 (predicted value)

Why Vectors Then?

Now the question is that if we can solve the problem without vectors , then what is the need of vectors.  

Here is a time comparison for finding W and both using traditional computation and vector calculations for large dataset I.e for 10k records

We can see that using vectorise form we can have faster calculation which increases the efficiency 

References 

Kaggle : https://www.kaggle.com/code/mishrapankaj26/traditional-vs-vector-approach-for-ml-tasks/edit