<jupyter_start><jupyter_text>Baseline models and Feature Selection<jupyter_code>import os
import glob
import warnings
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

from tqdm import tqdm_notebook as tqdm
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import MinMaxScaler

import utils
from constants import DATA_DIR
np.random.seed(23)
warnings.filterwarnings("ignore")<jupyter_output><empty_output><jupyter_text>Simple Season AverageBaseline model - DraftKings shows season average in their data and interface<jupyter_code>df_baseline = utils.csv_concatenate(os.path.join(DATA_DIR, 'Dataframes','Modelling', 'Baseline'))
df_baseline.sort_values(by=['Date','Name']).reset_index(drop=True, inplace=True)
df_baseline['FPTS_pred'] = utils.calculate_FPTS(df_baseline)

# Season average
print(' MAE | ', utils.calculate_MAE(df_baseline['FPTS_pred'], df_baseline['FPTS']))
print('RMSE | ', utils.calculate_RMSE(df_baseline['FPTS_pred'], df_baseline['FPTS']))
# Linear Regression with basic 9 variables counted for fantasy points
df_baseline = df_baseline
basic =  ['PTS','3P','AST','TRB','STL','BLK','TOV', 'DD', 'TD']

X = df_baseline.loc[:, basic]
X = MinMaxScaler().fit_transform(X)
y = df_baseline['FPTS'].values.reshape(-1,1).flatten()

reg = LinearRegression()
errors = utils.cross_val(reg, X, y, n_folds=5, verbose=0)
utils.summarize_errors(errors)<jupyter_output><empty_output><jupyter_text>Weighted FeaturesChoose weighting scheme from linear, square root and quadratic<jupyter_code>features = ['SG', 'F', 'C', 'PTS', '3P', 'AST', 'TRB', 'STL', 'BLK', 'TOV', 'DD', 'TD', 'MP', 'FT',
            'FTA', 'FGA', '3PA', 'DRB', 'ORB', 'USG_perc', 'DRtg', 'ORtg', 'AST_perc', 'DRB_perc',
            'ORB_perc', 'BLK_perc', 'TOV_perc', 'STL_perc', 'eFG_perc', 'FG_perc', '3P_perc', 'FT_perc']
for weighting in ['sqrt', 'linear', 'quad']:
    print("\nweighting sheme:", weighting)
    df_features = utils.csv_concatenate(os.path.join(DATA_DIR, 'Dataframes','Modelling', 'Features', weighting))  
    
    X = df_features.loc[:, features]
    X = MinMaxScaler().fit_transform(X)
    y = df_features['FPTS'].values.reshape(-1,1).flatten()

    reg = LinearRegression()
    errors = utils.cross_val(reg, X, y, n_folds=5, verbose=0)
    utils.summarize_errors(errors, verbose=0)<jupyter_output>weighting sheme: sqrt<jupyter_text>Feature Selection with Feature Importances<jupyter_code>weighting = 'quad'
df_features = utils.csv_concatenate(os.path.join(DATA_DIR, 'Dataframes','Modelling', 'Features', weighting))  
_all = ['Salary', 'Rest', 'Rota_All', 'Rota_Pos', 'Home', 'SG', 'F', 'C', 'Value', 'FPTS_std',
        'PTS', '3P', 'AST', 'TRB', 'STL', 'BLK', 'TOV', 'DD', 'TD', 'MP', 'FT', 'FTA', 'FGA', '3PA', 'DRB',
        'ORB', 'USG_perc', 'DRtg', 'ORtg', 'AST_perc', 'DRB_perc', 'ORB_perc', 'BLK_perc', 'TOV_perc', 
        'STL_perc', 'eFG_perc', 'FG_perc', '3P_perc', 'FT_perc']
X = df_features.loc[:, _all]
X = MinMaxScaler().fit_transform(X)
y = df_features['FPTS'].values.reshape(-1,1).flatten()

# Takes 2 minutes
model = GradientBoostingRegressor()
model.fit(X, y)

top_features = pd.Series(model.feature_importances_, index = _all).sort_values()
top_features.plot(kind = "barh", figsize=(15,10) ,title='Top Features')
plt.show()<jupyter_output><empty_output><jupyter_text>Linear Regression with All / Selected Features<jupyter_code>omit_lowest = 20
_selected = list(top_features[omit_lowest:].index)
for feature_type in [_all, _selected]:
    for weighting in ['sqrt', 'linear', 'quad']:
        df_features = utils.csv_concatenate(os.path.join(DATA_DIR, 'Dataframes','Modelling',
                                                         'Features', weighting))
        print("\nweighting sheme: {}".format(weighting))

        X = df_features.loc[:, feature_type]
        X = MinMaxScaler().fit_transform(X)
        y = df_features['FPTS'].values.reshape(-1,1).flatten()
        
        reg = LinearRegression()

        errors = utils.cross_val(reg, X, y, verbose=0)
        utils.summarize_errors(errors, verbose=0)
        
    print("========================")<jupyter_output>weighting sheme: sqrt
{'fraction_non_alphanumeric': 0.16828632377340602, 'fraction_numerical': 0.007584735719364778, 'mean_word_length': 5.403641881638847, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '597195', 'license': 'MIT', 'n_tokens_mistral': 1580, 'n_tokens_neox': 1530, 'n_words': 302, 'path': 'src/05.baseline_models.ipynb', 'repo_name': 'ozgurhepsag/fantasy-basketball', 'size': '4219'}