<jupyter_start><jupyter_text>_Lambda School Data Science, Unit 2_  Sprint Challenge: Predict Steph Curry's shots 🏀For your Sprint Challenge, you'll use a dataset with all Steph Curry's NBA field goal attempts. (Regular season and playoff games, from October 28, 2009, through June 5, 2019.) You'll predict whether each shot was made, using information about the shot and the game. This is hard to predict! Try to get above 60% accuracy. The dataset was collected with the [nba_api](https://github.com/swar/nba_api) Python library.<jupyter_code>import sys
in_colab = 'google.colab' in sys.modules

if in_colab:
    # Install packages in Colab
    !pip install category_encoders==2.0.0
    !pip install pandas-profiling==2.3.0
    !pip install plotly==4.1.1
import pandas as pd
# Read data
url = 'https://drive.google.com/uc?export=download&id=1fL7KPyxgGYfQDsuJoBWHIWwCAf-HTFpX'
df = pd.read_csv('https://drive.google.com/uc?export=download&id=1fL7KPyxgGYfQDsuJoBWHIWwCAf-HTFpX')

# Check data shape
assert df.shape == (13958, 20)
df.head(5)<jupyter_output><empty_output><jupyter_text>To demonstrate mastery on your Sprint Challenge, do all the required, numbered instructions in this notebook.To earn a score of "3", also do all the stretch goals.You are permitted and encouraged to do as much data exploration as you want.**1. Begin with baselines for classification.** Your target to predict is `shot_made_flag`. What is your baseline accuracy, if you guessed the majority class for every prediction?**2. Hold out your test set.** Use the 2018-19 season to test. NBA seasons begin in October and end in June. You'll know you've split the data correctly when your test set has 1,709 observations.**3. Engineer new feature.** Engineer at least **1** new feature, from this list, or your own idea.- **Homecourt Advantage**: Is the home team (`htm`) the Golden State Warriors (`GSW`) ?- **Opponent**: Who is the other team playing the Golden State Warriors?- **Seconds remaining in the period**: Combine minutes remaining with seconds remaining, to get the total number of seconds remaining in the period.- **Seconds remaining in the game**: Combine period, and seconds remaining in the period, to get the total number of seconds remaining in the game. A basketball game has 4 periods, each 12 minutes long.- **Made previous shot**: Was <NAME>'s previous shot successful?**4. Decide how to validate** your model. Choose one of the following options. Any of these options are good. You are not graded on which you choose.- **Train/validate/test split: train on the 2009-10 season through 2016-17 season, validate with the 2017-18 season.** You'll know you've split the data correctly when your train set has 11,081 observations, and your validation set has 1,168 observations.- **Train/validate/test split: random 80/20%** train/validate split.- **Cross-validation** with independent test set. You may use any scikit-learn cross-validation method.**5.** Use a scikit-learn **pipeline** to **encode categoricals** and fit a **Decision Tree** or **Random Forest** model.**6.** Get your model's **validation accuracy.** (Multiple times if you try multiple iterations.) **7.** Get your model's **test accuracy.** (One time, at the end.)**8.** Given a **confusion matrix** for a hypothetical binary classification model, **calculate accuracy, precision, and recall.** Stretch Goals- Engineer 4+ new features total, either from the list above, or your own ideas.- Make 2+ visualizations to explore relationships between features and target.- Optimize 3+ hyperparameters by trying 10+ "candidates" (possible combinations of hyperparameters). You can use `RandomizedSearchCV` or do it manually.- Get and plot your model's feature importances.  1. Begin with baselines for classification. >Your target to predict is `shot_made_flag`. What would your baseline accuracy be, if you guessed the majority class for every prediction?<jupyter_code>y_train = df['shot_made_flag']
y_train.value_counts(normalize=True)
majority_class = y_train.mode()[0]
y_pred = [majority_class] * len(y_train)
print(len(y_pred)) 
#Accuracy of majority class baseline = frequency of the majority class
from sklearn.metrics import accuracy_score
accuracy_score(y_train, y_pred)<jupyter_output><empty_output><jupyter_text>2. Hold out your test set.>Use the 2018-19 season to test. NBA seasons begin in October and end in June. You'll know you've split the data correctly when your test set has 1,709 observations.<jupyter_code># Convert to datetime and look at the date range
df['game_date'] = pd.to_datetime(df['game_date'], infer_datetime_format=True)

cutoff = pd.to_datetime('2018-06-30')
train = df[df.game_date < cutoff]
test = df[df.game_date >= cutoff]
train.shape, test.shape<jupyter_output><empty_output><jupyter_text>3. Engineer new feature.>Engineer at least **1** new feature, from this list, or your own idea.>>- **Homecourt Advantage**: Is the home team (`htm`) the Golden State Warriors (`GSW`) ?>- **Opponent**: Who is the other team playing the Golden State Warriors?>- **Seconds remaining in the period**: Combine minutes remaining with seconds remaining, to get the total number of seconds remaining in the period.>- **Seconds remaining in the game**: Combine period, and seconds remaining in the period, to get the total number of seconds remaining in the game. A basketball game has 4 periods, each 12 minutes long.>- **Made previous shot**: Was <NAME>'s previous shot successful?<jupyter_code>df.head()
df['new_seconds'] = (df.period)*720 + df.seconds_remaining
df.head()<jupyter_output><empty_output><jupyter_text>**4. Decide how to validate** your model. >Choose one of the following options. Any of these options are good. You are not graded on which you choose.>>- **Train/validate/test split: train on the 2009-10 season through 2016-17 season, validate with the 2017-18 season.** You'll know you've split the data correctly when your train set has 11,081 observations, and your validation set has 1,168 observations.>- **Train/validate/test split: random 80/20%** train/validate split.>- **Cross-validation** with independent test set. You may use any scikit-learn cross-validation method.<jupyter_code>from sklearn.model_selection import train_test_split
X = df.drop(['shot_made_flag', 'game_date'], axis=1)
Y = df.shot_made_flag
X_train = X
y_train = df.shot_made_flag

X_train.shape, y_train.shape
X_train, X_val, y_train, y_val = train_test_split(
    X_train, y_train, train_size = 0.80, test_size = 0.20,
    stratify = y_train, random_state=42
)

X_train.shape, X_val.shape, y_train.shape, y_val.shape<jupyter_output><empty_output><jupyter_text>5. Use a scikit-learn pipeline to encode categoricals and fit a Decision Tree or Random Forest model.<jupyter_code>from sklearn.tree import DecisionTreeClassifier

dt = make_pipeline(
    ce.OneHotEncoder(use_cat_names=True), 
    SimpleImputer(), 
    DecisionTreeClassifier(random_state=42)
)


dt.fit(X_train, y_train)
score = dt.score(X_val, y_val)
print('Decision Tree, Validation Accuracy', score)
import category_encoders as ce
from sklearn.impute import SimpleImputer
from sklearn.ensemble import RandomForestClassifier
from sklearn.pipeline import make_pipeline

pipeline = make_pipeline(
   ce.OneHotEncoder(use_cat_names='True'),
   SimpleImputer(strategy='median'),
   RandomForestClassifier(n_estimators=60, random_state=42, n_jobs=-1) 
)


pipeline.fit(X_train, y_train)
print ('Validation Accuracy', pipeline.score(X_val, y_val))


y_pred = pipeline.predict(X_val)
y_pred<jupyter_output><empty_output><jupyter_text>6.Get your model's validation accuracy> (Multiple times if you try multiple iterations.)<jupyter_code>from sklearn.metrics import accuracy_score
model = dt.named_steps['decisiontreeclassifier']


y_pred = pipeline.predict(X_val)
accuracy_score(y_val, y_pred)<jupyter_output><empty_output><jupyter_text>7. Get your model's test accuracy> (One time, at the end.)<jupyter_code>]<jupyter_output><empty_output><jupyter_text>8. Given a confusion matrix, calculate accuracy, precision, and recall.Imagine this is the confusion matrix for a binary classification model. Use the confusion matrix to calculate the model's accuracy, precision, and recall.          Predicted        Negative    Positive        Actual    Negative    85    58        Positive    8     36    Calculate accuracy<jupyter_code>correct_pred = 85 + 36
total_pred = 85 + 58 + 8 + 36
accuracy = correct_pred / total_pred
print(accuracy)<jupyter_output>0.6470588235294118<jupyter_text>Calculate precision<jupyter_code>correct_pred = 36
total_pred = 58 + 36
precision = correct_pred / total_pred
print(precision)<jupyter_output>0.3829787234042553<jupyter_text>Calculate recall<jupyter_code>correct_pred = 36
actual = 8 + 36
recall = correct_pred / actual
print(recall)<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup