<jupyter_start><jupyter_text>Technology Fundamentals Course 3, Lab 2: Practice with Supervised Learners**Instructor**: <NAME>**Contact**: <EMAIL>**Teaching Assitants**: <NAME>, <NAME>**Contact**: <EMAIL>, <EMAIL>---In this lab we will continue to practice creation of pipelines, feature engineering, and applying learning algorithms.Now that we have covered supervised learning methods, and we've covered Grid Search, we will use these tools to do a sophisticated, search of hyperparameter optimization.---  L3 Q1: Create train and test datasets for wine qualityCreate new train/test datasets that are normalized (but have the same indices as the original train/test sets for comparison)<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import classification_report,confusion_matrix
import seaborn as sns

from sklearn.preprocessing import OneHotEncoder
from sklearn.impute import SimpleImputer
from sklearn.linear_model import LinearRegression
from sklearn.pipeline import make_pipeline
from sklearn.compose import make_column_selector, make_column_transformer
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
# Code Cell for L1 Q1
df = pd.read_csv("https://raw.githubusercontent.com/wesleybeckner/"\
                 "ds_for_engineers/main/data/wine_quality/winequalityN.csv")
# Code Cell for L1 Q1

# create X and y
X = df.copy()
y = X.pop('quality')

# split into train/test
X_train, X_test, y_train, y_test = train_test_split(X, y)

# the numerical values pipe
num_proc = make_pipeline(SimpleImputer(strategy='median'), # impute with median
                         StandardScaler()) # scale and center

# the categorical values pipe
cat_proc = make_pipeline(
    SimpleImputer(strategy='constant', 
                  fill_value='missing'), # impute with placeholder
    OneHotEncoder(handle_unknown='ignore')) # one hot encode

# parallelize the two pipes
preprocessor = make_column_transformer((num_proc,
                                make_column_selector(dtype_include=np.number)),
                                       (cat_proc,
                                make_column_selector(dtype_include=object)))<jupyter_output><empty_output><jupyter_text>L3 Q2:Evaluate the performance of a Random Forest on classifying wine quality<jupyter_code># Code Cell for L1 Q2
from sklearn.ensemble import RandomForestRegressor
def MyPipeline(**kwargs):
    return make_pipeline(preprocessor,
                         RandomForestRegressor(**kwargs))
model = MyPipeline()
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
model.score(X_test,y_test)
plt.plot(y_pred, y_test, ls='', marker='.')
plt.figure(figsize=(10,6))
sns.heatmap(confusion_matrix(y_test,np.round(y_pred)),annot=True)<jupyter_output><empty_output><jupyter_text>L3 Q3:Do a grid search to optimize your Random Forest model, use whatever hyperparameters you would like<jupyter_code># Code Cell for L1 Q3
from sklearn.model_selection import GridSearchCV

param_grid = {'randomforestregressor__n_estimators': [100, 200, 300],
              'randomforestregressor__criterion': ['mse', 'mae'],
              'randomforestregressor__min_samples_leaf': [1,5]}

grid = GridSearchCV(MyPipeline(), param_grid, cv=3, verbose=3)
grid.fit(X_train,y_train)
grid.best_params_
model = grid.best_estimator_<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.0876212878565128, 'fraction_numerical': 0.009408997353719493, 'mean_word_length': 4.736930860033727, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '402929', 'license': 'MIT', 'n_tokens_mistral': 1036, 'n_tokens_neox': 993, 'n_words': 283, 'path': 'C3 Machine Learning I/SOLUTIONS/SOLUTION_Tech_Fun_C3_L2_Practice_with_Supervised_Learners.ipynb', 'repo_name': 'ronva-h/technology_fundamentals', 'size': '3450'}