<jupyter_start><jupyter_text>Cost Benefit Questions1. How would you rephrase the business problem if your model was optimizing toward _precision_? i.e., How might the model behave differently, and what effect would it have?2. How would you rephrase the business problem if your model was optimizing toward _recall_?3. What would the most ideal model look like in this case?<jupyter_code>import pandas as pd
import sklearn.linear_model as lm
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
from sklearn.linear_model import LogisticRegression

titanic = pd.read_csv('titanic.csv')
titanic.set_index('PassengerId', inplace=True)
titanic = titanic.join(pd.get_dummies(titanic.Pclass))
titanic['is_male'] = titanic.Sex.apply(lambda x: 1 if x == 'male' else 0)

titanic.groupby('Survived').Age.hist()
titanic['Age'] = titanic.groupby(["Sex", 'Pclass']).Age.transform(lambda x: x.fillna(x.mean()))
titanic['had_parents'] = titanic.Parch.apply(lambda x: 1 if x > 0 else 0)
titanic['had_siblings'] = titanic.SibSp.apply(lambda x: 1 if x > 0 else 0)
from sklearn import grid_search, cross_validation

feature_set = titanic[['is_male', 1, 2, 'Fare', 'Age', 'had_parents', 'had_siblings']]
cv=cross_validation.KFold(n=len(titanic), n_folds=10)
gs = grid_search.GridSearchCV(
    estimator=LogisticRegression(),
    param_grid={'C': [10**-i for i in range(-5, 5)], 'class_weight': [None, 'balanced']},
    cv=cv,
    scoring='roc_auc'
)


gs.fit(feature_set, titanic.Survived)
print(gs.best_estimator_)
titanic['prob'] = gs.predict_proba(feature_set).T[1]
titanic['predict'] = gs.predict(feature_set)
from sklearn.metrics import roc_curve, roc_auc_score
import matplotlib.pyplot as plt

mod = gs.best_estimator_

actuals = mod.predict(feature_set) 
probas = mod.predict_proba(feature_set)
plt.plot(roc_curve(titanic[['Survived']], probas[:,1])[0], roc_curve(titanic[['Survived']], probas[:,1])[1])
for c in [10**-i for i in range(-5, 5)]:
    for class_weight in [None, 'balanced']:
        gs_ind = grid_search.GridSearchCV(
        estimator=LogisticRegression(),
        cv=cv,
        param_grid={'C': [c], 'class_weight': [class_weight]},
        scoring='roc_auc')
        
        gs_ind.fit(feature_set, titanic.Survived)
        actuals = gs_ind.predict(feature_set) 
        probas = gs_ind.predict_proba(feature_set)
        plt.plot(roc_curve(titanic[['Survived']], probas[:,1])[0], roc_curve(titanic[['Survived']], probas[:,1])[1])
roc_auc_score(titanic['Survived'], mod.predict_proba(feature_set)[:,1])
import numpy as np
score = []
for train, test in cv:
    score.append(roc_auc_score(titanic.Survived.iloc[test], mod.predict_proba(feature_set.iloc[test])[:,1]))
print(np.mean(score))<jupyter_output><empty_output><jupyter_text>Visualizing models over variables<jupyter_code>%matplotlib inline
import pandas as pd
import sklearn.linear_model as lm
import matplotlib.pyplot as plt
import seaborn as sns

df = pd.read_csv('flight_delays.csv')
df = df.loc[df.DEP_DEL15.notnull()].copy()
df.head()
df = df[df.DEP_DEL15.notnull()]
df = df.join(pd.get_dummies(df['CARRIER'], prefix='carrier'))
df = df.join(pd.get_dummies(df['DAY_OF_WEEK'], prefix='dow'))
model = lm.LogisticRegression()
features = [i for i in df.columns if 'dow_' in i]
df.shape
features += ['CRS_DEP_TIME']
model.fit(df[features[1:]], df['DEP_DEL15'])

df['probability'] = model.predict_proba(df[features[1:]]).T[1]
ax = plt.subplot(111)
colors = ['blue', 'green', 'red', 'purple', 'orange', 'brown']
for e, c in enumerate(colors):
    df[df[features[e]] == 1].plot(x='CRS_DEP_TIME', y='probability', kind='scatter', color = c, ax=ax, label=features[e])

ax.set(title='Probability of Delay\n Based on Day of Week and Time of Day')<jupyter_output><empty_output><jupyter_text>Other Answers: visualizing Airline or the inverse<jupyter_code>features2 = [i for i in df.columns if 'carrier_' in i]
features2 += ['CRS_DEP_TIME']

model.fit(df[features2[1:]], df['DEP_DEL15'])
df['probability'] = model.predict_proba(df[features2[1:]]).T[1]

features2
ax = plt.subplot(111)

c=['blue','red','yellow','green','purple','orange','grey','black','pink']

for e, c in enumerate(c):
    df[df[features2[e]] == 1].plot(x='CRS_DEP_TIME'
                                   , y='probability'
                                   , kind='scatter'
                                   , ax=ax
                                   , label=features2[e]
                                   , color = c)

ax.set(title='Probability of Delay\n Based on Carrier')

#loops until it runs out of colours
df['inverse_probability'] = model.predict_proba(df[features2[1:]]).T[0]

c=['blue','red','yellow','green','purple','orange','grey','black','pink']

ax = plt.subplot(111)

for e, c in enumerate(c):
    df[df[features2[e]] == 1].plot(x='CRS_DEP_TIME'
                                   , y='inverse_probability'
                                   , kind='scatter'
                                   , ax=ax
                                   , color=c
                                   , label=features2[e])

ax.set(title='Probability of No Delay\n Based on Carrier')<jupyter_output><empty_output><jupyter_text>Visualizing Performance Against Baseline  Visualizing AUC and comparing Models<jupyter_code>from sklearn import dummy, metrics
model0 = dummy.DummyClassifier()
model0.fit(df[features[1:]], df['DEP_DEL15'])
df['probability_0'] = model0.predict_proba(df[features[1:]]).T[1]

model1 = lm.LogisticRegression()
model.fit(df[features[1:]], df['DEP_DEL15'])
df['probability_1'] = model.predict_proba(df[features[1:]]).T[1]

df.shape
ax = plt.subplot(111)
vals = metrics.roc_curve(df.DEP_DEL15, df.probability_0)
ax.plot(vals[0], vals[1])
vals = metrics.roc_curve(df.DEP_DEL15, df.probability_1)
ax.plot(vals[0], vals[1])

ax.set(title='Area Under the Curve for prediction delayed=1', ylabel='', xlabel='', xlim=(0, 1), ylim=(0, 1))
metrics.roc_curve(df.DEP_DEL15,df.probability_0)<jupyter_output><empty_output><jupyter_text>Visualizing Precision / Recall<jupyter_code>metrics.precision_recall_curve(df.DEP_DEL15,df['probability_0'])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1551780463900686, 'fraction_numerical': 0.01829467494282914, 'mean_word_length': 4.511251125112511, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '573260', 'license': 'MIT', 'n_tokens_mistral': 2341, 'n_tokens_neox': 2214, 'n_words': 433, 'path': 'general_assembly/10_communicating_results/starter-code-10.ipynb', 'repo_name': 'zoealdington/data-science', 'size': '6122'}