In [79]:import numpy as np
...:import matplotlib.pyplot as plt
In [80]: x = np.random.uniform(-3,3,size=100)...: X = x.reshape(-1,1)
In [81]: y =0.5* x**2+ x +2+ np.random.normal(0,1,size=100)
线性回归
In [83]:from sklearn.linear_model import LinearRegression
...: lin_reg = LinearRegression()...: lin_reg.fit(X,y)
In [84]: y_predict = lin_reg.predict(X)
In [85]: plt.scatter(x,y)...: plt.plot(x,y_predict,color='r')
线性回归效果不好
解决方案,添加一个特征
In [86]:(X**2).shape
Out[86]:(100,1)
In [87]: X2 = np.hstack([X,X**2])
In [88]: X2.shape
Out[88]:(100,2)
In [79]: import numpy as np ...: import matplotlib.pyplot as pltIn [80]: x = np.random.uniform(-3,3,size=100) ...: X = x.reshape(-1,1)In [81]: y = 0.5 * x**2 + x + 2 + np.random.normal(0,1,...