<jupyter_start><jupyter_text>Ridge regressionadd L2 regularization term to cost function<jupyter_code>import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
from sklearn import linear_model, datasets, cross_validation
# datasets
boston = datasets.load_boston()
data = boston.data
target = boston.target
x_train, x_test, y_train, y_test = cross_validation.train_test_split(data, target, test_size=0.2)
# define model
model = linear_model.Ridge(alpha=1.0)
model.fit(x_train, y_train)
# predict for test set
y_pred = model.predict(x_test)
print(np.mean((y_pred - y_test)**2))
print(model.coef_)
# linear regression
model_l = linear_model.LinearRegression()
model_l.fit(x_train, y_train)
y_pred_l = model_l.predict(x_test)
print(np.mean((y_pred_l - y_test)**2))
print(model_l.coef_)<jupyter_output>19.666581156998216
[-1.06446116e-01  5.61459693e-02  3.47899554e-02  2.16209691e+00
 -1.84908000e+01  3.27759494e+00  7.04083277e-03 -1.53412749e+00
  3.22856037e-01 -1.21758363e-02 -9.42201760e-01  9.53050841e-03
 -5.86782791e-01]
{'fraction_non_alphanumeric': 0.1147227533460803, 'fraction_numerical': 0.15965583173996176, 'mean_word_length': 5.933774834437086, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '4691', 'license': 'MIT', 'n_tokens_mistral': 520, 'n_tokens_neox': 431, 'n_words': 80, 'path': 'regression/ridge_regression/residence.ipynb', 'repo_name': 'fumiphys/data-science', 'size': '1046'}