-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
165 lines (148 loc) · 7.33 KB
/
main.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
160
161
162
163
164
165
import definition
import numpy as np
import pandas as pd
from sklearn.metrics import accuracy_score
from sklearn.metrics import precision_score
from sklearn.metrics import recall_score
from sklearn.metrics import f1_score
from sklearn.metrics import jaccard_score
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn import preprocessing
from sklearn.model_selection import cross_validate
from preprocess import FootballPreprocessesor
from dataset import Dataset
def seperate_labels_from_data(df):
labels = df.pop(definition.TOKEN_CLASS_NAME).tolist()
return labels, df
def remove_features(df, feature):
return df.drop(columns=[feature])
def normalize(df) -> pd.DataFrame:
cols = df.columns.tolist()
min_max = preprocessing.MinMaxScaler()
scaled_df = min_max.fit_transform(df.values)
df2 = pd.DataFrame(scaled_df,columns=cols)
return df2
ds = Dataset("database.sqlite")
preprocessor = FootballPreprocessesor(ds)
df2, df2test = preprocessor.preprocess()
labels_train, df2 = seperate_labels_from_data(df2)
labels_test, df2test = seperate_labels_from_data(df2test)
df2 = normalize(df2)
df2test = normalize(df2test)
df2=np.array(df2)
df2test=np.array(df2test)
Y=np.array(labels_train)
X=df2[:,0:df2.shape[1]-1]
Y1=np.array(labels_test)
X1=df2test[:,0:df2.shape[1]-1]
## KNeighbors Model
model1 = KNeighborsClassifier(n_neighbors=5)
model1.fit(X,Y)
score=cross_validate(model1,X,Y,scoring='accuracy')
print("KNeighbors Cross Validation accuracy %.2f%%" % (np.mean(score['test_score'])*100))
score=cross_validate(model1,X,Y,scoring='precision_macro')
print("KNeighbors Cross Validation precision %.2f%%" % (np.mean(score['test_score'])*100))
score=cross_validate(model1,X,Y,scoring='recall_macro')
print("KNeighbors Cross Validation recall %.2f%%" % (np.mean(score['test_score'])*100))
score=cross_validate(model1,X,Y,scoring='f1_macro')
print("KNeighbors Cross Validation f1 %.2f%%" % (np.mean(score['test_score'])*100))
score=cross_validate(model1,X,Y,scoring='jaccard_macro')
print("KNeighbors Cross Validation jaccard %.2f%%" % (np.mean(score['test_score'])*100))
y_pred = model1.predict(X1)
predictions = [round(value) for value in y_pred]
# evaluate predictions
accuracy1 = accuracy_score(Y1, predictions)
precision1 = precision_score(Y1, predictions, average='macro')
recall1 = recall_score(Y1, predictions, average='macro')
f1 = f1_score(Y1,predictions,average='macro')
jaccard = jaccard_score(Y1, predictions, average='macro')
print("KNeighbors Model Accuracy: %.2f%%" % (accuracy1 * 100.0))
print("KNeighbors Model Precision: %.2f%%" % (precision1 * 100.0))
print("KNeighbors Model Recall: %.2f%%" % (recall1 * 100.0))
print("KNeighbors Model F1: %.2f%%" % (f1 * 100.0))
print("KNeighbors Model jaccard: %.2f%%" % (jaccard * 100.0))
print()
## Gaussian Model
model2 = GaussianNB()
model2.fit(X,Y)
score=cross_validate(model2,X,Y,scoring='accuracy')
print("Gaussian Cross Validation accuracy %.2f%%" % (np.mean(score['test_score'])*100))
score=cross_validate(model2,X,Y,scoring='precision_macro')
print("Gaussian Cross Validation precision %.2f%%" % (np.mean(score['test_score'])*100))
score=cross_validate(model2,X,Y,scoring='recall_macro')
print("Gaussian Cross Validation recall %.2f%%" % (np.mean(score['test_score'])*100))
score=cross_validate(model2,X,Y,scoring='f1_macro')
print("Gaussian Cross Validation f1 %.2f%%" % (np.mean(score['test_score'])*100))
score=cross_validate(model2,X,Y,scoring='jaccard_macro')
print("Gussian Cross Validation jaccard %.2f%%" % (np.mean(score['test_score'])*100))
y_pred = model2.predict(X1)
predictions = [round(value) for value in y_pred]
# evaluate predictions
accuracy1 = accuracy_score(Y1, predictions)
precision1 = precision_score(Y1, predictions, average='macro')
recall1 = recall_score(Y1, predictions, average='macro')
f1 = f1_score(Y1,predictions,average='macro')
jaccard = jaccard_score(Y1, predictions, average='macro')
print("Gaussian Model Accuracy: %.2f%%" % (accuracy1 * 100.0))
print("Gaussian Model Precision: %.2f%%" % (precision1 * 100.0))
print("Gaussian Model Recall: %.2f%%" % (recall1 * 100.0))
print("Gaussian Model F1: %.2f%%" % (f1 * 100.0))
print("Guassian Model jaccard: %.2f%%" % (jaccard * 100.0))
print()
# Discriminant Analysis
model3 = LinearDiscriminantAnalysis(solver="svd")
seed = 42
model3.fit(X,Y)
score=cross_validate(model3,X,Y,scoring='accuracy')
print("Discriminant Analysis Cross Validation accuracy %.2f%%" % (np.mean(score['test_score'])*100))
score=cross_validate(model3,X,Y,scoring='precision_macro')
print("Discriminant Analysis Cross Validation precision %.2f%%" % (np.mean(score['test_score'])*100))
score=cross_validate(model3,X,Y,scoring='recall_macro')
print("Discriminant Analysis Cross Validation recall %.2f%%" % (np.mean(score['test_score'])*100))
score=cross_validate(model3,X,Y,scoring='f1_macro')
print("Discriminant Analysis Cross Validation f1 %.2f%%" % (np.mean(score['test_score'])*100))
score=cross_validate(model3,X,Y,scoring='jaccard_macro')
print("Discriminant Analysis Cross Validation jaccard %.2f%%" % (np.mean(score['test_score'])*100))
y_pred = model3.predict(X1)
predictions = [round(value) for value in y_pred]
# evaluate predictions
accuracy1 = accuracy_score(Y1, predictions)
precision1 = precision_score(Y1, predictions, average='macro')
recall1 = recall_score(Y1, predictions, average='macro')
f1 = f1_score(Y1,predictions,average='macro')
jaccard = jaccard_score(Y1, predictions, average='macro')
print("Discriminant Analysis Model Accuracy: %.2f%%" % (accuracy1 * 100.0))
print("Discriminant Analysis Model Precision: %.2f%%" % (precision1 * 100.0))
print("Discriminant Analysis Model Recall: %.2f%%" % (recall1 * 100.0))
print("Discriminant Analysis Model F1: %.2f%%" % (f1 * 100.0))
print("Discriminant Analysis Model jaccard: %.2f%%" % (jaccard * 100.0))
print()
#Random Forest
model6 = RandomForestClassifier()
model6.fit(X,Y)
score=cross_validate(model6,X,Y,scoring='accuracy')
print("Random Forest Cross Validation accuracy %.2f%%" % (np.mean(score['test_score'])*100))
score=cross_validate(model6,X,Y,scoring='precision_macro')
print("Random Forest Cross Validation precision %.2f%%" % (np.mean(score['test_score'])*100))
score=cross_validate(model6,X,Y,scoring='recall_macro')
print("Random Forest Cross Validation recall %.2f%%" % (np.mean(score['test_score'])*100))
score=cross_validate(model6,X,Y,scoring='f1_macro')
print("Random Forest Cross Validation f1 %.2f%%" % (np.mean(score['test_score'])*100))
score=cross_validate(model6,X,Y,scoring='jaccard_macro')
print("Random Forest Cross Validation jaccard %.2f%%" % (np.mean(score['test_score'])*100))
y_pred = model6.predict(X1)
predictions = [round(value) for value in y_pred]
# # evaluate predictions
accuracy1 = accuracy_score(Y1, predictions)
precision1 = precision_score(Y1, predictions, average='macro')
recall1 = recall_score(Y1, predictions, average='macro')
f1 = f1_score(Y1,predictions,average='macro')
jaccard = jaccard_score(Y1, predictions, average='macro')
print("Random Forest Model Accuracy: %.2f%%" % (accuracy1 * 100.0))
print("Random Forest Model Precision: %.2f%%" % (precision1 * 100.0))
print("Random Forest Model Recall: %.2f%%" % (recall1 * 100.0))
print("Random Forest Model F1: %.2f%%" % (f1 * 100.0))
print("Random Forest Model jaccard: %.2f%%" % (jaccard * 100.0))