<jupyter_start><jupyter_text>Chapter Break<jupyter_code>from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(inputs, output, test_size=0.33, random_state=42)
from sklearn.preprocessing import PolynomialFeatures, StandardScaler
from sklearn.pipeline import make_pipeline

pipe = make_pipeline(LinearRegression())
pipe.fit(X_train, y_train)
pipe.score(X_train, y_train)
pipe.score(X_test, y_test)
from sklearn.linear_model import Ridge

# tactic 1: minimize weights, smaller the better, higher penalty on large weights
# = ridge regression

pipe = make_pipeline(StandardScaler(), PolynomialFeatures(degree=3), Ridge())
pipe.fit(X_train, y_train)
pipe.steps[2][1].coef_
pipe.steps[2][1].coef_.max(), pipe.steps[2][1].coef_.min(), pipe.steps[2][1].coef_.std()
pipe.score(X_train, y_train)
pipe.score(X_test, y_test)
from sklearn.linear_model import Lasso

# tactic 2: minimize number of non-zero weights
# = Lasso

pipe = make_pipeline(StandardScaler(), PolynomialFeatures(degree=3), Lasso())
pipe.fit(X_train, y_train)
pipe.score(X_train, y_train)
pipe.score(X_test, y_test)
pipe.steps[2][1].coef_
pipe.steps[2][1].coef_.max(), pipe.steps[2][1].coef_.min(), pipe.steps[2][1].coef_.std()
from sklearn.linear_model import ElasticNet

# tactic 3: mix lasso and ridge!
# = elasticnet

pipe = make_pipeline(StandardScaler(), PolynomialFeatures(degree=3), ElasticNet())
pipe.fit(X_train, y_train)
pipe.score(X_train, y_train)
pipe.score(X_test, y_test)
pipe.steps[2][1].coef_
pipe.steps[2][1].coef_.max(), pipe.steps[2][1].coef_.min(), pipe.steps[2][1].coef_.std()<jupyter_output><empty_output><jupyter_text>----  Implementing a model to classify wines<jupyter_code>from sklearn.datasets import load_wine
wine_data = load_wine()
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(
    wine_data.data, wine_data.target, test_size=0.5, random_state=42)
import numpy as np
import pandas as pd
df_x_train = pd.DataFrame(X_train, columns=wine_data.feature_names)
df_x_train.describe()
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import make_pipeline
from sklearn import linear_model
from sklearn import svm
from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import confusion_matrix
pipe = make_pipeline(GaussianNB())
pipe.fit(X_train, y_train)
pipe.score(X_train, y_train), pipe.score(X_test, y_test)
confusion_matrix(y_test, pipe.predict(X_test))
pipe = make_pipeline(svm.SVC())
pipe.fit(X_train, y_train)
pipe.score(X_train, y_train), pipe.score(X_test, y_test)
confusion_matrix(y_test, pipe.predict(X_test))
pipe = make_pipeline(linear_model.LogisticRegression())
pipe.fit(X_train, y_train)
pipe.score(X_train, y_train), pipe.score(X_test, y_test)
confusion_matrix(y_test, pipe.predict(X_test))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12341010656583018, 'fraction_numerical': 0.013406668958404951, 'mean_word_length': 8.009287925696594, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '13228', 'license': 'MIT', 'n_tokens_mistral': 1075, 'n_tokens_neox': 1059, 'n_words': 207, 'path': 'Chapter 5.ipynb', 'repo_name': 'PacktPublishing/-Python-Your-First-Step-Toward-Data-Science-V-', 'size': '2909'}