Repository navigation
Expand file tree
/
Copy pathfinal_project_machine_learning.py
More file actions
189 lines (115 loc) · 4.87 KB
/
Copy pathfinal_project_machine_learning.py
File metadata and controls
189 lines (115 loc) · 4.87 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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import warnings
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import train_test_split
from sklearn import metrics
from sklearn.linear_model import LinearRegression
from sklearn.tree import DecisionTreeRegressor
from sklearn.svm import SVR
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn import linear_model
def make_plot(y_true, y_pred, title):
'''
Creates and labels the plot given the input.
'''
plt.scatter(y_true, y_pred, alpha = 0.4, c = 'blue', label = title)
plt.xlabel('True')
plt.ylabel('Prediction')
plt.legend()
plt.savefig('./plots/' + title + '.png')
plt.show()
plt.clf()
# Using root mean squared error as my score metric.
def score(y_pred, y_true):
mse = metrics.mean_squared_error(y_true, y_pred)
rmse = np.sqrt(mse)
rounded_rmse = round(rmse, 3)
return rounded_rmse
# Used to get the train set and test set from the DataFrame as input.
def gather_data(pokemon, stat):
x = pokemon.drop([stat], axis = 1)
y = pokemon[stat]
return train_test_split(x, y, test_size = 0.20, random_state = 42)
def random_forest_regression(pokemon, stat):
'''
Section for using the Random Forest Regressor model to fit the data.
'''
x_train, x_test, y_train, y_test = gather_data(pokemon, stat)
rfr_model = RandomForestRegressor()
rfr_model.fit(x_train, y_train)
y_pred = rfr_model.predict(x_test)
accuracy = score(y_pred, y_test)
print('\n Random Forest Regression: ' + stat + ' - ' + str(accuracy))
make_plot(y_test, y_pred, 'Random Forest Regression - ' + stat)
def linear_regression(pokemon, stat):
'''
Section for using the Linear Regression model to fit the data.
'''
x_train, x_test, y_train, y_test = gather_data(pokemon, stat)
lr_model = LinearRegression()
lr_model.fit(x_train, y_train)
y_pred = lr_model.predict(x_test)
accuracy = score(y_pred, y_test)
print('\n Linear Regression: ' + stat + ' - ' + str(accuracy))
make_plot(y_test, y_pred, 'Linear Regression - ' + stat)
def decision_tree_regression(pokemon, stat):
'''
Section for using the Decision Tree Regressor model to fit the data.
'''
x_train, x_test, y_train, y_test = gather_data(pokemon, stat)
accuracies = []
for i in range(1,15):
dtr_model = DecisionTreeRegressor(max_depth = i)
dtr_model.fit(x_train, y_train)
y_pred = dtr_model.predict(x_test)
accuracy = score(y_pred, y_test)
accuracies.append(accuracy)
if i == 1:
min_index = i
best_y_pred = y_pred
elif accuracy < accuracies[min_index]:
min_index = i
best_y_pred = y_pred
print('\n Decision Tree Regression (Max Depth = ' + str(min_index) + '): ' + stat + ' - ' + str(accuracies[min_index]))
make_plot(y_test, best_y_pred, 'Decision Tree Regression - ' + stat)
def support_vector_regression(pokemon, stat):
'''
Section for using the Support Vector Regression model to fit the data.
'''
x_train, x_test, y_train, y_test = gather_data(pokemon, stat)
svr_model = make_pipeline(StandardScaler(),
SVR(C = 1.0, epsilon = 0.2))
svr_model.fit(x_train, y_train)
y_pred = svr_model.predict(x_test)
accuracy = score(y_pred, y_test)
print('\n Support Vector Regression: ' + stat + ' - ' + str(accuracy))
make_plot(y_test, y_pred, 'Support Vector Regression - ' + stat)
def lasso_regression(pokemon, stat):
'''
Section for using the Lasso Regression model to fit the data.
'''
x_train, x_test, y_train, y_test = gather_data(pokemon, stat)
lasso_model = linear_model.Lasso(alpha = 0.1)
lasso_model.fit(x_train, y_train)
y_pred = lasso_model.predict(x_test)
accuracy = score(y_pred, y_test)
print('\n Lasso Regression: ' + stat + ' - ' + str(accuracy))
make_plot(y_test, y_pred, 'Lasso Regression - ' + stat)
def main():
warnings.simplefilter(action = 'ignore', category = FutureWarning)
pd.set_option('display.max_columns', 15)
pokemon = pd.read_csv('./csv/cleaned_pokemon.csv')
stats = ['hp', 'attack', 'defense', 'spatk', 'spdef', 'speed']
pokemon = pokemon[stats]
for stat in stats:
print('\n\n')
random_forest_regression(pokemon, stat)
linear_regression(pokemon, stat)
decision_tree_regression(pokemon, stat)
support_vector_regression(pokemon, stat)
lasso_regression(pokemon, stat)
if __name__ == '__main__':
main()