forked from simoncos/lola
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchampion_cluster.py
172 lines (156 loc) · 6.52 KB
/
champion_cluster.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
167
168
169
170
171
172
import sqlite3
import pandas as pd
from sklearn.cluster import KMeans
from sklearn.cluster import AgglomerativeClustering
from scipy import spatial
import matplotlib.pyplot as plt
# TODO: py2 print to py3
# All fields' name
fields = ['kills', 'deaths', 'assists', 'gold_earned', 'magic_damage', 'physical_damage', 'true_damage', 'damage_taken',
'crowd_control_dealt', 'ward_kills', 'wards_placed']
fields_total = ['total_gold_earned','total_magic_damage','total_physical_damage', 'total_true_damage', 'total_damage_taken',
'total_crowd_control_dealt','total_ward_kills','total_wards_placed']
fields_percent = ['gold_percent', 'magic_percent','physical_percent', 'true_percent', 'taken_percent', 'control_percent', 'wardK_percent',
'wardP_percent']
def count_avg_dict(label):
tmp_dict = {'kills': 0, 'deaths': 0, 'assists': 0, 'gold_earned': 0, 'magic_damage': 0, 'physical_damage': 0, 'true_damage': 0,
'damage_taken': 0, 'crowd_control_dealt': 0, 'ward_kills': 0, 'wards_placed': 0}
for name in label_dict[label]:
for key in tmp_dict.keys():
tmp_dict[key] += all_stats[name][key]
for key in tmp_dict.keys():
tmp_dict[key] /= len(label_dict[label])
return tmp_dict
def count_avg_arr(label):
tmp_arr = [0,0,0,0,0,0,0,0,0,0,0]
for name in new_label_dict[label]:
for i in range(0, len(fields)):
tmp_arr[i] += all_stats[name][fields[i]]
for i in range(0,len(tmp_arr)):
tmp_arr[i] /= len(new_label_dict[label])
return tmp_arr
# iterate all champions' name, retrieve every column by fields' name, calculate the average stats per game of each champions
conn = sqlite3.connect('lola.db')
cursor = conn.cursor()
df = pd.read_sql('SELECT * FROM ChampionMatchStats', conn, index_col=['champion'])
all_stats = {}
all_stats_arr = []
names = []
for champion in df.index:
names.append(champion)
kills = df.ix[champion]['kills']/df.ix[champion]['picks']
deaths = df.ix[champion]['deaths']/df.ix[champion]['picks']
assists = df.ix[champion]['assists']/df.ix[champion]['picks']
gold_earned = df.ix[champion]['gold_earned']/df.ix[champion]['picks']
magic_damage = df.ix[champion]['magic_damage']/df.ix[champion]['picks']
physical_damage = df.ix[champion]['physical_damage']/df.ix[champion]['picks']
true_damage = df.ix[champion]['true_damage']/df.ix[champion]['picks']
damage_taken = df.ix[champion]['damage_taken']/df.ix[champion]['picks']
crowd_control_dealt = df.ix[champion]['crowd_control_dealt']/df.ix[champion]['picks']
ward_kills = df.ix[champion]['ward_kills']/df.ix[champion]['picks']
wards_placed = df.ix[champion]['wards_placed']/df.ix[champion]['picks']
tmp_dict = {'kills': kills, 'assists': assists, 'deaths': deaths, 'gold_earned': gold_earned, 'magic_damage': magic_damage,
'physical_damage': physical_damage, 'true_damage': true_damage,'damage_taken': damage_taken, 'crowd_control_dealt': crowd_control_dealt,
'ward_kills': ward_kills,'wards_placed': wards_placed}
tmp_arr = [kills, assists, deaths, gold_earned, magic_damage, physical_damage, true_damage, damage_taken, crowd_control_dealt, ward_kills,
wards_placed]
all_stats[champion] = tmp_dict
all_stats_arr.append(tmp_arr)
#for (k, v) in all_stats.items():
# print k, '\n', v
#------------------k-means------------------#
meandistortions = []
num_clusters = 6
km = KMeans(n_clusters=num_clusters, max_iter=100, n_init=100)
km.fit(all_stats_arr)
label_dict = []
for i in range(0, num_clusters):
label_dict.append([])
i = 0
for label in km.labels_:
label_dict[label].append(names[i])
#print names[i], int(label)
cursor.execute('UPDATE ChampionMatchStats SET label=? WHERE champion=?', (int(label), names[i],))
i += 1
#Show clustering results and average statistics
for i in range(0, len(label_dict)):
print label_dict[i]
print count_avg_dict(i), '\n'
centers = km.cluster_centers_.tolist()
sum_dist = 0
for label in range(0, num_clusters):
for name in label_dict[label]:
sum_dist += spatial.distance.euclidean(all_stats_arr[names.index(name)], centers[label])
#print num_clusters, sum_dist/128
#Calculate distance between clusters
dist_cluster = 0
num_normalize = 0
for i in range(0, num_clusters):
for j in range(i+1, num_clusters):
dist_cluster += spatial.distance.euclidean(centers[i], centers[j])
num_normalize += 1
print 'k-means\nnumber of clusters:', num_clusters
print 'distortion:', sum_dist/128
print 'distance betweeen clusters/distortion: %.5f\n' % (dist_cluster/(num_normalize*sum_dist/128))
'''
meandistortions.append(dist_cluster/(num_normalize*sum_dist/128))
plt.plot(num_clusters, meandistortions, 'bx-')
plt.xlabel('k')
plt.ylabel('Average distance of clusters/distortion')
plt.title('Selecting k with the Elbow Method')
plt.show()
'''
#------------------agglomerative------------------#
meandistortions = []
num_clusters = 6
ward = AgglomerativeClustering(n_clusters=num_clusters, linkage='complete')
ward.fit(all_stats_arr)
new_label_dict = []
for i in range(0, num_clusters):
new_label_dict.append([])
i = 0
for label in ward.labels_:
new_label_dict[label].append(names[i])
i += 1
#Show clustering results and average statistics
'''for i in range(0, len(new_label_dict)):
#print new_label_dict[i]
#print count_avg_arr(i)
print label_dict[i]
print count_avg_dict(i), '\n'
'''
#Find centroids
centroids = []
for i in range(0, num_clusters):
centroids.append(count_avg_arr(i))
sum_dist = 0
#Calcultate distortions
for i in range(0, num_clusters):
for name in new_label_dict[i]:
sum_dist += spatial.distance.euclidean(all_stats_arr[names.index(name)], centroids[i])
#print num, sum_dist/128
#meandistortions.append(sum_dist/128)
#Calculate distance between clusters
dist_cluster = 0
num_normalize = 0
for i in range(0, num_clusters):
for j in range(i+1, num_clusters):
dist_cluster += spatial.distance.euclidean(centroids[i], centroids[j])
num_normalize += 1
print 'agglomerative clustering\nnumber of clusters:', num_clusters
print 'distortion:', sum_dist/128
print 'distance betweeen clusters/distortion: %.5f' % (dist_cluster/(num_normalize*sum_dist/128))
'''
print all_stats['Rumble']
print all_stats['Orianna']
print all_stats['Anivia']
meandistortions.append(dist_cluster/(num_normalize*sum_dist/128))
plt.plot(num_clusters, meandistortions, 'bx-')
plt.xlabel('n')
plt.ylabel('Average distance of clusters/distortion')
plt.title('Selecting n with the Elbow Method\nin Agglomerative Clustering')
plt.show()
'''
cursor.close()
conn.commit()
conn.close()