<jupyter_start><jupyter_text>Ridge and Lasso Regression  Example 1: Housing Data<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
df = pd.read_csv('../data/kc_house_data.csv')
df.head(n=3)
df.drop(['id', 'date'], axis=1, inplace=True)
df.describe()
X = df.drop(['price'], axis=1)
y = df['price']
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
from sklearn.preprocessing import MinMaxScaler

sc = MinMaxScaler().fit(X_train)
X_train_scaled = sc.transform(X_train)
X_test_scaled = sc.transform(X_test)
from sklearn.linear_model import LinearRegression

lm = LinearRegression().fit(X_train_scaled, y_train)
predict = lm.predict(X_test_scaled)
delta = y_test - predict

plt.scatter(range(len(delta)), delta);
plt.xlabel('Test Sample');
plt.ylabel('Delta');
plt.title('Model Prediction vs Truth');
from sklearn.linear_model import Lasso, Ridge

lasso = Lasso(alpha=500).fit(X_train_scaled, y_train)
ridge = Ridge(alpha=50).fit(X_train_scaled, y_train)
# plt.scatter(range(len(lm.coef_)), lm.coef_, c='g', marker='o', label='Linear Regression');
plt.scatter(range(len(lasso.coef_)), lasso.coef_, c='b', marker='^', label='Lasso Regression');
plt.scatter(range(len(ridge.coef_)), ridge.coef_, c='r', marker='s', label='Ridge Regression');
plt.xlabel('Feature Indices');
plt.ylabel('Feature Weights');
plt.xticks(range(len(X.columns.values)), X.columns.values, rotation=90);
plt.legend();
from sklearn.metrics import r2_score

predict = lm.predict(X_test_scaled)
print(r2_score(y_test, predict))

predict = lasso.predict(X_test_scaled)
print(r2_score(y_test, predict))

predict = ridge.predict(X_test_scaled)
print(r2_score(y_test, predict))<jupyter_output>0.7012700720317631
0.6918891842002093
0.6798494210315613
{'fraction_non_alphanumeric': 0.14802981895633652, 'fraction_numerical': 0.03727369542066028, 'mean_word_length': 7.780373831775701, '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': '291257', 'license': 'MIT', 'n_tokens_mistral': 767, 'n_tokens_neox': 716, 'n_words': 136, 'path': 'notebooks/ridge_and_lasso_regression.ipynb', 'repo_name': 'Joshua-Robison/MLRegression', 'size': '1878'}