<jupyter_start><jupyter_text>sklearn中的多项式回归<jupyter_code>from sklearn.preprocessing import PolynomialFeatures
"""
degree=2
表示最多到2次幂

针对X，增加多个特征
"""
poly=PolynomialFeatures(degree=2)
poly.fit(X)
X2 = poly.transform(X)
X2.shape
X2[:5, :]
X[:5,:]
from sklearn.linear_model import LinearRegression
lin_reg2=LinearRegression()
lin_reg2.fit(X2, y)
y_predict2=lin_reg2.predict(X2)
plt.scatter(x, y, color='r')
plt.plot(np.sort(x), y_predict2[np.argsort(x)], color='g')
plt.show()
lin_reg2.coef_
lin_reg2.intercept_<jupyter_output><empty_output><jupyter_text>关于PolynomialFeatures<jupyter_code>X=np.arange(1,11).reshape(-1,2)
X.shape
X
poly=PolynomialFeatures(degree=2)
poly.fit(X)
X2=poly.transform(X)
X2.shape
X2<jupyter_output><empty_output><jupyter_text>Pipeline 把每次常规化的操作，放进管道中<jupyter_code>x=np.random.uniform(-3.0, 3.0, size=100)
X=x.reshape(-1,1)
y= 0.5*x**2 + x + 2.0 + np.random.normal(0.0, 1.0, size=100)
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import PolynomialFeatures
from sklearn.preprocessing import StandardScaler
poly_reg=Pipeline([
    ('poly', PolynomialFeatures(degree=2)),
    ('std_scaler', StandardScaler()),
    ('lin_reg', LinearRegression())
])
poly_reg.fit(X, y)
y_predict=poly_reg.predict(X)
y_predict.shape
plt.scatter(x, y)
plt.plot(np.sort(x), y_predict[np.argsort(x)], color='r')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16143170197224252, 'fraction_numerical': 0.03506208911614317, 'mean_word_length': 8.51388888888889, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '88236', 'license': 'Apache-2.0', 'n_tokens_mistral': 624, 'n_tokens_neox': 613, 'n_words': 78, 'path': 'imooc/PCA/sklearn-polynomial.ipynb', 'repo_name': 'imgoodman/play-with-machine-learning', 'size': '1369'}