from os import listdir from os.path import isfile, join import math import numpy as np import matplotlib.pyplot as plt import csv import copy import operator import scipy # load data win_rate_matric = {} win_rate_matric1 = {} with open('matric.txt') as f: for line in f: data = line[0:-1].split(' ') win_rate_matric[(data[0], data[1])] = eval(data[2]) with open('matric1.txt') as f: for line in f: data = line[0:-1].split(' ') win_rate_matric1[(data[0], data[1])] = eval(data[2]) hero_name_list = [] for key in win_rate_matric: if key[0] not in hero_name_list: hero_name_list.append(key[0]) # change the ordering of hero_name_list_x # change the ordering of hero_name_list_y hero_name_list_x = hero_name_list hero_name_list_y = copy.deepcopy(hero_name_list) for t in range(3000): # create dict for hero_name_list_x dict_x = {} for i in range(len(hero_name_list_x)): dict_x[i] = 0 for j in range(len(hero_name_list_y)): if hero_name_list_x[i] != hero_name_list_y[j]: dict_x[i] = dict_x[i] + j * (win_rate_matric[(hero_name_list_x[i], hero_name_list_y[j])]) sorted_x = sorted(dict_x.items(), key=operator.itemgetter(1)) temp_hero_name_list_x = [] for i in sorted_x: temp_hero_name_list_x.append(hero_name_list_x[i[0]]) hero_name_list_x = temp_hero_name_list_x # create dict for hero_name_list_y dict_y = {} for i in range(len(hero_name_list_y)): dict_y[i] = 0 for j in range(len(hero_name_list_x)): if hero_name_list_y[i] != hero_name_list_x[j]: dict_y[i] = dict_y[i] + j * (win_rate_matric[(hero_name_list_x[j], hero_name_list_y[i])]) sorted_y = sorted(dict_y.items(), key=operator.itemgetter(1)) temp_hero_name_list_y = [] for i in sorted_y: temp_hero_name_list_y.append(hero_name_list_y[i[0]]) hero_name_list_y = temp_hero_name_list_y import sklearn from sklearn.cluster import KMeans from sklearn import metrics from sklearn.cluster import KMeans from sklearn.datasets import load_digits from sklearn.decomposition import PCA from sklearn.preprocessing import scale x = [] y = [] c = [] s = [] m = [] for i in range(len(hero_name_list_x)): temp_m = [] for j in range(len(hero_name_list_y)): if hero_name_list_x[i] != hero_name_list_x[j]: temp_m.append(win_rate_matric[(hero_name_list_x[i], hero_name_list_x[j])]) x.append(len(hero_name_list_x) - i) y.append(j) if win_rate_matric[(hero_name_list_x[i], hero_name_list_x[j])] > 0: rgb = (1.0, 0.0, 0.0) else: rgb = (0, 0.5, 0.0) t = abs(win_rate_matric[(hero_name_list_x[i], hero_name_list_x[j])])/3 if t > 1: t = 1 c.append((rgb[0], rgb[1], rgb[2], t)) s.append(5) else: temp_m.append(0) m.append(temp_m) num_clusters = 10 estimator = KMeans(init='k-means++', n_clusters=num_clusters, n_init=10) estimator.fit(np.array(m)) cutoff = [] hero_name_list_x_temp = [] estimator.labels_ for i in range(num_clusters): for j in range(len(hero_name_list_x)): if estimator.labels_[j] == i: hero_name_list_x_temp.append(hero_name_list_x[j]) cutoff.append(len(hero_name_list_x_temp)) hero_name_list_x = hero_name_list_x_temp cutoff = [0] + cutoff name_list = [] for i in range(len(cutoff) - 1): temp_list = hero_name_list_x[cutoff[i]: cutoff[i+1]] name_list.append(temp_list) result = range(10) avg_matrix = [] for i in result: temp = [] for j in result: sum = 0 count = 0 for e in name_list[i]: for e1 in name_list[j]: if e != e1: sum = sum + win_rate_matric[(e, e1)] count = count + 1 temp.append(sum/(count)) avg_matrix.append(temp) def search(l, m): if len(l) == 10: temp = 0 for i in range(10): for j in range(10): d_list = [(-1, 0), (1, 0), (0, -1), (0, 1)] for d in d_list: if d[0] + l[i]< 10 and d[0]+l[i] >= 0 \ and d[1] + l[j]<10 and d[1]+l[j] >= 0: temp = temp + abs(m[l[i]][l[j]] - m[l[i] + d[0]][l[j]+d[1]]) * (1.0/(abs(i-j)+1)**2) return temp, l max = 0 result = [] for i in range(10): if i not in l: t, r = search(l+[i], m) if t > max: result = r max = t return max, result temp, result = search([], avg_matrix) result = [3, 6, 7, 5, 8, 2, 4, 9, 1, 0] temp = [] for i in range(10): temp = temp + name_list[result[i]] hero_name_list_x = temp x = [] y = [] c = [] s = [] m = [] for i in range(len(hero_name_list_x)): temp_m = [] for j in range(len(hero_name_list_y)): if hero_name_list_x[i] != hero_name_list_x[j]: temp_m.append(win_rate_matric[(hero_name_list_x[i], hero_name_list_x[j])]) x.append(len(hero_name_list_x) - i) y.append(j) if win_rate_matric[(hero_name_list_x[i], hero_name_list_x[j])] > 0: rgb = (1.0, 0.0, 0.0) else: rgb = (0, 0.5, 0.0) t = abs(win_rate_matric[(hero_name_list_x[i], hero_name_list_x[j])])/3 if t > 1: t = 1 c.append((rgb[0], rgb[1], rgb[2], t)) s.append(5) else: temp_m.append(0) m.append(temp_m) plt.scatter(x, y, color=c, s=s) #for t1 in cutoff: # plt.plot(range(-1, 120), [len(hero_name_list_x) - t1]*121, '-') # plt.plot([t1]*121, range(-1, 120), '-') plt.show() for i in result: for t in range(3000): hero_name_list_y = hero_name_list_x # create dict for hero_name_list_x dict_x = {} for key in name_list[i]: dict_x[key] = 0 for j in range(len(hero_name_list_y)): if key != hero_name_list_y[j]: dict_x[key] = dict_x[key] + j * (win_rate_matric[(key, hero_name_list_y[j])]) sorted_x = sorted(dict_x.items(), key=operator.itemgetter(1)) temp_hero_name_list_x = [] for item in sorted_x: temp_hero_name_list_x.append(item[0]) name_list[i] = temp_hero_name_list_x hero_name_list_x = [] for item in result: hero_name_list_x = hero_name_list_x + name_list[item]