Ejemplo n.º 1
0
def main(pred_week, vegas_adjustment=False, run_query=False, expert_projections=False):

    db = nfldb.connect()
    result_path='../results'

    full_train, pipe, stats = load_feature_set(db, load_cached=not run_query, to_yr_wk=(2015, pred_week))

    # picks columns to model
    lag_cols = [stat + '_lag' for stat in stats]
    mean_cols = [stat + '_mean' for stat in stats]
    other_cols = ['same_year_lag', 'played_lag']

    infoColumns = ExtractColumns(like=[], exact=['year','week','time','player_id','full_name'])
    row_info = infoColumns.fit_transform(X=full_train)

    pred_data, predict_i, pred_info, pred_yr_wk = prediction_feature_set(db, pipe, infoColumns, pred_week=pred_week)

    X_all = full_train
    pred_all = pred_data.iloc[predict_i]
    pred_results = pred_info.iloc[predict_i]

    # which rows did players play
    played_bool = full_train['played'] == 1
    played_index = [i for i in range(X_all.shape[0]) if played_bool[i]]

    # random split train and test
    train_index, test_index = train_test_split_index(X_all.shape[0], test_size=0.1, seed=0)

    feature_cols = lag_cols + mean_cols + other_cols
    XColumns = ExtractColumns(like=feature_cols)
    X = XColumns.fit_transform(X=X_all)
    X_pred = XColumns.fit_transform(X=pred_all)

    played_only = True

    y_cols = ['played', 'receiving_rec', 'receiving_tds', 'receiving_yds', 'rushing_att', 'rushing_tds','rushing_yds']

    # added for saving test results for model evaluation
    rows = []

    for y_col in y_cols:

        y = X_all[y_col]

        if(played_only and y_col != 'played'):
            train_i = list(set.intersection(set(train_index), set(played_index)))
            test_i = list(set.intersection(set(test_index), set(played_index)))
        else:
            train_i = train_index
            test_i = test_index

        X_train = X.iloc[train_i]
        y_train = y.iloc[train_i]
        X_test = X.iloc[test_i]
        y_test = y.iloc[test_i]

        # get player info for train and test data
        X_train_info = row_info.iloc[train_i]
        X_test_info = row_info.iloc[test_i]

        ### Test Predictions

        predict_proba = y_col == 'played'

        if(predict_proba):
            models = {
                'gb':GradientBoostingClassifier(n_estimators=100, learning_rate=0.1),
                'rf':RandomForestClassifier(),
                'lin':LogisticRegression(),
                'dum':DummyClassifier()
            }
        else:
            models = {
                'gb':GradientBoostingRegressor(n_estimators=100, learning_rate=0.1),
                'rf':RandomForestRegressor(),
                'lin':LinearRegression(),
                'dum':DummyRegressor()
            }

        gb, gb_test, gb_scores = fit_predict(
            model=models['gb'],
            X_train=X_train,
            y_train=y_train,
            X_test=X_test,
            y_test=y_test,
            predict_proba=predict_proba)

        rf, rf_test, rf_scores = fit_predict(
            model=models['rf'],
            X_train=X_train,
            y_train=y_train,
            X_test=X_test,
            y_test=y_test,
            predict_proba=predict_proba)

        lin, lin_test, lin_scores = fit_predict(
            model=models['lin'],
            X_train=X_train,
            y_train=y_train,
            X_test=X_test,
            y_test=y_test,
            predict_proba=predict_proba)


        dum, dum_test, dum_scores = fit_predict(
            model=models['dum'],
            X_train=X_train,
            y_train=y_train,
            X_test=X_test,
            y_test=y_test,
            predict_proba=predict_proba)

        if vegas_adjustment and y_col != 'played':
            print '-'*50
            print 'Vegas Adjusted:', y_col

            X_train_all = build_vegas_dataframe(X=X_train, y=y_train,
                row_info=X_train_info, model=gb, db=db, y_col=y_col)
            X_test_all = build_vegas_dataframe(X=X_test, y=y_test,
                row_info=X_test_info, model=gb, db=db, y_col=y_col)

            features = [y_col, 'Total','is_favorite','spread_x_favorite']
            X_cols = ExtractColumns(exact=features)
            X_train = X_cols.fit_transform(X=X_train_all)
            X_test = X_cols.fit_transform(X=X_test_all)

            gb_a, gb_test_a, gb_scores_a = fit_predict(
                model=models['gb'],
                X_train=X_train,
                y_train=y_train,
                X_test=X_test,
                y_test=y_test)

            rf_a, rf_test_a, rf_scores_a = fit_predict(
                model=models['rf'],
                X_train=X_train,
                y_train=y_train,
                X_test=X_test,
                y_test=y_test)

            lin_a, lin_test_a, lin_scores_a = fit_predict(
                model=models['lin'],
                X_train=X_train,
                y_train=y_train,
                X_test=X_test,
                y_test=y_test)

            print 'Predicting %s' % (y_col)
            print lin_a.coef_
            print 'Gradient Boosting: RMSE %.2f | MAE %.2f' % (gb_scores_a['rmse'], gb_scores_a['mae'])
            print 'Random Forest: RMSE %.2f | MAE %.2f' % (rf_scores_a['rmse'], rf_scores_a['mae'])
            print '%s Regression: RMSE %.2f | MAE %.2f' % ('Linear', lin_scores_a['rmse'], lin_scores_a['mae'])

        print '-'*50
        print 'Historical Prediction:', y_col
        # Print Results
        print 'Gradient Boosting: RMSE %.2f | MAE %.2f' % (gb_scores['rmse'], gb_scores['mae'])
        print 'Random Forest: RMSE %.2f | MAE %.2f' % (rf_scores['rmse'], rf_scores['mae'])
        print '%s Regression: RMSE %.2f | MAE %.2f' % ('Logistic' if predict_proba else 'Linear', lin_scores['rmse'], lin_scores['mae'])
        print 'Baseline: RMSE %.2f | MAE %.2f' % (dum_scores['rmse'], dum_scores['mae'])


        result_row(method='Historical Only', results=gb_scores, stat=y_col, learner='Gradient Boosting', rows=rows)
        result_row(method='Historical Only', results=rf_scores, stat=y_col, learner='Random Forest', rows=rows)
        result_row(method='Historical Only', results=lin_scores, stat=y_col, learner='Logistic' if predict_proba else 'Linear', rows=rows)

        result_row(method='Baseline', results=dum_scores, stat=y_col, learner='Stratified' if predict_proba else 'Mean', rows=rows)

        # result_row(method='Vegas Adjusted', results=gb_scores_a, stat=y_col, learner='Gradient Boosting', rows=rows)
        # result_row(method='Vegas Adjusted', results=rf_scores_a, stat=y_col, learner='Random Forest', rows=rows)
        # result_row(method='Vegas Adjusted', results=lin_scores_a, stat=y_col, learner='Logistic' if predict_proba else 'Linear', rows=rows)


        # Build full models on all data
        gb = gb.fit(X, y)
        rf = rf.fit(X, y)
        lin = lin.fit(X, y)
        #### Next week's predictions
        # Make prediction, just gbr for now

        if(y_col == 'played'):
            preds = gb.predict_proba(X_pred)[:,1]
        else:
            preds = gb.predict(X_pred)
            if expert_projections:
                # create dataframe with predictions for all weeks of current
                # year to use with the expert prediction
                mask = (row_info.year == 2015) & (row_info.week <= pred_week)
                X_2015 = X[mask]
                y_2015 = X_all[mask][y_col]
                preds_2015 = cross_val_predict(gb, X_2015, y_2015)
                info_2015 = row_info[mask][['full_name','week','year']]
                info_2015.loc[:, y_col] = preds_2015
                info_2015.loc[:,'position'] = 'RB'
                info_2015.loc[:, y_col] = X_all[mask][y_col]

        # add our prediction based on historical data to output
        pred_results.loc[:,y_col] = preds

        # add expert projections, then make a final prediction
        if expert_projections and y_col != 'played':
            pred_results = add_expert_projections(pred_results, pred_week, y_col, info_2015, result_rows=rows)

    pred_results.replace(0, np.nan, inplace=True)
    out_path = result_path + '/predictions' + '_' + str(int(pred_yr_wk[0])) + '_' + str(int(pred_yr_wk[1])) + '.json'
    pred_results.to_json(path_or_buf = out_path, orient = 'records')
