-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRandomForests.py
More file actions
105 lines (77 loc) · 3.59 KB
/
Copy pathRandomForests.py
File metadata and controls
105 lines (77 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import json
import math
#Use pandas for messing with the csv
import pandas as pd
#Use matplotlib for plots
import matplotlib.pyplot as plt
# Use numpy to convert to arrays
import numpy as np
# Import the model we are using
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_squared_error
from sklearn import metrics
# Using Skicit-learn to split data into training and testing sets
from sklearn.model_selection import train_test_split
#jsonObj[teamAbbr][year][week][unit]
#where unit is either 'off' or 'def'
with open("eloVals.json") as file:
elos = json.load(file)
data_orig = pd.read_csv("./data/allSeasonScores.csv")
data = data_orig.copy()
#print(data)
home_off_elo = []
home_def_elo = []
away_off_elo = []
away_def_elo = []
# Select column contents by column
# name using [] operator
homeTeamNames = data[data.columns[3]]
# print('Column Name : ', data.columns[3])
# print('Column Contents : ', homeTeamNames.values)
awayTeamNames = data[data.columns[4]]
year = data[data.columns[1]]
week = data[data.columns[2]]
#choose either off or def for unit
num_rows = data.shape[0]
#Create lists of elos so they can be appended to the dataFrame
for i in range(num_rows):
home_off_elo.insert(i, elos[ homeTeamNames[i] ][ str(year[i]) ][ str(week[i]) ]['off'])
home_def_elo.insert(i, elos[ homeTeamNames[i] ][ str(year[i]) ][ str(week[i]) ]['def'])
away_off_elo.insert(i, elos[ awayTeamNames[i] ][ str(year[i]) ][ str(week[i]) ]['off'])
away_def_elo.insert(i, elos[ awayTeamNames[i] ][ str(year[i]) ][ str(week[i]) ]['def'])
elo_data = data.assign(homeOffElo = home_off_elo, awayOffElo = away_off_elo, homeDefElo = home_def_elo, awayDefElo = away_def_elo)
#print(elo_data)
features = elo_data.drop( ['homeOffPoints', 'homeDefPoints', 'awayOffPoints', 'awayDefPoints','count', 'homeTeam', 'awayTeam', 'week', 'season',], axis=1 )
target = elo_data['homeOffPoints']
feature_list = list(features.columns)
#Convert features and labels into numpy arrays
features = np.array(features)
labels = np.array(target)
# Split the data into training and testing sets
train_features, test_features, train_labels, test_labels = train_test_split(features, labels, test_size = 0.25, random_state = 42)
# print('Training Features Shape:', train_features.shape)
# print('Training Labels Shape:', train_labels.shape)
# print('Testing Features Shape:', test_features.shape)
# print('Testing Labels Shape:', test_labels.shape)
# print(train_features)
# print(train_labels)
# Instantiate model with 1000 decision trees
rf = RandomForestRegressor(n_estimators = 1000, random_state = 42)
# Train the model on training data
rf.fit(train_features, train_labels)
# Use the forest's predict method on the test data
predictions = rf.predict(test_features)
# print(predictions[:20])
# print(test_labels[:20])
print('Mean Absolute Error:', metrics.mean_absolute_error(test_labels, predictions))
print('Mean Squared Error:', metrics.mean_squared_error(test_labels, predictions))
print('Root Mean Squared Error:', np.sqrt(metrics.mean_squared_error(test_labels, predictions)))
#plot predictions vs actual labels
fig = plt.figure(1)
plt.title("Predicting Total Points Scored by Home Team")
plt.plot( range(0,50), predictions[:50], 'b.', markersize = 3)
plt.plot(range(0,50), test_labels[:50], 'r-', markersize = 2)
plt.ylabel("Total Points Scored by Home Team")
plt.xlabel("Each Test Performed (Indices of Predictions and Test Label Lists Up to 20)" )
plt.legend(('Prediction', 'Actual'), loc='lower right')
plt.show()