Первый код
Код: Выделить всё
s = open('name_of_file.txt')
s.readline()
data = []
for p in s:
p = p.replace(',', '.')
r = [float(i) for i in p.split()]
data.append(r)
print(len(data))
def dist(p1, p2):
x1, y1, x2, y2 = *p1, *p2
return ((x2 - x1)**2 + (y2 - y1)**2)**0.5
def getCluster(p0):
cluster = [p for p in data if dist(p0, p) < 1]
if len(cluster) > 0:
for p in cluster: data.remove(p)
next_cluster = [getCluster(p) for p in cluster]
for c in next_cluster: cluster.extend(c)
return cluster
clusters = []
while len(data) > 0:
p0 = data.pop()
cluster = [p0] + getCluster(p0)
print(len(cluster), print(cluster[:15]))
clusters.append(cluster)
def center(kl):
m = []
for p in kl:
s = sum(dist(p, p0) for p0 in kl)
m.append([s, p])
return min(m)[1]
centers = [center(c) for c in clusters]
Код: Выделить всё
def getCluster(p0):
cluster = [p for p in data if dist(p0, p) < 1]
if len(cluster) > 0:
for p in cluster: data.remove(p)
next_cluster = [getCluster(p) for p in cluster]
cluster.extend(next_cluster) #this is it
return cluster
Подробнее здесь: https://stackoverflow.com/questions/792 ... ween-codes
Мобильная версия