<jupyter_start><jupyter_text>Lambda School Data Science*Unit 2, Sprint 1, Module 4*---  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. 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).<jupyter_code>%%capture
import sys

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

# If you're working locally:
else:
    DATA_PATH = '../data/'
# Load data downloaded from https://srcole.github.io/100burritos/
import pandas as pd
df = pd.read_csv(DATA_PATH+'burritos/burritos.csv')
# 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
# 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'
# Drop some high cardinality categoricals
df = df.drop(columns=['Notes', 'Location', 'Reviewer', 'Address', 'URL', 'Neighborhood'])
# Drop some columns to prevent "leakage"
df = df.drop(columns=['Rec', 'overall'])<jupyter_output><empty_output><jupyter_text>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.<jupyter_code>#My code starts here
df.Date[0]
df['Date'] = pd.to_datetime(df['Date'])
df['Date'].dtype
df['year'] = pd.to_datetime(df['Date']).dt.to_period('Y')
df[['Date', 'year']].head(2)
df_train = df[(df['year']<='2016')]
print(df_train.shape)
print(df.shape)
df_train.head(2)
df_validate = df[df['year']=='2017']
print(df_validate.shape)
print(df.shape)
df_validate.head(2)
test_df= df[df['year']>='2018']
print(test_df.shape)
print(df.shape)
test_df.head(2)
#Lets drop the date columns and only leave the Year
df_train = df_train.drop(columns=['Date'])
df_validate = df_validate.drop(columns=['Date'])
test_df = test_df.drop(columns=['Date'])
#Drop the target from the test_df
test_df = test_df.drop(columns=['Great'])
print(df.shape)
print(df_train.shape)
print(df_validate.shape)
print(test_df.shape)
test_df.head(2)<jupyter_output>(421, 60)
(298, 59)
(85, 59)
(38, 58)<jupyter_text>**Begin with baselines for classification.**<jupyter_code># how many people in our training dataset actually survived the voyage?
target = 'Great'
y_train = df_train[target]
y_train.value_counts(normalize=True)
import seaborn as sns
sns.countplot(y_train);
#Whats the most frequent value
majority_class = y_train.mode()
majority_class
#List of zeros the size of the df
y_pred = [majority_class]* len(y_train)
print(y_train.shape)
len(y_pred)
from sklearn.metrics import accuracy_score
print('Training accuracy of majority class baseline')
print(accuracy_score(y_train, y_pred))

print('\nValidation accuracy of majority class baseline')
y_val= df_validate[target]
y_Vpred = [majority_class]*len(y_val)
print(accuracy_score(y_val, y_Vpred))<jupyter_output><empty_output><jupyter_text>**Use scikit-learn for logistic regression.**<jupyter_code>df.columns
features = ['Tortilla', 'Temp', 'Meat', 'Salsa', 'Guac', 'Pico']
target='Great'

X_Train = df_train[features]
y_train= df_train[target]
X_val = df_validate[features]
y_val = df_validate[target]

X_Train.shape, y_train.shape, X_val.shape, y_val.shape
print(X_Train.isnull().sum())
print(X_val.isnull().sum())
print(X_Train['Temp'].value_counts(ascending=False).head(1))
# X_Train['Temp'].fillna(4.0, inplace=True)
print(X_Train['Meat'].value_counts(ascending=False).head(1))
# X_Train['Meat'].fillna(4.0, inplace=True)
print(X_Train['Salsa'].value_counts(ascending=False).head(1))
# X_Train['Salsa'].fillna(3.0, inplace=True)
print(X_Train['Guac'].value_counts(ascending=False).head(1))
# X_Train['Guac'].fillna('x', inplace=True)
print(X_Train['Pico'].value_counts(ascending=False).head(2))
# X_Train['Pico'].fillna('x', inplace=True)
print(X_Train.isnull().sum())
print(X_val.isnull().sum())
import category_encoders as ce
from sklearn.impute import SimpleImputer
from sklearn.linear_model import LogisticRegressionCV
from sklearn.preprocessing import StandardScaler
X_Train.describe(include='all')
#Convert categorical values
encoder = ce.OneHotEncoder(use_cat_names=True)
X_train_encoded = encoder.fit_transform(X_Train)

X_val_encoded = encoder.fit_transform(X_val)
print(X_train_encoded.head(2))
X_train_encoded.isnull().sum()
#lets input the missing data with the mean

imputer = SimpleImputer(strategy='mean')
X_train_imputed = imputer.fit_transform(X_train_encoded)
#Do the same to Validation but do not fit it
X_val_imputed = imputer.transform(X_val_encoded)

#Scale the data

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

#same to the validation data only traqnsform
X_val_scaled = scaler.transform(X_val_imputed)
pd.DataFrame(X_val_scaled).head()
#Fit the model using the transformed training dataset
model = LogisticRegressionCV()
model.fit(X_train_scaled, y_train)
#Make predictions
y_pred = model.predict(X_val_scaled)
#Validation Accuracy
print('validation Accuracy: ', accuracy_score(y_val, y_pred))
coefficients = pd.DataFrame(model.coef_[0], X_train_encoded.columns, columns=['probabilities'])
print(coefficients)
import numpy as np
odds_ratios = np.exp(model.coef_)
coefficients['odd_ratios']= odds_ratios[0]
print(coefficients)
coefficients= pd.Series(model.coef_[0], X_train_encoded.columns)
coefficients.sort_values().plot.barh();<jupyter_output>probabilities  odd_ratios
Tortilla       0.641584    1.899487
Temp           0.469464    1.599138
Meat           1.545594    4.690756
Salsa          0.571020    1.770072
Guac_x         0.358517    1.431206
Guac_nan      -0.190677    0.826399
Guac_X        -0.223590    0.799643
Pico_x        -0.201505    0.817499
Pico_nan       0.252125    1.286757
Pico_X        -0.095506    0.908913
{'fraction_non_alphanumeric': 0.1374652363925308, 'fraction_numerical': 0.03098927294398093, 'mean_word_length': 5.797479747974798, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 18, 'https://': 5, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '629636', 'license': 'MIT', 'n_tokens_mistral': 2820, 'n_tokens_neox': 2604, 'n_words': 630, 'path': 'LS_DS_214_assignment.ipynb', 'repo_name': 'yestrella14/DS-Unit-2-Linear-Models', 'size': '7551'}