<jupyter_start><jupyter_text><jupyter_code># Installs
%%capture
!pip install pmdarima
# Imports
import numpy as np
import pandas as pd
from statsmodels.tsa.arima_model import ARIMA
import pmdarima as pm
from sklearn import preprocessing
# Import data
original_df = pd.read_csv('https://raw.githubusercontent.com/Lambda-School-Labs/nfl-fantasy-ds/master/data/actuals/actuals_defense.csv')
player_df = pd.read_csv('https://raw.githubusercontent.com/Lambda-School-Labs/nfl-fantasy-ds/master/data/raw/players.csv')
bye_df = pd.read_csv('https://raw.githubusercontent.com/Lambda-School-Labs/nfl-fantasy-ds/master/data/raw/bye.csv')
# The dataframe of actual defense points for each game from 2000-2019
original_df.head()
# The dataframe of all 2019 players
player_df.head()
# The dataframe of bye weeks for 2019 teams
bye_df.head()
# Add a row to the final_df dataframe
# Each row represents the predicted points for each team
def add_row(df, p, f, l, n, pos, wc, wp, wa, cur, pred, act):

    df = df.append({'player': p,
                    'first': f,
                    'last': l,
                    'name': n,
                    'position': pos,
                    wc: cur,
                    wp: pred,
                    wa: act
                    }, ignore_index=True)
    
    return df
# The main code for iterating through the player(defense) list, calculating the points and adding the rows
# to the final_df dataframe.
def main(w):

    # Create the column names
    week_cname = 'week' + str(w) + '-cur'
    week_pname = 'week' + str(w) + '-pred'
    week_aname = 'week' + str(w) + '-act'

    column_names = ['player',
                    'first',
                    'last',
                    'name',
                    'position',
                    week_cname, 
                    week_pname,
                    week_aname
                     ]

    defense_df = player_df.loc[player_df['position1'] == 'DF']
    player_list = defense_df['player'].tolist()

    # The row is the current row of the dataframe (0)
    # The column is the column of the dataframe (309 = 2019-game1)
    row = 0
    col = 308 + w

    week_df = pd.DataFrame(columns = column_names)

    for player in player_list:

        first = player_df['first'].loc[(player_df['player']==player)].iloc[0]
        last = player_df['last'].loc[(player_df['player']==player)].iloc[0]
        name = player_df['name'].loc[(player_df['player']==player)].iloc[0]
        position1 = player_df['position1'].loc[(player_df['player']==player)].iloc[0]

        bye_week = bye_df['bye-week'].loc[(bye_df['player']==player)].iloc[0]

        # Handle bye week
        if w > bye_week:
            new_col = col - 1
        else:
            new_col = col

        # Handle Houston (started in 2002)
        if player == 'HOU':
            train_data = original_df.iloc[row, 37:new_col]
        else:
            train_data = original_df.iloc[row, 5:new_col]

        # Calculate current points
        if w == 1:
            current = original_df.iloc[row, 293:309]
        else:
            current = original_df.iloc[row, 309:new_col]
        cur_points = current.sum()

        # Calculate actual points
        actuals = original_df.iloc[row, new_col:325]
        act_points = actuals.sum()
        row = row + 1

        # ARIMA model
        model = pm.auto_arima(train_data, start_p=1, start_q=1,
                          test='adf',       # use adftest to find optimal 'd'
                          max_p=3, max_q=3, # maximum p and q
                          m=1,              # frequency of series
                          d=None,           # let model determine 'd'
                          seasonal=False,   # No Seasonality
                          start_P=0, 
                          D=0, 
                          trace=False,
                          error_action='ignore',  
                          suppress_warnings=True, 
                          stepwise=True)
    
        # Forecast
        if w > bye_week:
            n_periods = 17 - w + 1
        else:
            n_periods = 17 - w
 
        # Calculate predicted points
        fc = model.predict(n_periods=n_periods, return_conf_int=False)
        index_of_fc = np.arange(len(train_data), len(train_data)+n_periods)
        fc_series = pd.Series(fc, index=index_of_fc)
        pred_points = fc_series.sum()

        week_df = add_row(week_df, player, first, last, name, position1, week_cname, 
                          week_pname, week_aname, cur_points, pred_points, act_points)
    return week_df
# Run for seasons 1 through 17
for i in range(1, 18):
    week = i
    final_df = main(week)

    week_pred = 'week' + str(week) + '-pred'
    week_act = 'week' + str(week) + '-act'
    week_diff = 'week' + str(week) + '-diff'
    week_pct = 'week' + str(week) + '-pct'

    final_df[week_diff] = final_df[week_pred] - final_df[week_act]
    final_df[week_pct] = final_df[week_diff] / final_df[week_pred]
    final_df[week_pred] = final_df[week_pred].astype(int)

    file_name = '/content/week' + str(week) + '-pred-defense.csv'
    final_df.to_csv(file_name, index=False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11396561715279119, 'fraction_numerical': 0.015066640911724938, 'mean_word_length': 1.9878822850548183, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 3, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '453733', 'license': 'MIT', 'n_tokens_mistral': 1754, 'n_tokens_neox': 1626, 'n_words': 382, 'path': 'predictions-final/Defense_Predictions_All.ipynb', 'repo_name': 'JimKing100/nfl-test', 'size': '5177'}