forked from Benjamin-Wolff/BasketballPointPrediction
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogistic_regression.py
159 lines (113 loc) · 4.32 KB
/
logistic_regression.py
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import numpy as np
import matplotlib.pyplot as plt
from sklearn import linear_model, metrics
import scipy.io as sp
import pandas as pd
from IPython.display import display
url = "https://raw.githubusercontent.com/Benjamin-Wolff/BasketballPointPrediction/main/julius_randle_career_stats_by_game.csv"
data = pd.read_csv("julius_randle_career_stats_by_game.csv", error_bad_lines=False)
data_cpy = data.copy()
# data_cpy.columns
data_cpy = data_cpy.tail(10)
feature_cols = ["PlayerEfficiencyRating", "UsageRatePercentage", "FantasyPointsFantasyDraft", "FantasyPoints", "Minutes", "Assists",
"PlusMinus"]
X_and_y = feature_cols.copy()
X_and_y.append("Points")
# X = data_cpy[["PlayerEfficiencyRating", "UsageRatePercentage", "FantasyPointsFantasyDraft"]]
data_cpy["Points"] = (data_cpy["ThreePointersMade"] * 3) + ((data_cpy["FieldGoalsMade"] - data_cpy["ThreePointersMade"]) * 2) + data_cpy["FreeThrowsMade"]
data_cpy = data_cpy.loc[:, X_and_y]
data_cpy = data_cpy.dropna()
X = data_cpy.loc[:, feature_cols]
X.to_csv("test.csv")
# print(X.shape)
# print(X)
# Get the Y values
# print(data_cpy[data_cpy["Index"].between(500, 600)])
def average_last_n(df, window_size, reference_column, output_column):
lst = []
for i in range(0, len(data_cpy)):
if(i < window_size):
lst.append(df[reference_column].iloc[0:i].mean())
else:
lst.append(df[reference_column].iloc[i-window_size:i].mean())
lst[0] = 0
df[output_column] = lst
average_last_n(data_cpy, 2, "Points", "PointsLast5")
Y = data_cpy[["Points", "PointsLast5"]]
Y.to_csv("test_output.csv")
Y = data_cpy.Points
# print(Y.shape)
# print(np.isnan(data_cpy.any())) #and gets False
# print(np.isfinite(data_cpy.all())) #and gets True
# print(Y)
# X = data["X_trn"]
# Y = data["Y_trn"]
# # print(X, Y)
# X_test = data["X_tst"]
# Y_test = data["Y_tst"]
# ----------------
# Fit the data to a logistic regression model.
data_cpy = data_cpy.reset_index()
model = linear_model.LogisticRegression(solver="newton-cg")
model.fit(X, Y)
# Retrieve the model parameters.
# b = model.intercept_[0]
# w1, w2 = model.coef_.T
# # Calculate the intercept and gradient of the decision boundary.
# c = -b/w2
# m = -w1/w2
# predict classes
yhat = model.predict(X)
Y_values = []
for i in Y:
Y_values.append(i)
# print(Y_values)
# print(yhat)
# evaluate the predictions
acc_train = metrics.accuracy_score(Y, yhat)
print("Training Data Classification Accuracy: %.3f" % acc_train)
print()
# ----------------
# acc_test = model.score(X_test, Y_test)
# print("Testing Data Classification Accuracy: " + str(acc_test))
# print("Testing Data Classification Error: " + str(1 - acc_test))
# #Configuring two plots
# fig, axs = plt.subplots(2, figsize=(6.4, 9.6))
# fig.suptitle('Logistic Regression Decision Boundaries')
# # Plot the data and the classification with the decision boundary.
# xmin, xmax = -2, 2
# ymin, ymax = -1, 6
# xd = np.array([xmin, xmax])
# yd = m*xd + c
# axs[0].plot(xd, yd, 'k', lw=1, ls='--')
# axs[0].fill_between(xd, yd, ymin, color='tab:blue', alpha=0.2)
# axs[0].fill_between(xd, yd, ymax, color='tab:orange', alpha=0.2)
# axs[1].plot(xd, yd, 'k', lw=1, ls='--')
# axs[1].fill_between(xd, yd, ymin, color='tab:blue', alpha=0.2)
# axs[1].fill_between(xd, yd, ymax, color='tab:orange', alpha=0.2)
# def create_axes(X, Y):
# x1_class_0 = []
# x2_class_0 = []
# x1_class_1 = []
# x2_class_1 = []
# for i in range(0, len(X)):
# if(Y[i] == 0):
# x1_class_0.append(X[i][0])
# x2_class_0.append(X[i][1])
# else:
# x1_class_1.append(X[i][0])
# x2_class_1.append(X[i][1])
# return x1_class_0, x2_class_0, x1_class_1, x2_class_1
# train_x1_class_0, train_x2_class_0, train_x1_class_1, train_x2_class_1 = create_axes(X, Y)
# test_x1_class_0, test_x2_class_0, test_x1_class_1, test_x2_class_1 = create_axes(X_test, Y_test)
# axs[0].set_title("Training Data")
# axs[0].scatter(train_x1_class_0, train_x2_class_0, s=8, alpha=0.5)
# axs[0].scatter(train_x1_class_1, train_x2_class_1, s=8, alpha=0.5)
# axs[0].set_xlim([xmin, xmax])
# axs[0].set_ylim([ymin, ymax])
# axs[1].set_title("Testing Data")
# axs[1].scatter(test_x1_class_0, test_x2_class_0, s=8, alpha=0.5)
# axs[1].scatter(test_x1_class_1, test_x2_class_1, s=8, alpha=0.5)
# axs[1].set_xlim([xmin, xmax])
# axs[1].set_ylim([ymin, ymax])
# plt.show()