-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheventClassification.py
166 lines (136 loc) · 4.86 KB
/
eventClassification.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
166
import networkx as nx
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from quickGraph import getTrainTest
from sklearn.linear_model import Ridge
from sklearn.linear_model import RidgeClassifier
from sklearn.svm import SVC
from sklearn.preprocessing import LabelEncoder
from mlxtend.evaluate import confusion_matrix
from mlxtend.plotting import plot_confusion_matrix
from quickGraph import makeGraph
from sklearn.decomposition import PCA
np.seterr(divide="ignore", invalid="ignore")
def confustionMatrixToGraph(classes, true, predicted, names):
G = nx.Graph()
for event in classes:
G.add_node(event, type="event")
for athlete in names:
G.add_node(athlete, type="athlete")
for name, true, pred in zip(names, y_test, y_pred):
G.add_edge(name, true)
G.add_edge(name, pred)
return G
def testConfusion(clf, X, y):
y_pred = clf.predict(X)
score = clf.score(X, y)
cm = confusion_matrix(y, y_pred)
# Plot it
classes = np.append("", max(np.unique(y), np.unique(y_pred), key=lambda x: len(x)))
fig, ax = plot_confusion_matrix(conf_mat=cm)
ax.set_xticklabels(classes, rotation=90)
ax.set_yticklabels(classes)
ax.set_title("Binary testing error {:.2f}".format(score))
plt.show()
return y_pred, cm
def confusionGraph(true, predicted):
G = nx.MultiDiGraph()
for event in max(np.unique(true), np.unique(predicted), key=lambda x: len(x)):
G.add_node(event)
for t, p in zip(true, predicted):
if t != p and (p, t) not in G.edges:
G.add_edge(t, p)
return G
def drawConfusionGraph(G):
pos = nx.shell_layout(G)
nx.draw_networkx_nodes(G, pos, with_labels=True)
ax = plt.gca()
for e in cg.edges:
ax.annotate(
"",
xy=pos[e[0]],
xycoords="data",
xytext=pos[e[1]],
textcoords="data",
arrowprops=dict(
arrowstyle="<-",
color="0.5",
shrinkA=15,
shrinkB=15,
patchA=None,
patchB=None,
connectionstyle="arc3,rad=rrr".replace("rrr", str(0.1 * e[2])),
),
)
for v in G.nodes:
ax.annotate(v, xy=pos[v] - [0.05, 0.02])
plt.title("Confusion Graph\nDirection goes true label to predicted")
plt.show()
return None
def shapiro_centrality(events, X, y):
# Linear model
le = LabelEncoder().fit(y)
clf = Ridge().fit(X, le.transform(y))
return {node: weight for node, weight in zip(events, clf.coef_)}
def centrality_features(G, X, y, num=10):
centrality = dict()
centrality["shapiro"] = shapiro_centrality()
if __name__ == "__main__":
df = pd.read_csv("./fullDetails.csv", index_col=0)
X_train, y_train, X_test, y_test = getTrainTest(
df, "RPI", "RIT", True, True, "proportion"
)
# Build a classifier
# clf = KNeighborsClassifier(10).fit(X_train, y_train)
# clf = SVC(kernel="rbf", gamma="auto").fit(X_train, y_train)
clf = RidgeClassifier().fit(X_train, y_train)
# clf = RandomForestClassifier(max_depth=8, random_state=0).fit(X_train, y_train)
# clf = MultinomialNB().fit(X_train, y_train) # should be method = 3 or 0
#print(clf.score(X_train, y_train), clf.score(X_test, y_test))
# get confusion matrix
y_pred = clf.predict(X_test)
cm = confusion_matrix(y_test, y_pred)
# Plot it
classes = np.append(
"", max(np.unique(y_pred), np.unique(y_test), key=lambda x: len(x))
)
fig, ax = plot_confusion_matrix(conf_mat=cm, cmap=plt.cm.coolwarm)
ax.set_xticklabels(classes, rotation=90)
ax.set_yticklabels(classes)
ax.set_title("RIT binary testing error {:.3f}".format(clf.score(X_test, y_test)))
plt.show()
exit()
# confusion graph
cg = confusionGraph(y_test, y_pred)
drawConfusionGraph(cg)
exit()
# centrality and weights
weights = shapiro_centrality(df.columns[5:], X_train, y_train)
df.loc[:, sorted(weights, key=lambda x: abs(weights[x]), reverse=True)]
G = makeGraph(df[df.School == "RPI"])
events = [
node
for node, stuff in dict(G.nodes(data=True)).items()
if stuff["type"] == "event"
]
select = sorted(
[x[0] for x in nx.degree_centrality(G).items() if x[0] in events],
key=lambda x: x[1],
reverse=True,
)[:10]
X_train2, y_train2, X_test2, y_test2 = getTrainTest(
df.loc[:, ["Gender", "School", "State", "Year", "Event"] + select],
"RPI",
"RIT",
True,
True,
"feature",
)
clf = RidgeClassifier().fit(X_train2, y_train2)
"{:.3f}, {:.3f}".format(clf.score(X_train2, y_train2), clf.score(X_test2, y_test2))
#PCA attempt
pca = PCA(n_components=10)
X_new = pca.fit_transform(X_train)
clf = RidgeClassifier().fit(X_new, y_train)
clf.score(pca.transform(X_test), y_test)