-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlda.py
151 lines (133 loc) · 4.56 KB
/
lda.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
# -*- coding: UTF-8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import numpy as np
import json
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from sklearn.datasets.samples_generator import make_classification
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.metrics import silhouette_score
from sklearn.cluster import KMeans
from scipy.spatial.distance import cdist
from sklearn.cluster import AffinityPropagation
from sklearn import metrics
from sklearn.datasets.samples_generator import make_blobs
from scipy.cluster.hierarchy import dendrogram, linkage
from scipy.cluster.hierarchy import cophenet
from scipy.spatial.distance import pdist
# DATA_PATH = 'play_types.data'
DATA_PATH = 'csv/league_data.csv'
PATH = 'cluster_img'
defensive_sides = ['DL', 'DR']
offensive_sides = ['AML', 'ML', 'MR', 'AMR']
defensive_centers = ['DC', 'DEFENDER']
keeper = ['KEEPER', 'GOALKEEPER', 'GK']
midfielders = ['DMC', 'MC', 'AMC', 'MIDFIELDER']
forwards = ['FW', 'FORWARD']
simple_positions = {
'defensive_sides': defensive_sides,
'offensive_sides': offensive_sides,
'defensive_centers': defensive_centers,
'keeper': keeper,
'midfielders': midfielders,
'forwards': forwards
}
def load_json(file_name):
with open(file_name) as json_data:
d = json.load(json_data)
return d
def get_positions(data):
result = {}
positions = []
for item in data:
simple_position = []
for position in get_position(item):
for key, value in simple_positions.iteritems():
if position in value:
simple_position.append(key)
simple_position = list(set(simple_position))
list.sort(simple_position)
result[item] = ','.join(simple_position)
positions.append(result[item])
positions = list(set(positions))
list.sort(positions)
for item in data:
result[item] = positions.index(result[item])
print len(data)
print positions
print len(positions)
return result
def get_position(original_position):
position = []
for item in original_position.split(' '):
if len(item.split('(')) > 1:
main_position = item.split('(')[0]
sides = item.split('(')[1].split(')')[0]
for side in sides:
position.append((main_position + side).upper())
else:
position.append(item.upper())
list.sort(position)
return position
def load_data():
f = open(DATA_PATH)
player_positions = load_json('data/positions.json')
positions = get_positions(
list(set([v['Position'] for k, v in player_positions.iteritems()])))
classes = []
used_positions = []
features = []
names = []
max_length = 99999
current_length = 0
for line in f:
if current_length >= max_length:
print current_length
break
current_length += 1
words = line.rstrip().split(',')
# Store player names
names.append(words[0].decode('utf-8', 'ignore'))
classes.append(positions[player_positions[words[0].decode(
'utf-8', 'ignore')]['Position']])
# Store features of each player
features.append([float(i) for i in words[1:]])
print 'Length: ' + str(len(features))
f.close()
print 'Shape: ' + str(np.array(features).shape)
print list(set(classes))
return (np.array(features), names, classes)
def show_result_sc(data):
X = data
for n_cluster in range(2, 80):
kmeans = KMeans(n_clusters=n_cluster).fit(X)
label = kmeans.labels_
sil_coeff = silhouette_score(X, label, metric='euclidean')
print("For n_clusters={}, The Silhouette Coefficient is {}".format(
n_cluster, sil_coeff))
X, names, y = load_data()
fig = plt.figure()
lda = LinearDiscriminantAnalysis(n_components=2)
lda.fit(X, y)
X_new = lda.transform(X)
show_result_sc(X_new)
# 2D
# plt.scatter(X_new[:, 0], X_new[:, 1], marker='o', c=y)
# 3D
# ax = Axes3D(fig, rect=[0, 0, 1, 1], elev=30, azim=20)
# plt.scatter(X[:, 0], X[:, 1], X[:, 2], marker='o', c=y)
# for label, x, y in zip(names, X_new[:, 0], X_new[:, 1]):
# plt.annotate(
# label,
# xy=(x, y),
# xytext=(-20, 20),
# textcoords='offset points',
# ha='right',
# va='bottom',
# bbox=dict(boxstyle='round,pad=0.5', fc='yellow', alpha=0.5),
# arrowprops=dict(arrowstyle='->', connectionstyle='arc3,rad=0'))
# for i, txt in enumerate(names):
# plt.annotate(txt.split('_')[0], (X_new[:, 0][i], X_new[:, 1][i]))
plt.show()