Skip to content
Advertisement

Why isn’t this Linear Regression line a straight line?

I have points with x and y coordinates I want to fit a straight line to with Linear Regression but I get a jagged looking line.

I am attemting to use LinearRegression from sklearn.

To create the points run a for loop that randomly crates one hundred points into an array that is 100 x 2 in shape. I slice the left side of it for the xs and the right side of it for the ys.

I expect to have a straight line when I print m.predict.

JavaScript

enter image description here

JavaScript

enter image description here

Advertisement

Answer

I am not good with numpy but, I think it is because the use of reshape() function to convert X[:,0] and X[:,1] from 1D to 2D, the resulting 2D array contains only one element, instead of creating a 2D array of len(X[:,0]) and len(X[:,1]) respectively. And resulting into an undesired regressor.
I am able to recreate this model using pandas and able to plot the desired result. Code as follows

JavaScript

enter image description here

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement