<jupyter_start><jupyter_text>Setup<jupyter_code>import pandas as pd
import numpy as np
from sklearn.pipeline import make_pipeline
from sklearn.impute import SimpleImputer
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.preprocessing import StandardScaler
from category_encoders import OneHotEncoder, OrdinalEncoder
from sklearn.metrics import classification_report, plot_roc_curve, roc_auc_score
import matplotlib.pyplot as plt
%%capture
import sys

# If you're on Colab:
if 'google.colab' in sys.modules:
    DATA_PATH = 'https://raw.githubusercontent.com/LambdaSchool/DS-Unit-2-Applied-Modeling/master/data/'
    !pip install category_encoders==2.*

# If you're working locally:
else:
    DATA_PATH = '../data/'<jupyter_output><empty_output><jupyter_text>I. Wrangle Data<jupyter_code>def wrangle(filepath):
    # Import CSV
    df = pd.read_csv(DATA_PATH+'burritos/burritos.csv',
                     parse_dates=['Date'],
                     index_col='Date')
    
    # Drop rows with no target
    df.dropna(axis=0, subset=['overall'], inplace=True)
    
    # Create `'great'` column as target
    df['great'] = (df['overall'] >= 4).astype(int)
    
    # Drop `'overall'` col to avoid leakage
    df.drop(columns='overall', inplace=True)
    
    # Drop columns with lots of `NaN` values
    df.dropna(axis=1, thresh=150, inplace=True)
    
    # Drop high-cardinality categorical variables
    threshold = 10
    high_card_cols = [col for col in df.select_dtypes('object').columns 
                      if df[col].nunique() > threshold]
    df.drop(high_card_cols, axis=1, inplace=True)
    
    # Fix binary columns
    binary_cols = ['Beef', 'Pico', 'Guac', 'Cheese']
    for col in binary_cols:
        df[col] = df[col].str.lower().replace('x', 1).fillna(0).astype(int)
    
    return df

df = wrangle(DATA_PATH+'burritos/burritos.csv')
df.info()
df.head()<jupyter_output><empty_output><jupyter_text>II. Split Data<jupyter_code># Split the TV and FM
target = 'great'
y = df[target]
X = df.drop(target, axis=1)
# Split data into training, validation, and test sets
train_mask = X.index.year < 2017
X_train, y_train = X.loc[train_mask], y.loc[train_mask]

validation_mask = X.index.year == 2017
X_val, y_val = X.loc[validation_mask], y.loc[validation_mask]

test_mask = X.index.year > 2017
X_test, y_test = X.loc[test_mask], y.loc[test_mask]<jupyter_output><empty_output><jupyter_text>III. Establish Baseline<jupyter_code>print('Baseline accuracy:', y_train.value_counts(normalize=True).max())<jupyter_output>Baseline accuracy: 0.5906040268456376<jupyter_text>IV. Build Models1. `LogisticRegression`        - `OneHotEncoder`    - `SimpleImputer`    - `StandardScaler`2. `RandomForestClassifier`        - `OrdinalEncoder`    - `SimpleImputer`<jupyter_code># Model 1: Logistic Regression Model
model_lr = make_pipeline(
    OneHotEncoder(use_cat_names=True),
    SimpleImputer(strategy='most_frequent'),
    StandardScaler(),
    LogisticRegression()
)

model_lr.fit(X_train, y_train);
# Model 2: Random Forest
model_rf = make_pipeline(
    OrdinalEncoder(),
    SimpleImputer(),
    RandomForestClassifier(random_state=42)
)

model_rf.fit(X_train, y_train);<jupyter_output><empty_output><jupyter_text>V. Check Metrics**Accuracy**<jupyter_code>print('Training Accuracy (LOGR):', model_lr.score(X_train, y_train))
print('Validation Accuracy (LOGR):', model_lr.score(X_val, y_val))
print('Training Accuracy (RF):', model_rf.score(X_train, y_train))
print('Validation Accuracy (RF):', model_rf.score(X_val, y_val))<jupyter_output>Training Accuracy (RF): 1.0
Validation Accuracy (RF): 0.8705882352941177<jupyter_text>**Precision, Recall, F1 Score**<jupyter_code>print('Logistic Regression')
print(classification_report(y_val, model_lr.predict(X_val)))
print('Random Forest')
print(classification_report(y_val, model_rf.predict(X_val))) # VALIDATION DATA<jupyter_output>Random Forest
              precision    recall  f1-score   support

           0       0.89      0.87      0.88        47
           1       0.85      0.87      0.86        38

    accuracy                           0.87        85
   macro avg       0.87      0.87      0.87        85
weighted avg       0.87      0.87      0.87        85<jupyter_text>**ROC Curve**- To evaluate models for binary classification.- Decide what probability threshold you should use when making your predictions.<jupyter_code># Use VALIDATION DATA
lr = plot_roc_curve(model_lr, X_val, y_val, label='Logistic')
rf = plot_roc_curve(model_rf, X_val, y_val, ax=lr.ax_, label='Forest')
plt.plot([(0,0), (1,1)], color='grey', linestyle='--')
plt.legend();
print('Logistic: ROC-AUC Score:', roc_auc_score(y_val, model_lr.predict(X_val)))
print('Random Forest: ROC-AUC Score:', roc_auc_score(y_val, model_rf.predict(X_val)))<jupyter_output>Logistic: ROC-AUC Score: 0.809070548712206
Random Forest: ROC-AUC Score: 0.870380739081747
{'fraction_non_alphanumeric': 0.12247372675828617, 'fraction_numerical': 0.031729991915925626, 'mean_word_length': 4.237037037037037, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 32, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '658042', 'license': 'MIT', 'n_tokens_mistral': 1849, 'n_tokens_neox': 1719, 'n_words': 356, 'path': 'module1-define-ml-problems/2021-01-25_231_guided-project.ipynb', 'repo_name': 'Frank-Howd/DS-Unit-2-Applied-Modeling', 'size': '4948'}