<jupyter_start><jupyter_text>岭回归<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
np.random.seed(42)
x = np.random.uniform(-3., 3. , size=100)
X = x.reshape(-1,1)
y = 0.5 * x + 3 + np.random.normal(0,1,size=100)
plt.scatter(x,y)
# plt.ylabel('Y label', rotation=0)
# plt.yticks(rotation=-90,fontsize = 8)
plt.show()
from sklearn.linear_model.base import LinearRegression
from sklearn.metrics.scorer import mean_squared_error
from sklearn.pipeline import Pipeline
from sklearn.preprocessing.data import PolynomialFeatures
from sklearn.preprocessing.data import StandardScaler

def PolynomialRegression(degree):
    return Pipeline(
        [
            ('poly', PolynomialFeatures(degree=degree)),
            ('std_scaler', StandardScaler()),
            ('lin_reg',LinearRegression())
        ]
    )

from sklearn.model_selection._split import train_test_split

np.random.seed(666)
X_train, X_test, y_train, y_test = train_test_split(X,y)
poly_reg = PolynomialRegression(degree=20)
poly_reg.fit(X_train, y_train)

y_poly_predict = poly_reg.predict(X_test)
mean_squared_error(y_test, y_poly_predict)<jupyter_output>/usr/local/seamonster/MachineLearningClassicAlgorithmEnv/lib/python3.6/site-packages/scipy/linalg/basic.py:1226: RuntimeWarning: internal gelsd driver lwork query error, required iwork dimension not returned. This is likely the result of LAPACK bug 0038, fixed in LAPACK 3.2.2 (released July 21, 2010). Falling back to 'gelss' driver.
  warnings.warn(mesg, RuntimeWarning)<jupyter_text>预测测试数据集时均方误差值这么大，显然是过拟合了<jupyter_code># 把拟合的曲线画出来
X_plot = np.linspace(-3,3,100).reshape(100,1)
y_plot = poly_reg.predict(X_plot)

plt.scatter(x,y)
plt.plot(X_plot[:,0], y_plot, color='r')
plt.axis([-3,3,0,6])
plt.show()
# 把画图抽出来作为一个方法
def plot_model(model):
    X_plot = np.linspace(-3,3,100).reshape(100,1)
    y_plot = model.predict(X_plot)

    plt.scatter(x,y)
    plt.plot(X_plot[:,0], y_plot, color='r')
    plt.axis([-3,3,0,6])
    plt.show()
plot_model(poly_reg)<jupyter_output><empty_output><jupyter_text>使用岭回归调整过拟合情况<jupyter_code>from sklearn.linear_model.ridge import Ridge

# 超参数α
ridge = Ridge(alpha=1)
# 封装一个使用岭回归的管道
def RidgeRegression(degree, alpha):
    return Pipeline([
        ('poly', PolynomialFeatures(degree=degree)),
        ('std_scaler', StandardScaler()),
        ('ridge_reg',Ridge(alpha=alpha))
    ])<jupyter_output><empty_output><jupyter_text>α取0.0001<jupyter_code># 使用岭回归后的（对测试集的）均方误差是多少呢？
ridge1_reg =RidgeRegression(20, 0.0001)
ridge1_reg.fit(X_train, y_train)

y1_predict = ridge1_reg.predict(X_test)
mean_squared_error(y_test, y1_predict)<jupyter_output><empty_output><jupyter_text>均方误差大幅下降了超过90%<jupyter_code># 使用岭回归后的拟合曲线是什么样呢？
plot_model(ridge1_reg)<jupyter_output><empty_output><jupyter_text>α取1<jupyter_code>ridge2_reg =RidgeRegression(20, 1)
ridge2_reg.fit(X_train, y_train)

y2_predict = ridge2_reg.predict(X_test)
mean_squared_error(y_test, y2_predict)
# 绘制拟合曲线
plot_model(ridge2_reg)<jupyter_output><empty_output><jupyter_text>α取100<jupyter_code>ridge3_reg =RidgeRegression(20, 100)
ridge3_reg.fit(X_train, y_train)

y3_predict = ridge3_reg.predict(X_test)
mean_squared_error(y_test, y3_predict)
plot_model(ridge3_reg)<jupyter_output><empty_output><jupyter_text>α取一个非常大的数<jupyter_code>ridge4_reg =RidgeRegression(20, 1e8)
ridge4_reg.fit(X_train, y_train)

y4_predict = ridge4_reg.predict(X_test)
mean_squared_error(y_test, y4_predict)
plot_model(ridge4_reg)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1257242178447277, 'fraction_numerical': 0.03707995365005794, 'mean_word_length': 6.97459584295612, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 32, '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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '590661', 'license': 'MIT', 'n_tokens_mistral': 1538, 'n_tokens_neox': 1499, 'n_words': 193, 'path': 'c5_polynomial_regression/08_Ridge_Regression.ipynb', 'repo_name': 'Sea-Monster/MachineLearningClassicAlgorithm', 'size': '3452'}