这里主要是用多项式线性拟合,然后通过两种方法来优化过拟合和欠拟合
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
import numpy as np
import matplotlib.pyplot as plt
X_train = [[6], [8], [10], [14], [18]]
y_train = [[7], [9], [13], [17.5], [18]]
#线性回归
model = LinearRegression()
model.fit(X_train,y_train)
# #在0到25上均匀取100个点
xx = np.linspace(0,26,100)
xx = xx.reshape(xx.shape[0],1)
yy = model.predict(xx)
#
# plt.scatter(X_train,y_train)
# plt1, = plt.plot(xx,yy,label="Degree=1")
# plt.axis([0,25,0,25])
# plt.legend(handles = [plt1])
# plt.show()
#二次多项式
poly2 = PolynomialFeatures(degree=2)
'''以线性回归器为基础,用上面函数映射出2次多项式特征'''
X_train_poly2 = poly2.fit_transform(X_train)
model2 = LinearRegression()
model2.fit(X_train_poly2,y_train)
xx_poly2 = poly2.transform(xx)
yy_poly2 = model2.predict(xx_poly2)
# plt.scatter(X_