<jupyter_start><jupyter_text>Ridge Regression<jupyter_code>import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression, Ridge
from sklearn.metrics import mean_squared_error
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import MinMaxScaler, PolynomialFeatures
_df = pd.read_csv('https://raw.githubusercontent.com/PacktWorkshops/The-Data-Science-Workshop/master/Chapter07/Dataset/ccpp.csv')
_df.info()
X = _df.drop(['PE'], axis=1).values
y = _df['PE'].values
train_X, eval_X, train_y, eval_y = train_test_split(X, y, train_size=0.8, random_state=0)<jupyter_output><empty_output><jupyter_text>Implement a LinearRegression model<jupyter_code>lr_model_1 = LinearRegression()
lr_model_1.fit(train_X, train_y)
lr_model_1_preds = lr_model_1.predict(eval_X)
print('lr_model_1 R2 Score: {}'.format(lr_model_1.score(eval_X, eval_y)))
print('lr_model_1 MSE: {}'.format(mean_squared_error(eval_y, lr_model_1_preds)))<jupyter_output>lr_model_1 MSE: 19.733699303497648<jupyter_text>Engineer cubic features<jupyter_code>steps = [
    ('scaler', MinMaxScaler()),
    ('poly', PolynomialFeatures(degree=3)),
    ('lr', LinearRegression())
]
lr_model_2 = Pipeline(steps)
lr_model_2.fit(train_X, train_y)
print('lr_model_2 R2 Score: {}'.format(lr_model_2.score(eval_X, eval_y)))
lr_model_2_preds = lr_model_2.predict(eval_X)
print('lr_model_2 MSE: {}'.format(mean_squared_error(eval_y, lr_model_2_preds)))
print(lr_model_2[-1].coef_)
print(len(lr_model_2[-1].coef_))<jupyter_output>35<jupyter_text>Engineer polynomial features<jupyter_code>steps = [
    ('scaler', MinMaxScaler()),
    ('poly', PolynomialFeatures(degree=10)),
    ('lr', LinearRegression())
]
lr_model_3 = Pipeline(steps)
lr_model_3.fit(train_X, train_y)
print('lr_model_3 R2 Score: {}'.format(lr_model_3.score(eval_X, eval_y)))
lr_model_3_preds = lr_model_3.predict(eval_X)
print('lr_model_3 MSE: {}'.format(mean_squared_error(eval_y, lr_model_3_preds)))
print(len(lr_model_3[-1].coef_))
print(lr_model_3[-1].coef_[:35])<jupyter_output>[ 3.92417572e+05 -6.90884957e+07 -4.12728037e+07  2.27928559e+07
 -4.76788092e+07  2.96663457e+08  2.73267484e+08  1.07844757e+08
  3.73718995e+08  8.79698968e+07 -2.35367959e+07  2.46251982e+08
 -2.61104947e+08  1.86087661e+07  1.41131196e+08 -6.53886924e+08
 -8.90633353e+08 -1.06074814e+09 -1.29264510e+09 -4.28435046e+08
  5.31626843e+07 -1.30408977e+09  4.41024830e+08 -8.86228713e+08
 -8.78160515e+08 -1.97377311e+06 -5.39375259e+08 -3.68352714e+08
  9.82113559e+08 -2.76718687e+08 -6.28824872e+08  8.14257203e+08
  5.43205856e+08 -2.03045074e+08 -2.42929048e+08]<jupyter_text>Implement Ridge on the same pipeline<jupyter_code>steps = [
    ('scaler', MinMaxScaler()),
    ('poly', PolynomialFeatures(degree=10)),
    ('lr', Ridge(alpha=0.9))
]
ridge_model = Pipeline(steps)
ridge_model.fit(train_X, train_y)
print('ridge_model R2 Score: {}'.format(ridge_model.score(eval_X, eval_y)))
ridge_model_preds = ridge_model.predict(eval_X)
print('ridge_model MSE: {}'.format(mean_squared_error(eval_y, ridge_model_preds)))
print(len(ridge_model[-1].coef_))
print(ridge_model[-1].coef_[:35])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14850863422291993, 'fraction_numerical': 0.14442700156985872, 'mean_word_length': 7.657608695652174, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, 'https://': 1, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '492721', 'license': 'MIT', 'n_tokens_mistral': 1608, 'n_tokens_neox': 1375, 'n_words': 175, 'path': 'Chapter07/Exercise7.10/Exercise7_10.ipynb', 'repo_name': 'pmayd/The-Data-Science-Workshop', 'size': '3185'}