<jupyter_start><jupyter_text>岭回归  目标：$$J(\theta)={{MSE}(y, \hat y;\theta)+\alpha\frac{1}{2}{\sum_{i=1}^n}{\theta_i^2}}$$<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
np.random.seed(42)
x = np.random.uniform(-3.0, 3.0, size=100)
X = x.reshape(-1, 1)
y = 0.5 * x + 3 + np.random.normal(0, 1, size=100)
plt.scatter(x, y)
plt.show()
X.shape
x.shape
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import PolynomialFeatures
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LinearRegression

def PolynomialRegression(degree):
    return Pipeline([
        ('ploy', PolynomialFeatures(degree=degree)),
        ('std_scaler', StandardScaler()),
        ('lin_reg', LinearRegression())
    ])
from sklearn.model_selection import train_test_split

np.random.seed(666)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=666)
from sklearn.metrics import mean_squared_error
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)
X_polt = np.linspace(-3, 3, 100).reshape(100, 1)
y_polt = poly_reg.predict(X_polt)

plt.scatter(x, y)
plt.plot(X_polt[:, 0], y_polt, color='r')
plt.axis([-3, 3, 0, 6])
plt.show()
def plot_model(model):
    X_polt = np.linspace(-3, 3, 100).reshape(100, 1)
    y_polt = model.predict(X_polt)

    plt.scatter(x, y)
    plt.plot(X_polt[:, 0], y_polt, 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 import Ridge
def RidgeRegression(degree, alpha):
    return Pipeline([
        ('ploy', PolynomialFeatures(degree=degree)),
        ('std_scaler', StandardScaler()),
        ('ridge_reg', Ridge(alpha=alpha))
    ])
ridge1_reg = RidgeRegression(degree=20, alpha=0.0001)
ridge1_reg.fit(X_train, y_train)

y1_predict = ridge1_reg.predict(X_test)
mean_squared_error(y_test, y1_predict)
plot_model(ridge1_reg)
ridge2_reg = RidgeRegression(degree=20, alpha=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)
ridge3_reg = RidgeRegression(degree=20, alpha=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>
{'fraction_non_alphanumeric': 0.14297419090536664, 'fraction_numerical': 0.03850880786562884, 'mean_word_length': 6.119533527696793, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '144129', 'license': 'MIT', 'n_tokens_mistral': 1039, 'n_tokens_neox': 975, 'n_words': 165, 'path': 'data/19-Ridge-Regression.ipynb', 'repo_name': 'IIMostro/machine-learning', 'size': '2441'}