<jupyter_start><jupyter_text>TUNING INFO hyper_parameters from here https://machinelearningmastery.com/hyperparameters-for-classification-machine-learning-algorithms/for xgboost from here https://machinelearningmastery.com/extreme-gradient-boosting-ensemble-in-python/xgbtrees = [10, 50, 100, 500, 1000, 5000]  100  num of treesmax_depth = range(1,11)  3-5rates = [0.0001, 0.001, 0.01, 0.1, 1.0]  0.1subsample in arange(0.1, 1.1, 0.1):  0.4, 0.5  this is 0.1, 0.2 ... 1.0  % of features to samplesvc kernels in [‘linear’, ‘poly’, ‘rbf’, ‘sigmoid’] if you use poly, then adjust degreeC in [100, 10, 1.0, 0.1, 0.001]gblearning_rate in [0.001, 0.01, 0.1]n_estimators [10, 100, 1000]subsample in [0.5, 0.7, 1.0]max_depth in [3, 7, 9]rfcmax_features [1 to 20]  keymax_features in [‘sqrt’, ‘log2’]n_estimators in [10, 100, 1000]bcn_estimators in [10, 100, 1000]svm_dic = {'kernels':[‘linear’, ‘poly’, ‘rbf’, ‘sigmoid’]}lrc_dic = {'alpha': [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]}lgr_hp_dic = {'solver': [‘newton-cg’, ‘lbfgs’, ‘liblinear’, ‘sag’, ‘saga’], 'penalty' : [‘none’, ‘l1’, ‘l2’, ‘elasticnet’],'C' :[100, 10, 1.0, 0.1, 0.01]}<jupyter_code>X = pd.read_csv('/Users/joejohns/data_bootcamp/GitHub/final_project_nhl_prediction/Data/Shaped_Data/data_LJ.csv')

X['season'] = X['season'].apply(int)
X['game_id'] = X['game_id'].apply(int)
X['mp_date'] = X['mp_date'].apply(int)
X['season'].value_counts()
y_cols = ['Unnamed: 0', 'game_id', 'mp_date', 'season', 'home_team', 'away_team',
       'home_odds', 'away_odds', 'home_goals', 'away_goals', 'home_win',
       'settled_in', ]

x_cols = ['CF%', 'CSh%', 'CSv%', 'FF%', 'FSh%', 'FSv%', 'GDiff',
       'GF%', 'PDO', 'PENDiff', 'SF%', 'SDiff', 'Sh%', 'Sv']
columns_to_scale = ['CF%', 'CSh%', 'CSv%', 'FF%', 'FSh%', 'FSv%', 'GDiff',
       'GF%', 'PDO', 'PENDiff', 'SF%', 'SDiff', 'Sh%', 'Sv']  ##same


x = np.array(X.loc[(X['season'] != 20172018), x_cols].copy())
  #features test-train
Y = X.loc[(X['season'] != 20172018), y_cols].copy()
   #targets
y = np.array(Y['home_win']).reshape(-1,1)


              
x_17 = X.loc[(X['season'] == 20172018), x_cols].copy()  #features test-train
Y_17 = X.loc[(X['season'] == 20172018), y_cols].copy()   #targets
y_17 = np.array(Y['home_win']).reshape(-1,1)

X.loc[(X['season'] != 20172018), x_cols].copy().shape
x.shape
y.shape
Y['season'].value_counts()
Y_17['season'].value_counts()
from sklearn.pipeline import make_pipeline

from sklearn.preprocessing import StandardScaler, MinMaxScaler 
std_scal = StandardScaler()
mm_scal = MinMaxScaler()
x_test
from sklearn.model_selection import train_test_split

x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=42, stratify=y)

#do standard/minmax scaling on X_train numeric columns ... better to do pipeline? 
x_train_sc = std_scal.fit_transform(x_train)
    
#fit the scaler from train portion to the test portion 
x_test_sc = std_scal.transform(x_test)

x_test_sc
pd.DataFrame(x_test_sc)
##handy code from Leung

# Taken from <NAME>
#for measuring training time#for me 
from time import time 
# F1 score (also F-score or F-measure) is a measure of a test's accuracy. 
#It considers both the precision p and the recall r of the test to compute 
#the score: p is the number of correct positive results divided by the number of 
#all positive results, and r is the number of correct positive results divided by 
#the number of positive results that should have been returned. The F1 score can be 
#interpreted as a weighted average of the precision and recall, where an F1 score 
#reaches its best value at 1 and worst at 0.
from sklearn.metrics import f1_score

