-
Notifications
You must be signed in to change notification settings - Fork 3
/
kmeans.py
59 lines (43 loc) · 1.27 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
"""
calcula los centroides que mejor quedan con los Datos
"""
import numpy as np
import random
from cercanos import cercanos
from Centros import centros
from distancia import distancia
def kmeans(puntos,k,it=100):
cent = random.sample(list(puntos),k)
for i in range(it):
clusters = cercanos(puntos,cent)
cent = centros(clusters)
return cent
class KMeansTec:
def __init__(self,n_clusters):
self.k = n_clusters
self.points = None
self.centers = None
def fit(self,X,**kw):
self.points = X
self.centers = kmeans(self.points,self.k,**kw)
def predict(self,X):
"""
Returns Array With Class prediction (Could be improved using nearest 'logic')
"""
if not self.centers:
raise ValueError("Call Fit First!")
prediction = []
for point in X:
min = float('inf')
cidx = 0
for idx,center in enumerate(self.centers):
val = distancia(point,center) #positive
if val < min:
min = val
cidx = idx
prediction.append(cidx)
return prediction
# N =100
# x = np.random.rand(N)
# y = np.random.rand(N)
# data = [[x,y] for x,y in zip(x,y) ]