# How to handle overfitting in Random Forest

I have a random forest model I built to predict if NFL teams will score more combined points than the line Vegas has set. The features I use are Total - the total number of combined points Vegas thinks both teams will score, over_percentage - the percentage of public bets on the over, and under_percentage - the percentage of public bets on the under. The over means people are betting that both team's combined scores will be greater than the number Vegas sets and under means the combined score will go under the Vegas number. When I run my model I'm getting a confusion_matrix like this

and an accuracy_score of 76%. However, the predictions do not perform well. Right now I have it giving me the probability the classification will be 0. I'm wondering if there are parameters I can tune or solutions to prevent my model from overfitting. I have over 30K games in the training data set so I don't think lack of data is causing the issue.

Here is the code:

import pandas as pd
import numpy as np
import json
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score
from sklearn.preprocessing import StandardScaler

training_data = pd.read_csv(
'/Users/aus10/NFL/Data/Betting_Data/Training_Data_Betting.csv')
test_data = pd.read_csv(
'/Users/aus10/NFL/Data/Betting_Data/Test_Data_Betting.csv')

df_model = training_data.copy()
df_model = df_model.dropna()

X = df_model.loc[:, ["Total", "Over_Percentage",
"Under_Percentage"]]  # independent columns
y = df_model["Over_Under"]  # target column

results = []

model = RandomForestClassifier(n_estimators=1000, random_state=100)

model.fit(X, y)

X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.25, random_state=100)

y_train_pred = model.predict(X_train)
y_test_pred = model.predict(X_test)

model.fit(X_train, y_train)

y_pred = model.predict(X_test)

print(confusion_matrix(y_test, y_pred))
print(classification_report(y_test, y_pred))
print(round(accuracy_score(y_test, y_pred), 2))

index = 0
count = 0

while count < len(test_data):
team = test_data.loc[index].at['Team']
total = test_data.loc[index].at['Total']
over_perc = test_data.loc[index].at['Over_Percentage']
under_perc = test_data.loc[index].at['Under_Percentage']

Xnew = [[total, over_perc, under_perc]]
# make a prediction
ynew = model.predict_proba(Xnew)
# show the inputs and predicted outputs
results.append(
{
'Team': team,
'Over': ynew[0][0]
})
index += 1
count += 1

sorted_results = sorted(results, key=lambda k: k['Over'], reverse=True)

df = pd.DataFrame(sorted_results, columns=[
'Team', 'Over'])
writer = pd.ExcelWriter('/Users/aus10/NFL/Data/ML_Results/Over_Probability.xlsx',  # pylint: disable=abstract-class-instantiated
engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1', index=False)
df.style.set_properties(**{'text-align': 'center'})
pd.set_option('display.max_colwidth', 100)
pd.set_option('display.width', 1000)
writer.save()


And here are links the the google docs with the test and training data.

Test Data

https://docs.google.com/spreadsheets/d/1IWZ6UPDd8ZZXsON_3kQCVynb2XDu4l1zza2lDL5vBbA/edit?usp=sharing

Training Data

https://docs.google.com/spreadsheets/d/1Z36dIrin7Xtup6qbAKfQycWD4cChX4g7nneaAzL1UdQ/edit?usp=sharing

• Interesting problem. If you are seeking help debugging the code then this is the wrong site and you should delete the question here and ask it on stack overflow. We don't debug code here. – pacmaninbw Nov 10 '20 at 14:56
• It's not a bug just a way to improve the code. – Austin Johnson Nov 10 '20 at 14:57
• Most feedback here - codereview.meta.stackexchange.com/questions/8781/… - would suggest that issues of ML accuracy are likely off-topic for CR. – Reinderien Nov 10 '20 at 15:21