# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/maiormarso/DS-Unit-2-Linear-Models/blob/master/module4-logistic-regression/LS_DS9_214_assignment_burrito_regression_classification_4.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + [markdown] id="gvbBPML4taKB" colab_type="text"
# Lambda School Data Science
#
# *Unit 2, Sprint 1, Module 4*
#
# ---

# + [markdown] colab_type="text" id="7IXUfiQ2UKj6"
# # Logistic Regression
#
#
# ## Assignment 🌯
#
# You'll use a [**dataset of 400+ burrito reviews**](https://srcole.github.io/100burritos/). How accurately can you predict whether a burrito is rated 'Great'?
#
# > We have developed a 10-dimensional system for rating the burritos in San Diego. ... Generate models for what makes a burrito great and investigate correlations in its dimensions.
#
# - [ ] Do train/validate/test split. Train on reviews from 2016 & earlier. Validate on 2017. Test on 2018 & later.
# - [ ] Begin with baselines for classification.
# - [ ] Use scikit-learn for logistic regression.
# - [ ] Get your model's validation accuracy. (Multiple times if you try multiple iterations.)
# - [ ] Get your model's test accuracy. (One time, at the end.)
# - [ ] Commit your notebook to your fork of the GitHub repo.
# - [ ] Watch Aaron's [video #1](https://www.youtube.com/watch?v=pREaWFli-5I) (12 minutes) & [video #2](https://www.youtube.com/watch?v=bDQgVt4hFgY) (9 minutes) to learn about the mathematics of Logistic Regression.
#
#
# ## Stretch Goals
#
# - [ ] Add your own stretch goal(s) !
# - [ ] Make exploratory visualizations.
# - [ ] Do one-hot encoding.
# - [ ] Do [feature scaling](https://scikit-learn.org/stable/modules/preprocessing.html).
# - [ ] Get and plot your coefficients.
# - [ ] Try [scikit-learn pipelines](https://scikit-learn.org/stable/modules/compose.html).

# + id="T7IrxsSNx4Ji" colab_type="code" colab={}
import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/LambdaSchool/DS-Unit-2-Linear-Models/master/data/burritos/burritos.csv')

# + id="yuFxSDY6yA4b" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 150} outputId="cd5c8fa4-0923-428f-bb40-beefeb8b7315"
df.head(1)

# + id="-LQ5EZ3PtaKM" colab_type="code" colab={}
# Derive binary classification target:
# We define a 'Great' burrito as having an
# overall rating of 4 or higher, on a 5 point scale.
# Drop unrated burritos.
df = df.dropna(subset=['overall'])
df['Great'] = df['overall'] >= 4

# + id="FULJQbA9taKP" colab_type="code" colab={}
# Clean/combine the Burrito categories
df['Burrito'] = df['Burrito'].str.lower()

california = df['Burrito'].str.contains('california')
asada = df['Burrito'].str.contains('asada')
surf = df['Burrito'].str.contains('surf')
carnitas = df['Burrito'].str.contains('carnitas')

df.loc[california, 'Burrito'] = 'California'
df.loc[asada, 'Burrito'] = 'Asada'
df.loc[surf, 'Burrito'] = 'Surf & Turf'
df.loc[carnitas, 'Burrito'] = 'Carnitas'
df.loc[~california & ~asada & ~surf & ~carnitas, 'Burrito'] = 'Other'

# + id="Le5zjFRztaKR" colab_type="code" colab={}
# Drop some high cardinality categoricals
df = df.drop(columns=['Notes', 'Location', 'Reviewer', 'Address', 'URL', 'Neighborhood'])

# + id="Ic_MKKENtaKV" colab_type="code" colab={}
# Drop some columns to prevent "leakage"
df = df.drop(columns=['Rec', 'overall'])

# + id="a9txHPvUtaKa" colab_type="code" colab={}
df.tail(20)

# + id="D24QBC9ls_10" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 126} outputId="1d9efc80-c401-461c-811b-4ef14d3ae4cf"
df.Burrito.value_counts()

# + id="iFxfsENatVT1" colab_type="code" colab={}
burrito_dict = {'California': 0,'Other': 1,'Asada': 2,'Surf & Turf':3,'Carnitas':4}

# + id="V1jSlpNXuHNw" colab_type="code" colab={}
df['Burrito']=df['Burrito'].replace(burrito_dict)

# + id="cuLvf-4EujEk" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 235} outputId="436248e4-b851-470b-dd06-30603eabe876"
df.Burrito

# + [markdown] id="XW0qGvxIBo8z" colab_type="text"
# ###10 Dimensional Burrito
# 1. Volume - “size matters,” “bigger is better,” or whatever your favorite innuendo is fits because there’s nothing more disconcerting than ordering a burrito and not being full.
# 2. Tortilla quality
# 3. Temperature - the Goldilocks zone
# 4. Meat quality
# 5. Non-meat filling quality
# 6. Meat : filling - The ratio between meat and non-meat. Perhaps the golden ratio: 1.6180339887…
# 7. Uniformity - Bites full of sour cream and cheese with no meat are disappointing.
# 8. Salsa quality - and variety!
# 9. Flavor synergy - "That magical aspect a great burrito has, making everything come together like it is a gift from the skies" - A wise Dutchman
# 10.Wrap integrity - you ordered a burrito, not a burrito bowl.

