import numpy as np
import pandas as pd
from scipy import stats
import matplotlib.pyplot as plt
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
data = np.array([[ -2.95507616, 10.94533252],
[ -0.44226119, 2.96705822],
[ -2.13294087, 6.57336839],
[ 1.84990823, 5.44244467],
[ 0.35139795, 2.83533936],
[ -1.77443098, 5.6800407 ],
[ -1.8657203 , 6.34470814],
[ 1.61526823, 4.77833358],
[ -2.38043687, 8.51887713],
[ -1.40513866, 4.18262786]])
m = data.shape[0] #样本个数
xMat = data[:,0].reshape(-1,1)
yMat = data[:,1].reshape(-1,1) #将array转换为矩阵
poly = PolynomialFeatures(degree=2)
xPolyMat=poly.fit_transform(xMat)
print("多项式处理后的数据为:",xPolyMat)
intercept,coef=LinearRegression().fit(xPolyMat,yMat)
xPlot = np.linspace(-3,3,1000).reshape(-1,1)
xPlotPoly =poly.fit_transform(xPlot)
yPlot = np.dot(xPlotPoly,coef.T)+intercept
plt.plot(xPlot,yPlot,'r-')
plt.plot(xMat,yMat,'b.')
plt.show()
这个是基于jupyter notebook,关于机械学习多项式回归,不论我怎么运行都不行,希望帮忙查看一下错误,给出修改意见和原因,衷心感谢!
以下为错误
TypeError Traceback (most recent call last)
in
22 xPolyMat=poly.fit_transform(xMat)
23 print("多项式处理后的数据为:",xPolyMat)
-> 24 intercept,coef=LinearRegression().fit(xPolyMat,yMat)
25 xPlot = np.linspace(-3,3,1000).reshape(-1,1)
26 xPlotPoly =poly.fit_transform(xPlot)
TypeError: 'LinearRegression' object is not iterable