from sklearn.metrics import f1_score

def train_classifier(clf, X_train, y_train):
    ''' Fits a classifier to the training data. '''
    
    # Start the clock, train the classifier, then stop the clock
    start = time()
    clf.fit(X_train, y_train)
    end = time()
    
    # Print the results
    print("Trained model in {:.4f} seconds".format(end - start))

    
def predict_labels(clf, features, target):
    ''' Makes predictions using a fit classifier based on F1 score. '''
    
    # Start the clock, make predictions, then stop the clock
    start = time()
    y_pred = clf.predict(features)
    
    end = time()
    # Print and return results
    print("Made predictions in {:.4f} seconds.".format(end - start))
    
    return f1_score(target, y_pred), sum(target == y_pred) / float(len(y_pred))


def train_predict(clf, X_train, y_train, X_test, y_test):
    ''' Train and predict using a classifer based on F1 score. '''
    
    # Indicate the classifier and the training set size
    print("Training a {} using a training set size of {}. . .".format(clf.__class__.__name__, len(X_train)))
    
    # Train the classifier
    train_classifier(clf, X_train, y_train)
    
    # Print the results of prediction for both training and testing
    f1, acc = predict_labels(clf, X_train, y_train)
    print(f1, acc)
    print("F1 score and accuracy score for test set: ", f1, acc )
    
    f1, acc = predict_labels(clf, X_test, y_test)
    print("F1 score and accuracy score for test set: ",  f1, acc)

def train_predictproba(clf, X_train, y_train, X_test, y_test):
    #Train and predict using a classifer based on F1 score. '''
    
    # Indicate the classifier and the training set size
    print("Training a {} using a training set size of {}. . .".format(clf.__class__.__name__, len(X_train)))
    
    # Train the classifier
    train_classifier(clf, X_train, y_train)
    
    # Print the results of prediction for both training and testing
    f1, acc = predict_labels(clf, X_train, y_train)
    print(f1, acc)
    print("F1 score and accuracy score for training set: ", f1, acc) 
    
    display(clf.predict_proba(X_test))

from sklearn.linear_model import LogisticRegression
import xgboost as xgbq
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import SVC
import xgboost as xgb


clf_A = LogisticRegression(random_state = 42)
clf_B = SVC(random_state = 43, kernel = 'rbf')
clf_C = xgb.XGBClassifier(seed = 44)
clf_D = SVC(probability=True)

train_predict(clf_A, x_train_sc, y_train, x_test_sc, y_test)
print('')
train_predict(clf_B, x_train_sc, y_train, x_test_sc, y_test)
print('')
train_predict(clf_C, x_train_sc, y_train, x_test_sc, y_test)
print('')
train_predict(clf_D, x_train_sc, y_train, x_test_sc, y_test)
print('')<jupyter_output>Training a LogisticRegression using a training set size of 7333. . .
Trained model in 0.0551 seconds
Made predictions in 0.0003 seconds.
0.6724046328764213 [0.54820674 0.54820674 0.54820674 ... 0.54820674 0.54820674 0.45179326]
F1 score and accuracy score for test set:  0.6724046328764213 [0.54820674 0.54820674 0.54820674 ... 0.54820674 0.54820674 0.45179326]
Made predictions in 0.0002 seconds.
F1 score and accuracy score for test set:  0.6586310804993544 [0.54798255 0.54798255 0.54798255 ... 0.54798255 0.54798255 0.54798255]

Training a SVC using a training set size of 7333. . .
{'fraction_non_alphanumeric': 0.1386237355748682, 'fraction_numerical': 0.06852828038182077, 'mean_word_length': 4.246636771300448, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, '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': '45215', 'license': 'MIT', 'n_tokens_mistral': 2927, 'n_tokens_neox': 2594, 'n_words': 742, 'path': 'Note_books/Explore_Models/.ipynb_checkpoints/Model3_Leung_v2-checkpoint.ipynb', 'repo_name': 'joeyamosjohns/final_project_nhl_prediction_first_draft', 'size': '7024'}