# ---
# 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
# ---

# # Phase 3 Code Challenge Review
#
# Agenda:
# - Gradient Descent & Cost Function
# - Logistic Regression
# - Evaluation Metrics
# - Decision Trees

# ## Part I. Gradient Descent & Cost Function
# - What is a cost function? What are we trying to find?
# - How to use gd to find the lowest point? How does the gradient change as we get closer to the bottom?
# - What's the role learning rate play? How can learning rate affect your result? 
#
#
# <p style='text-align:center;font-size:20px'>$ \theta_j := \theta_j - \alpha * \frac{\partial J(\theta)}{\partial\theta_i} $</p>

# +
import matplotlib.pyplot as plt
import numpy as np
from sklearn.metrics import mean_squared_error
import seaborn as sns
# %matplotlib inline
x = [1, 1, 2, 3, 4, 3, 4, 6, 4]
y = [2, 1, 0.5, 1, 3, 3, 2, 5, 4]
plt.figure(figsize = (8,8))
fig, ax = plt.subplots()

ax.scatter(x,y)
plt.show()
# -

beta_0 = 0
#beta_1 = [.25, .5, .75, .8, 1,]
beta_1 = np.arange(0.25,3,0.1)
fig, ax = plt.subplots(figsize=(10,6))
mses = []
for t in beta_1:
    line = beta_0 + (np.array(x)*t)
    mse = round(mean_squared_error(y, line),3)
    mses.append(mse)
    ax.plot(x, line, label=f'{mse} {t}')
ax.scatter(x,y)
plt.legend()
plt.show()

# Plot the Cost Curve
fig, ax = plt.subplots(figsize=(10,6))
ax.plot(beta_1, mses)
ax.set_title('Cost Curve')
ax.set_xlabel('beta 1')
ax.set_ylabel('MSE')
plt.show()

# #### For gradient descent, the questions are going to be mostly intuitive and written answers. You will need to be able to answer questions such as the 3 bullet points above.

# ## Part II. Logistic Regression 
# - How does linear regression differ from logistic regression?
# - Why is logistic regression better at modeling a binary outcome?
# - What are some advantages and disadvantages of logistic regression?

# ## Part III. Evaluation Metrics 
# - What are precision and recall?
# - How to evaluate a logistic regression model?
# - What is roc auc curve?
# - What is class imbalance and how do we deal with it?

# <img src = 'confusion_matrix.png' width = 300>

### calculate precision here


### calculate recall here


### calculate F1 score here


# **Explain which model below has the best performance based on ROC-AUC curve? Why?**

# <img src='roc_auc.png' width = 400>

# ### Class Imbalance
# <img src = 'imbalanced.png' wid = 300>

# +
### what problem would it cause? 

# +
### How to remedy it?
# -

# ### Solution 1 - Resampling
# <img src = 'resampling.png'>

# #### Solution 2 - Smote
# <img src = 'smote.png'>

# #### Solution 3 - Tomek Link 
# <img src = 'tomek.png'>

# ## Part III. Decision Trees
# - Build trees with the sklearn machine learning framework

# import the dataset and set up predictors and target
titanic = sns.load_dataset('titanic')

# define x and y 
y = titanic['survived']
X = titanic[['pclass', 'sex', 'age', 'sibsp', 'parch', 'fare',
     'adult_male']]

# fill the age columns missing value with mean 


# Train test split
from sklearn.model_selection import train_test_split


# fit the tree
from sklearn.tree import DecisionTreeClassifier


# test the tree 


# generate prediction and output metric (use accuracy)
from sklearn.metrics import accuracy_score
acc = accuracy_score(y_test, pred)
print(acc)

# +
# how did our tree do? did it perform well?

## hint: think about baseline model

# -



StarcoderdataJupyterScriptsDedupFiltered