<jupyter_start><jupyter_text>Boston - Cross Validation  Import Libraries<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline

from sklearn import datasets
from sklearn.model_selection import train_test_split, cross_val_score, GridSearchCV
from sklearn.preprocessing import StandardScaler
#from sklearn.neighbors import KNeighborsRegressor
from sklearn.linear_model import LinearRegression, Ridge, Lasso
from sklearn.preprocessing import OneHotEncoder<jupyter_output><empty_output><jupyter_text>Data Loading<jupyter_code>boston = datasets.load_boston()
dir(boston)
#print(boston.DESCR)<jupyter_output><empty_output><jupyter_text>Data Exploring<jupyter_code>X, y = boston.data, boston.target
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
X.shape, y.shape
X_train.shape, X_test.shape, y_train.shape, y_test.shape<jupyter_output><empty_output><jupyter_text>Data Visualization<jupyter_code>fig, axes = plt.subplots(3, 5, figsize=(20, 10))
for i, ax in enumerate(axes.flat):
    if i > 12:
        ax.set_visible(False)
        continue
    ax.plot(X[:, i], y, 'o', alpha=.5)
    ax.set_title("{}: {}".format(i, boston.feature_names[i]))
    ax.set_ylabel("MEDV")
plt.boxplot(X)
plt.xticks(np.arange(1, X.shape[1] + 1),
           boston.feature_names, rotation=30, ha="right");
plt.savefig("../images/boston_unscaled_box.png")
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
plt.boxplot(X_train)
plt.xticks(np.arange(1, X.shape[1] + 1),
           boston.feature_names, rotation=30, ha="right");
plt.savefig("../images/boston_scaled_box.png")<jupyter_output><empty_output><jupyter_text>Linear Regression  Train-Test Score<jupyter_code>lr = LinearRegression()
lr.fit(X_train, y_train)
lr.score(X_test, y_test)<jupyter_output><empty_output><jupyter_text>Cross Validation Score<jupyter_code>scores = cross_val_score(LinearRegression(), X_train, y_train, cv=5)
np.mean(scores)<jupyter_output><empty_output><jupyter_text>Ridge Regression  Train-Test Score<jupyter_code>r = Ridge()
r.fit(X_train, y_train)
r.score(X_test, y_test)<jupyter_output><empty_output><jupyter_text>Cross Validation Score<jupyter_code>scores = cross_val_score(Ridge(), X_train, y_train, cv=5)
np.mean(scores)<jupyter_output><empty_output><jupyter_text>Grid Search: Parameter Alpha  Parameter Definition<jupyter_code>param_grid = {'alpha': np.logspace(-3, 3, 14)}
print(param_grid)<jupyter_output><empty_output><jupyter_text>Grid Creation & Training<jupyter_code>grid = GridSearchCV(Ridge(), param_grid, cv=10, return_train_score=True, iid=True)
grid.fit(X_train, y_train)<jupyter_output><empty_output><jupyter_text>Grid Best Params<jupyter_code>print(grid.best_params_)
print(grid.best_score_)<jupyter_output><empty_output><jupyter_text>Grid Results Visualization<jupyter_code>results = pd.DataFrame(grid.cv_results_)
fig, axes = plt.subplots(figsize=(8, 5))
results.plot('param_alpha', 'mean_train_score', ax=axes)
results.plot('param_alpha', 'mean_test_score', ax=axes)

plt.legend()
plt.xscale("log")
results[['param_alpha', 'mean_test_score', 'std_test_score', 'rank_test_score']]<jupyter_output><empty_output><jupyter_text>Lasso Regression  Train-Test Score<jupyter_code>l = Lasso()
l.fit(X_train, y_train)
r.score(X_test, y_test)<jupyter_output><empty_output><jupyter_text>Cross Validation Score<jupyter_code>scores = cross_val_score(Lasso(), X_train, y_train, cv=5)
np.mean(scores)<jupyter_output><empty_output><jupyter_text>Automatic Features Selection<jupyter_code>boston.feature_names
l.coef_
boston.feature_names[l.coef_ != 0]<jupyter_output><empty_output><jupyter_text>Grid Search: Parameter Alpha  Parameter Definition<jupyter_code>param_grid = {'alpha': np.logspace(-3, 3, 14)}
print(param_grid)<jupyter_output><empty_output><jupyter_text>Grid Creation & Training grid = GridSearchCV(Lasso(), param_grid, cv=10, return_train_score=True, iid=True)grid.fit(X_train, y_train)  Grid Best Params<jupyter_code>print(grid.best_params_)
print(grid.best_score_)<jupyter_output><empty_output><jupyter_text>Grid Results Visualization<jupyter_code>results = pd.DataFrame(grid.cv_results_)
fig, axes = plt.subplots(figsize=(8, 5))
results.plot('param_alpha', 'mean_train_score', ax=axes)
results.plot('param_alpha', 'mean_test_score', ax=axes)

plt.legend()
plt.xscale("log")
results[['param_alpha', 'mean_test_score', 'std_test_score', 'rank_test_score']]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13322918991296587, 'fraction_numerical': 0.00892657888864093, 'mean_word_length': 9.447552447552448, 'pattern_counts': {'":': 0, '<': 73, '<?xml version=': 0, '>': 74, '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': '18', 'dir': 'jupyter-structured-clean-dedup', 'id': '460643', 'license': 'MIT', 'n_tokens_mistral': 1759, 'n_tokens_neox': 1687, 'n_words': 247, 'path': '4-Model_Validation/notebooks/4.2-Boston-Cross_Validation.ipynb', 'repo_name': 'phermar/aprendiz-machine-learning', 'size': '4481'}