Esempio n. 1
0
    def __init__(self):

        self.label_translator = {
            "age_1": "BC Age",
            'age_2': 'RC Age',
            'age_diff': 'Age DIFF',
            'def_ko_1': 'RC Chin',
            'def_ko_2': 'BC Chin',
            'def_ko_diff': 'Chin DIFF',
            'def_ko_share': 'Chin SHARE',
            'def_strike_1': 'BC Striking Def',
            'def_strike_2': 'RC Striking Def',
            'def_strike_diff': 'Striking Def DIFF',
            'def_strike_share': 'Striking Def SHARE',
            'eff_grap_1': 'BC Grappling EFF',
            'eff_grap_2': 'RC Grappling EFF',
            'eff_grap_diff': 'Grappling EFF DIFF',
            'eff_grap_share': 'Grappling EFF SHARE',
            'eff_ko_2': 'RC KO EFF',
            'eff_strike_1': 'BC Striking EFF',
            'eff_strike_2': 'RC Striking EFF',
            'eff_strike_diff': 'Striking EFF DIFF',
            'eff_strike_share': 'Striking EFF SHARE',
            'off_grapp_1': 'BC Grappling Off',
            'off_grapp_2': 'RC Grappling Off',
            'off_grapp_diff': 'Grappling Off DIFF',
            'off_grapp_share': 'Grappling Off SHARE',
            'off_ko_1': 'BC KO',
            'off_ko_diff': 'KO DIFF',
            'off_ko_share': 'KO SHARE',
            'off_strike_1': 'BC Strike Off',
            'off_strike_2': 'RC Strike Off',
            'off_strike_diff': 'Strike Off DIFF',
            'off_strike_share': 'Strike Off SHARE',
            'prev_fights_1': 'BC Num Fights',
            'prev_fights_2': 'RC Num Fights',
            'rounds': 'Rounds'
        }

        print('retrieve_best_trained_models - loading best model')
        mod = load('src/predictors/all/post_feat/trained_model.joblib')
        print('retrieve_best_trained_models - successfully loaded best model')
        print('retrieve_best_trained_models - loading best scale')
        self.scale = load('src/predictors/all/post_feat/trained_scale.joblib')
        print('retrieve_best_trained_models - successfully loaded best scale')

        self.red_feats = retrieve_reduced_domain_features(domain='all')

        self.explainer = shap.TreeExplainer(mod)
Esempio n. 2
0
def _train_model(model, domain = 'strike'):
    scale = StandardScaler()
    X, Y = form_to_domain(domain = domain)
    red_feats = retrieve_reduced_domain_features(domain = domain)
    X = X[red_feats['features']]
    scale.fit(X)
    x_train = pd.DataFrame(scale.transform(X))
    x_train.set_index(Y.index, inplace = True)
    x_train.rename(columns = {i:j for i,j in zip(list(x_train), list(X))}, inplace = True)
    
    dump(scale, 'src/predictors/%s/post_feat/trained_scale.joblib' % (domain))
    
    model.fit(x_train, Y)
    
    return model
Esempio n. 3
0
def generate_new_win_score(data):
    try:
        print('generate_new_win_score - retrieving best model')
        sub_model, sub_scale = retrieve_best_trained_models(domain = 'all')
        print('generate_new_win_score - successfully retrieved best model')
    
        print('generate_new_win_score - retrieving domain features')
        red_feats = retrieve_reduced_domain_features(domain = 'all')
        print('generate_new_win_score - successfully retrieved domain features')
    
        print('generate_new_win_score - generating new score')
        sub_score = sub_model.predict_proba(sub_scale.transform(np.array(data[red_feats['features']]).reshape(1,-1)))[0][1]
        print('generate_new_win_score - successfully generated new score')
    
        return sub_score
    except Exception as e:
        print('generate_new_win_score failed with %s' % (e))
        raise e
