-
Notifications
You must be signed in to change notification settings - Fork 0
/
clustering.py
executable file
·130 lines (112 loc) · 3.94 KB
/
clustering.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
#!/usr/bin/python
import numpy as np
import os
import re
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
from scipy.cluster.hierarchy import dendrogram, linkage
import csv
import sys
from sklearn.metrics import euclidean_distances
from sklearn import manifold
import random
import itertools
import pandas as pd
from sklearn.cluster import DBSCAN
seed = np.random.RandomState(seed=100)
markers = ['o', 'v', 'h', 'H', 'o', 'v', 'h', 'H', 'h', 'H', 'o', 'v', 'h']
colors = ['b', 'r','g','c','y','m', 'b', 'r','g','c','y','m','y','m', 'b']
combs = list(itertools.product(markers, colors))
for index in [1]:
data = pd.read_csv('datasets/normalized_' + str(index) + '.csv', delimiter=' ')
C = data[['ID','engagement','length','current_result']]
users = C['ID'].unique()
clusters = 3
ff = open('datasets/user_models.csv','w')
user_models = []
perf_models = []
eng_models = []
userID = []
for user in users:
userID.append(user)
D = C.loc[C['ID']==user]
um = [-1.0,-1.0,-1.0,-1.0, 0.0, 0.0, 0.0, 0.0]
perf = [-1.0,-1.0,-1.0,-1.0]
eng = [0.0, 0.0, 0.0, 0.0]
for i, l in enumerate([3,5,7,9]):
L = D.loc[D['length']==l]
wins = len(L.loc[L['current_result']==1])
losses = len(L.loc[L['current_result']==-1])
if wins == 0:
um[i] = 0.0
perf[i] = 0.0
elif losses == 0:
um[i] = 1.0
perf[i] = 1.0
else:
um[i] = wins/float(wins+losses)
perf[i] = wins/float(wins+losses)
um[i+4] = L['engagement'].mean()
eng[i] = L['engagement'].mean()
user_models.append(um)
perf_models.append(perf)
eng_models.append(eng)
ff.write(user)
for m in um:
ff.write(' ' + str(m))
ff.write('\n')
ff.close()
#print user_models
#labels = ["performance_engagement", "performance_based", "engagement_based"]
#models = [user_models, perf_models, eng_models]
labels = ["performance", "engagement", "both"]
models = [perf_models, eng_models, user_models]
clusterID = []
for model, label in zip(models, labels):
M = model
model -= np.asarray(model).mean()
similarities = euclidean_distances(model)
mds = manifold.MDS(n_components=2, max_iter=300, eps=1e-6, random_state=seed, dissimilarity="precomputed", n_jobs=2)
pos = mds.fit(similarities).embedding_
#for i, p in enumerate(pos):
# plt.plot(p[0], p[1], combs[i][0], markersize=9, color = combs[i][1])
#plt.title('Multidimensional Scaling')
#plt.savefig(label + "_mds.png")
#plt.close()
# CLUSTERING on 2-D
kmeans = KMeans(n_clusters=clusters, random_state=0).fit(model)
#kmeans = DBSCAN(eps = 0.1, min_samples=15).fit(pos)
#print kmeans.labels_
mm = ['b', 'r', 'g', 'c']
first = [1,1,1,1]
for i, p in enumerate(pos):
if first[kmeans.labels_[i]]:
plt.plot(p[0], p[1], 'o', markersize=9, color = mm[kmeans.labels_[i]], label = 'cluster_' + str(kmeans.labels_[i] + 1))
first[kmeans.labels_[i]] = 0
else:
plt.plot(p[0], p[1], 'o', markersize=9, color = mm[kmeans.labels_[i]])
clusterID.append(kmeans.labels_[i])
plt.legend()
plt.title('Clustering using MDS')
plt.savefig('figures/'+ label + '_clustering.png')
plt.close()
f = open('datasets/user_models_' + str(label) + '.csv','w')
if label == "peformance":
f.write("ID cluster p1 p2 p3 p4\n")
if label == "engagement":
f.write("ID cluster e1 e2 e3 e4\n")
if label == "both":
f.write("ID cluster p1 p2 p3 p4 e1 e2 e3 e4\n")
for u, c, m in zip(userID, kmeans.labels_, M):
f.write(u + ' ' + str(c))
for mm in m:
f.write(' ' + str(mm))
f.write('\n')
f.close()
#f = open('datasets/user_models_clusters.csv','w')
#f1 = open('datasets/user_models.csv','w')
#f2 = open('datasets/user_clusters.csv','w')
#for a,b, model in zip(userID, clusterID, perf_models):
# f.write(str(a) + ' ' + str(b) + ' ' + str(model[0]) + ' ' + str(model[1]) + ' ' + str(model[2]) + ' ' + str(model[3]) + '\n')
# f1.write(str(a) + ' ' + str(model[0]) + ' ' + str(model[1]) + ' ' + str(model[2]) + ' ' + str(model[3]) + '\n')
# f2.write(str(a) + ' ' + str(b) + '\n')