Ejemplo n.º 2
0
def main():
	################################
	### CONFIGURE
	pred_week = 14 #None
	db = nfldb.connect()
	result_path='../results'

	### LOAD DATA
	# load train data
	full_train, pipe, stats = load_feature_set(db)

	# picks columns to model
	lag_cols = [stat + '_lag' for stat in stats]
	mean_cols = [stat + '_mean' for stat in stats]
	other_cols = ['same_year_lag', 'played_lag']

	infoColumns = ExtractColumns(like=[], exact=['year','week','time','player_id','full_name'])
	row_info = infoColumns.fit_transform(X=full_train)


	# load prediction data
	pred_data, predict_i, pred_info, pred_yr_wk = prediction_feature_set(db, pipe, infoColumns, pred_week=pred_week)

	##################################
	### PREPARE DATA FOR TRAIN AND PREDICT
	# train data with all columns
	X_all = full_train

	# prediction data with all columns
	pred_all = pred_data.iloc[predict_i]

	# which rows did players play
	played_bool = full_train['played'] == 1
	played_index = [i for i in range(X_all.shape[0]) if played_bool[i]]

	# random split train and test
	train_index, test_index = train_test_split_index(X_all.shape[0], test_size=0.1, seed=0)

	feature_cols = lag_cols + mean_cols + other_cols
	XColumns = ExtractColumns(like=feature_cols)
	X = XColumns.fit_transform(X=X_all)
	X_pred = XColumns.fit_transform(X=pred_all)

	##################################
	### SET UP & TRAIN KNN
	# fit k nearest neighbors
	k = 100
	played_only = True
	i_knn = played_index if played_only else range(X.shape[0])

	#nn = NearestNeighbors(n_neighbors=k).fit(X.iloc[i_knn])
	# regularization
	reg = CoefScaler(linear_model=Ridge())
	reg = reg.fit(X=X.iloc[i_knn], y = score_stats(X_all, make_scorer(base_type='standard')).iloc[i_knn])
	X_reg = reg.transform(X.iloc[i_knn])
	nn = NearestNeighbors(n_neighbors=k).fit(X_reg)

	# returns tuple of (distances, indices of neighbors)
	# for prediction set
	#distance, neighbor = nn.kneighbors(X=X_pred)
	X_reg_pred = reg.transform(X=X_pred)
	distance, neighbor = nn.kneighbors(X=X_reg_pred)

	##################################
	### READ AND PLOT KNN RESULTS
	nn_dict = {}
	for check_i in range(pred_all.shape[0]):
	    # check neighbors
	    # check_nn is a data frame where the first row is the player
	    # and the rest of the rows are the nearest neighbors
	    check_nn = pred_all.iloc[[check_i],:].append(X_all.iloc[i_knn].iloc[neighbor[check_i,:]])
	    check_nn['StandardPoints'] = score_stats(check_nn, make_scorer(base_type='standard'))
	    check_nn['PPRPoints'] = score_stats(check_nn, make_scorer(base_type='ppr'))

	    nn_i = plot_knn(check_nn, save_image=True, plot_stat='StandardPoints', pred_yr_wk=pred_yr_wk, result_path=plot_image_path(result_path, pred_yr_wk), n_bins=25, bandwidth=2.5)
	    nn_dict.update(nn_i)

	save_plot_data_json(nn_dict, result_path, pred_yr_wk)