Esempio n. 4
0
    def gen_win_pred_explainer(self, bout_id):

        print('retrieve_best_trained_models - loading best model')
        mod = load('src/predictors/all/post_feat/trained_model.joblib')
        print('retrieve_best_trained_models - successfully loaded best model')
        print('retrieve_best_trained_models - loading best scale')
        scale = load('src/predictors/all/post_feat/trained_scale.joblib')
        print('retrieve_best_trained_models - successfully loaded best scale')

        explainer = shap.TreeExplainer(mod)

        red_feats = retrieve_reduced_domain_features(domain='all')
        bout_detail = refreshBout(bout_id)
        # print('insert_new_ml_prob - forming new bout data')
        bout_data = form_new_ml_odds_data(bout_id)[red_feats['features']]

        new_row = pd.DataFrame(
            scale.transform(
                np.array(bout_data[red_feats['features']]).reshape(1,
                                                                   -1))[0]).T
        new_row.set_index(pd.Index([bout_id]), inplace=True)
        new_row.rename(
            columns={i: j
                     for i, j in zip(list(new_row), list(bout_data))},
            inplace=True)

        shap_values = explainer.shap_values(new_row)

        columns, fighter_1_data, fighter_2_data = self._win_pred_graph_data(
            shap_values, new_row)

        fighter_1_data['name'] = bout_detail['fighterBoutXRefs'][0]['fighter'][
            'fighterName']
        fighter_2_data['name'] = bout_detail['fighterBoutXRefs'][1]['fighter'][
            'fighterName']

        return columns, fighter_1_data, fighter_2_data
Esempio n. 5
0
from joblib import load
import pandas as pd
from training import retrieve_reduced_domain_features, form_new_ml_odds_data
from spring.api_wrappers import refreshBout
import numpy as np

domain = 'all'

print('retrieve_best_trained_models - loading best model')
mod = load('src/predictors/%s/post_feat/trained_model.joblib' % (domain))
print('retrieve_best_trained_models - successfully loaded best model')
print('retrieve_best_trained_models - loading best scale')
scale = load('src/predictors/%s/post_feat/trained_scale.joblib' % (domain))
print('retrieve_best_trained_models - successfully loaded best scale')

red_feats = retrieve_reduced_domain_features(domain=domain)

bout_id = '57939328c1214107'
bout_detail = refreshBout(bout_id)
print('insert_new_ml_prob - forming new bout data')
bout_data = form_new_ml_odds_data(bout_id)[red_feats['features']]

new_row = pd.DataFrame(
    scale.transform(np.array(bout_data[red_feats['features']]).reshape(
        1, -1))[0]).T
new_row.set_index(pd.Index([bout_id]), inplace=True)
new_row.rename(columns={i: j
                        for i, j in zip(list(new_row), list(bout_data))},
               inplace=True)

