I have been trying to implement multiple linear regression from scratch after implementing it using sklearn. The values predicted using sklearn is very accurate whereas the values predicted by the code which I have coded from scratch is not so much accurate. This is the dataset that I am using (I have provided the entire dataset in my Github link mentioned below along with the source code of my implementation)

enter image description here

I am predicting profit of an organisation by seeing the 4 independent variables. Notice that we also have a categorical value here, on which I have done 1 hot encoding, and I have also scaled all the data.

I am using gradient descent to reach the optimal values for slope for the independent variables. The function I have written for gradient descent updation is as follows.

#building the model

#lets start with a random value of m and c



l=0.0001 #l is the learning rate

n=float(len(X_train)) # n=number of training data, we are converting it to float since we will need to divide n

mse=[]  #we are creating a list for storing mean square errors for each iteration so that we can plot it

for i in range (0,20000):
    D_m_1= (2/n) * sum(X_train[:,0:1]*(Y_pred-Y_train))
    D_m_2= (2/n) * sum(X_train[:,1:2]*(Y_pred-Y_train))
    D_m_3= (2/n) * sum(X_train[:,2:3]*(Y_pred-Y_train))
    D_m_4= (2/n) * sum(X_train[:,3:4]*(Y_pred-Y_train))
    D_m_5= (2/n) * sum(X_train[:,4:5]*(Y_pred-Y_train))
    D_m_6= (2/n) * sum(X_train[:,5:6]*(Y_pred-Y_train))
    D_c= (2/n) * sum(Y_pred-Y_train)
    #Andrew Ng has not taken 2/n but instead 1/n as his cost function is multiplied by 1/2 to reduce complexity

#see the plot below, we can see the point where MSE stabilises. Run the above loop till that point and take MSE

This is the function that I have written, and in the other code where I have used sklearn library the code looks like this,

#now we will fit the linear regression model

from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt 
regressor= LinearRegression() 

#predicting the test results

But there is a significant difference between the predicted value of the 2 code and as obvious the sklearn is producing good results. I am not getting why the code which I have written from scratch is having low accuracy. I am providing the dataset as well as the code (where I have written the gradient descent) below.

Link to code and dataset--> https://github.com/Hirak999/StackExchange-Doubts


1 Answer 1


I am using gradient descent to reach the optimal values for slope for the independent variables.

Here is the answer. (Stochastic) gradient descent is not the only way to optimize linear regression model. There is an analytic solution $-$ the parameters $\mathbf{w}$ that minimize squared error loss are determined by a closed-form, ordinary least squares (OLS) solution $$ \mathbf{w} = (\mathbf{X}^T\mathbf{X})^{-1}\mathbf{X}^T\mathbf{y} $$ sklearn.linear_model.LinearRegression is an OLS estimator, and it turns out that your data is fit better with this model.


Not the answer you're looking for? Browse other questions tagged or ask your own question.