Search code examples
pythonnumpymatplotliblinear-regressioncurve

Difficulty in plotting 2 degree linear regression


I'm facing some trouble while plotting a 2d linear regression with a 2 degree curve from a Machine Learning model.

Here is my code:

m = 100
X = 6 * np.random.rand(m, 1) - 3

y = 0.5 * X**2 + X + 2 + np.random.randn(m, 1)
plt.plot(X, y, "b.")
plt.show() 

Until here ok, this is my scatterplot:

enter image description here

Now I'm training a 2 degree linear model.

from sklearn.preprocessing import PolynomialFeatures

poly_features = PolynomialFeatures(degree=2, include_bias=False)
X_poly = poly_features.fit_transform(X)

lin_reg = LinearRegression()
lin_reg.fit(X_poly, y)

Model trained.

And this is my result when I plot the curve:

plt.plot(X, lin_reg.predict(X_poly), "r-")
plt.show()

enter image description here

I'm figuring out how to print the continuous curve, and not those line-connected points.

Below is my desired output, manually draw.

enter image description here


Solution

    • You're going to kick yourself, probably
    • Everything is fine, except X needs to be sorted with numpy.sort().
      • The lines are drawn in order, from point to point. Since the points are not ordered by X, they're drawn all over.
    • np.random.seed(365) has been specified to produce the same values each time.
    import numpy
    from sklearn.linear_model import LinearRegression
    from sklearn.preprocessing import PolynomialFeatures
    
    m = 100
    np.random.seed(365)
    X = 6 * np.random.rand(m, 1) - 3
    X = np.sort(X, axis=0)  # sort the values
    
    y = 0.5 * X**2 + X + 2 + np.random.randn(m, 1)
    
    # regression code
    poly_features = PolynomialFeatures(degree=2, include_bias=False)
    X_poly = poly_features.fit_transform(X)
    
    lin_reg = LinearRegression()
    lin_reg.fit(X_poly, y)
    
    # plot
    plt.plot(X, y, "b.")
    plt.plot(X, lin_reg.predict(X_poly), "r-")
    plt.show() 
    

    enter image description here