对于使用sklearn
在python中实现的多项式回归代码
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
import pandas as pd
m=100
X=6*np.random.rand(m,1)-3
y=0.5*X**3+X+2+np.random.rand(m,1)
poly_features=PolynomialFeatures(3)
X_poly=poly_features.fit_transform(X)
lin_reg=LinearRegression()
X_new=[[0],[3]]
lin_reg.fit(X_poly,y)
plt.plot(X,y,"b.")
plt.plot(X, lin_reg.predict(poly_features.fit_transform(X)), "r-")
plt.show()
输出显示为
但是我想获得一条平滑的预测线。如何获得?