explainer = shap.TreeExplainer(mod)
Esempio n. 6
0
    def __init__(self, pw):
        self.pw = pw
        self.weight_class_rankings = {}
        self.weight_class_fighters = {}
        self.weight_classes = [
            'FW', 'WSW', 'WFW', 'BW', 'WBW', 'FFW', 'WFFW', 'LW', 'MW', 'WW',
            'LHW', 'HW'
        ]
        for wc in self.weight_classes:
            print('initializing %s' % (wc))
            self.weight_class_rankings[wc], self.weight_class_fighters[
                wc] = calc_rankings_for_wc(wc)
            print('initialized %s' % (wc))

        self.label_translator = {
            "age_1": "BC Age",
            'age_2': 'RC Age',
            'age_diff': 'Age DIFF',
            'age_share': 'Age SHARE',
            'def_grapp_1': 'BC Grappling Def',
            'def_grapp_2': 'RC Grappling Def',
            'def_grapp_diff': 'Grappling Def DIFF',
            'def_grapp_share': 'Grappling Def SHARE',
            'def_ko_2': 'BC Chin',
            'def_ko_diff': 'Chin DIFF',
            'def_ko_share': 'Chin SHARE',
            'def_strike_1': 'BC Striking Def',
            'def_strike_2': 'RC Striking Def',
            'def_strike_diff': 'Striking Def DIFF',
            'def_strike_share': 'Striking Def SHARE',
            'def_sub_1': 'BC Submission Def',
            'def_sub_2': 'RC Submission Def',
            'def_sub_diff': 'Submission Def DIFF',
            'def_sub_share': 'Submission Def SHARE',
            'eff_grap_1': 'BC Grappling EFF',
            'eff_grap_2': 'RC Grappling EFF',
            'eff_grap_diff': 'Grappling EFF DIFF',
            'eff_grap_share': 'Grappling EFF SHARE',
            'eff_ko_1': 'BC KO EFF',
            'eff_ko_2': 'RC KO EFF',
            'eff_ko_diff': 'KO EFF DIFF',
            'eff_ko_share': 'KO EFF SHARE',
            'eff_strike_1': 'BC Striking EFF',
            'eff_strike_2': 'RC Striking EFF',
            'eff_strike_diff': 'Striking EFF DIFF',
            'eff_strike_share': 'Striking EFF SHARE',
            'eff_sub_1': 'BC Submission EFF',
            'eff_sub_2': 'RC Submission EFF',
            'eff_sub_diff': 'Submission EFF DIFF',
            'eff_sub_share': 'Submission EFF SHARE',
            'off_grapp_1': 'BC Grappling Off',
            'off_grapp_2': 'RC Grappling Off',
            'off_grapp_diff': 'Grappling Off DIFF',
            'off_grapp_share': 'Grappling Off SHARE',
            'off_ko_1': 'BC KO',
            'off_ko_2': 'RC KO',
            'off_ko_diff': 'KO DIFF',
            'off_ko_share': 'KO SHARE',
            'off_strike_1': 'BC Strike Off',
            'off_strike_2': 'RC Strike Off',
            'off_strike_diff': 'Strike Off DIFF',
            'off_strike_share': 'Strike Off SHARE',
            'off_sub_1': 'BC Submission Off',
            'off_sub_2': 'RC Submission Off',
            'off_sub_diff': 'Submission Off DIFF',
            'off_sub_share': 'Submission Off SHARE',
            'prev_fights_1': 'BC Num Fights',
            'prev_fights_2': 'RC Num Fights',
            'rounds': 'Rounds'
            #             'def_ko_1': 'RC Chin',
            #             'def_ko_2': 'BC Chin',
            #             'def_ko_diff': 'Chin DIFF',
            #             'def_ko_share': 'Chin SHARE',
            #             'def_strike_1': 'BC Striking Def',
            #             'def_strike_2': 'RC Striking Def',
            #             'def_strike_diff': 'Striking Def DIFF',
            #             'def_strike_share': 'Striking Def SHARE',
            #             'eff_grap_1': 'BC Grappling EFF',
            #             'eff_grap_2': 'RC Grappling EFF',
            #             'eff_grap_diff': 'Grappling EFF DIFF',
            #             'eff_grap_share': 'Grappling EFF SHARE',
            #             'eff_ko_2': 'RC KO EFF',
            #             'eff_strike_1': 'BC Striking EFF',
            #             'eff_strike_2': 'RC Striking EFF',
            #             'eff_strike_diff': 'Striking EFF DIFF',
            #             'eff_strike_share': 'Striking EFF SHARE',
            #             'off_grapp_1': 'BC Grappling Off',
            #             'off_grapp_2': 'RC Grappling Off',
            #             'off_grapp_diff': 'Grappling Off DIFF',
            #             'off_grapp_share': 'Grappling Off SHARE',
            #             'off_ko_1': 'BC KO',
            #             'off_ko_diff': 'KO DIFF',
            #             'off_ko_share': 'KO SHARE',
            #             'off_strike_1': 'BC Strike Off',
            #             'off_strike_2': 'RC Strike Off',
            #             'off_strike_diff': 'Strike Off DIFF',
            #             'off_strike_share': 'Strike Off SHARE',
        }

        print('retrieve_best_trained_models - loading best model')
        mod = load('src/predictors/all/post_feat/trained_model.joblib')
        print('retrieve_best_trained_models - successfully loaded best model')
        print('retrieve_best_trained_models - loading best scale')
        self.scale = load('src/predictors/all/post_feat/trained_scale.joblib')
        print('retrieve_best_trained_models - successfully loaded best scale')

        self.red_feats = retrieve_reduced_domain_features(domain='all')

        self.explainer = shap.TreeExplainer(mod)