<jupyter_start><jupyter_text>______Copyright by <NAME> Inc.For more information, visit us at www.pieriandata.com  Grid Search We can search through a variety of combinations of hyperparameters with a grid search. While many linear models are quite simple and even come with their own specialized versions that do a search for you, this method of a grid search will can be applied to *any* model from sklearn, and we will need to use it later on for more complex models, such as Support Vector Machines.  Imports<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns<jupyter_output>C:\ProgramData\Anaconda3\lib\site-packages\statsmodels\tools\_testing.py:19: FutureWarning: pandas.util.testing is deprecated. Use the functions in the public API at pandas.testing instead.
  import pandas.util.testing as tm<jupyter_text>Data<jupyter_code>df = pd.read_csv("../DATA/Advertising.csv")
df.head()<jupyter_output><empty_output><jupyter_text>Formatting Data<jupyter_code>## CREATE X and y
X = df.drop('sales',axis=1)
y = df['sales']

# TRAIN TEST SPLIT
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=101)

# SCALE DATA
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
scaler.fit(X_train)
X_train = scaler.transform(X_train)
X_test = scaler.transform(X_test)<jupyter_output><empty_output><jupyter_text>Model<jupyter_code>from sklearn.linear_model import ElasticNet
help(ElasticNet)
base_elastic_model = ElasticNet()<jupyter_output><empty_output><jupyter_text>Grid SearchA search consists of:* an estimator (regressor or classifier such as sklearn.svm.SVC());* a parameter space;* a method for searching or sampling candidates;* a cross-validation scheme * a score function.<jupyter_code>param_grid = {'alpha':[0.1,1,5,10,50,100],
              'l1_ratio':[.1, .5, .7, .9, .95, .99, 1]}
from sklearn.model_selection import GridSearchCV
# verbose number a personal preference
grid_model = GridSearchCV(estimator=base_elastic_model,
                          param_grid=param_grid,
                          scoring='neg_mean_squared_error',
                          cv=5,
                          verbose=2)
grid_model.fit(X_train,y_train)
grid_model.best_estimator_
grid_model.best_params_
# pd.DataFrame(grid_model.cv_results_)<jupyter_output><empty_output><jupyter_text>Using Best Model From Grid Search<jupyter_code>y_pred = grid_model.predict(X_test)
from sklearn.metrics import mean_squared_error
mean_squared_error(y_test,y_pred)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08793300342596118, 'fraction_numerical': 0.01218119527978683, 'mean_word_length': 5.24228028503563, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '497829', 'license': 'MIT', 'n_tokens_mistral': 878, 'n_tokens_neox': 845, 'n_words': 225, 'path': 'Data Science Resources/Jose portila - ML/10-Cross-Val-and-LinReg-Project/01-Grid-Search.ipynb', 'repo_name': 'FarzinRad/CS-Tutorial', 'size': '2633'}