-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkmeans.py
152 lines (120 loc) · 3.72 KB
/
kmeans.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
# Inicializar el ambiente
import numpy as np
import pandas as pd
import math
import random
import time
import os
import sys
from scipy.spatial import distance
np.set_printoptions(precision=2, suppress=True) # Cortar la impresión de decimales a 1
os.chdir('datos')
LARGER_DISTANCE = sys.maxsize
TALK = True # TALK = True, imprime resultados parciales
# Leer los datos de archivo
df = pd.read_csv("tirosL.csv")
#pone NAN en los tiempos de toque cuando sean menores de 0
df.loc[df["TOUCH_TIME"]<=0,"TOUCH_TIME"]=np.nan
#quita los faltantes
df=df.dropna()
DATA_SET = df.values
DATA_LEN = len(DATA_SET)
# Definir una clase para expresar puntos y su asignación a un cluster
class DataPoint:
def __init__(self, p):
self.value = p[:]
def set_value(self, p):
self.value = p
def get_value(self):
return self.value
def set_cluster(self, cluster):
self.cluster = cluster
def get_cluster(self):
return self.cluster
data = []
def initialize_dataset():
for i in range(DATA_LEN):
point = DataPoint(DATA_SET[i])
point.set_cluster(None)
data.append(point)
return
# --------------------------
# Crear el conjunto de datos
initialize_dataset()
NUM_CLUSTERS = 5
# Definir forma de muestreo; 0 = random, 1=head, 2=tail
SAMPLING_METHOD = 0
centroids = []
def initialize_centroids():
if (TALK) :
print("Centroides inicializados en:")
for c in range(NUM_CLUSTERS):
if (SAMPLING_METHOD == 0) :
which = random.randint(0,DATA_LEN-1)
elif (SAMPLING_METHOD == 1):
which = c
else :
which = DATA_LEN-1 - c
centroids.append(list(DATA_SET[which]))
if (TALK) :
print(centroids[c])
if (TALK) :
print()
return
# --------------------------
# Inicializar los centroides
initialize_centroids()
def update_clusters():
changed = False
for i in range(DATA_LEN):
minDistance = LARGER_DISTANCE
currentCluster = 0
for j in range(NUM_CLUSTERS):
dist = distance.euclidean(data[i].get_value(), centroids[j])
if(dist < minDistance):
minDistance = dist
currentCluster = j
if(data[i].get_cluster() is None or data[i].get_cluster() != currentCluster):
data[i].set_cluster(currentCluster)
changed = True
members = [0] * NUM_CLUSTERS
for i in range(DATA_LEN):
members[data[i].get_cluster()] += 1
if (TALK) :
for j in range(NUM_CLUSTERS):
print("El cluster ", j, " incluye ", members[j], "miembros.")
print()
return changed
# --------------------------
# Actualizar los clusters
KEEP_WALKING = update_clusters()
def update_centroids():
if (TALK) :
print("Los nuevos centroids son:")
for j in range(NUM_CLUSTERS):
means = [0] * DATA_SET.shape[1]
clusterSize = 0
for k in range(len(data)):
if(data[k].get_cluster() == j):
p = data[k].get_value()
for i in range(DATA_SET.shape[1]):
means[i] += p[i]
clusterSize += 1
if(clusterSize > 0):
for i in range(DATA_SET.shape[1]):
centroids[j][i] = means[i] / clusterSize
if (TALK) :
print(centroids[j])
if (TALK) :
print()
return
# --------------------------
# Actualizar los centroides
update_centroids()
while(KEEP_WALKING):
KEEP_WALKING = update_clusters()
if (KEEP_WALKING):
update_centroids()
else :
if (TALK) :
print ("No más cambios.")