# + id="bdgxLxRF16WC" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 35} outputId="43b8ab45-4a8c-47cd-eb52-0b83cc6e74b6"
df.shape 

# + id="A8JmL7BA3n_W" colab_type="code" colab={}
df['Date'] = pd.to_datetime(df['Date'], infer_datetime_format=True)

# + id="AZ0debvL9x6D" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 35} outputId="e0c860df-bbb5-4fa8-e138-2c18c440ece1"
train = df[df.Date.dt.year <= 2016]
val = df[df.Date.dt.year == 2017]
test = df[df.Date.dt.year >= 2018]

train.shape, val.shape, test.shape

# + [markdown] id="HdMKow7O99Fu" colab_type="text"
#  ##Begin with baselines for classification.

# + id="u4GyBLXx98CY" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 72} outputId="a814da34-3831-41f8-c546-25b747796ed2"
target='Great'
train[target].value_counts(normalize=True)

# + [markdown] id="2g9CLfwoe7S1" colab_type="text"
# ###Use scikit-learn for logistic regression.

# + id="9OMIY8_tfCfK" colab_type="code" colab={}
features = ['Length','Circum','Volume','Tortilla','Temp','Meat','Fillings',
            'Meat:filling','Uniformity','Salsa','Synergy','Wrap']

# + id="E7GPPj3_gxN-" colab_type="code" colab={}
X_train=train[features]
y_train=train[target]
X_val=val[features]
y_val=val[target]

# + id="HwpvpGw4hQxh" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 35} outputId="a4611d26-ee4f-4ad9-f349-8313a3f41199"
from sklearn.impute import SimpleImputer
from sklearn.linear_model import LogisticRegressionCV
from sklearn.preprocessing import StandardScaler

imputer = SimpleImputer()
X_train_imputed = imputer.fit_transform(X_train)
X_val_imputed = imputer.transform(X_val)

scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train_imputed)
X_val_scaled = scaler.transform(X_val_imputed)

model = LogisticRegressionCV(cv=5, n_jobs=-1, random_state=42)
model.fit(X_train_scaled, y_train)
print('Validation Accuracy', model.score(X_val_scaled, y_val))

# + [markdown] id="x2q9oJXLlSGd" colab_type="text"
# ###one-hot-encoding

# + id="q-XHxrBVlMxT" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 35} outputId="26dc34a5-ea30-47bd-ec30-ee3fab21dad0"
import category_encoders as ce

features = train.columns.drop([target, 'Date'])
X_train = train[features]
X_val = val[features]

encoder = ce.OneHotEncoder(use_cat_names=True)
X_train_encoded = encoder.fit_transform(X_train)
X_val_encoded = encoder.transform(X_val)

imputer = SimpleImputer()
X_train_imputed = imputer.fit_transform(X_train_encoded)
X_val_imputed = imputer.transform(X_val_encoded)

scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train_imputed)
X_val_scaled = scaler.transform(X_val_imputed)

model = LogisticRegressionCV(cv=5, n_jobs=-1, random_state=42)
model.fit(X_train_scaled, y_train)

print('Validation Accuracy', model.score(X_val_scaled, y_val))

# + id="8cS8Cim2vVR8" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 445} outputId="5326051b-3607-47d3-8763-10dc369b0285"
X_train[features]

# + id="qHT7gvC1ntaL" colab_type="code" colab={}
features = ['Volume','Tortilla','Temp','Meat','Fillings',
           'Meat:filling','Uniformity','Salsa','Synergy','Wrap']

X_train = train[features]

imputer = SimpleImputer()
X_train_imputed = imputer.fit_transform(X_train)

scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train_imputed)

model = LogisticRegressionCV(cv=5, n_jobs=-1, random_state=42)
model.fit(X_train_scaled, y_train);       

# + id="hq6S3_WLpzfw" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 35} outputId="e9672c24-6e41-4431-8913-0aa8acd6be9a"
X_test = test[features]
y_test = test[target]
X_test_imputed = imputer.transform(X_test)
X_test_scaled = scaler.transform(X_test_imputed)
print('Test Accuracy', model.score(X_test_scaled, y_test))

# + id="spyJfdmFq-FJ" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 311} outputId="b8ab3442-fa1d-450e-f025-86809122ecb2"
# %matplotlib inline
import matplotlib
matplotlib.style.use('ggplot')
coefficients = pd.Series(model.coef_[0], features)
coefficients.sort_values().plot.bar();

# + id="TNthoOy2oF-t" colab_type="code" colab={}


StarcoderdataJupyterScriptsDedupFiltered