Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from math import dist
- import matplotlib.pyplot as plt
- from sklearn.cluster import DBSCAN
- data = open('q.txt').readlines()
- a = []
- for e in data:
- a.append(list(map(float, e.replace(',', '.').split())))
- db = DBSCAN(eps=0.27, min_samples=3)
- labels = db.fit_predict(a)
- plt.figure(figsize=(10, 10))
- colors = ['red', 'blue', 'green', 'yellow', 'purple']
- clusters = [[], [], [], [], []]
- for i in range(len(a)):
- if labels[i] == -1:
- plt.scatter(a[i][0], a[i][1], color='black', s=20)
- else:
- clusters[labels[i]].append(a[i])
- plt.scatter(a[i][0], a[i][1], color=colors[labels[i]], s=20)
- plt.show()
- cen = []
- for i in range(5):
- c_best = -1
- rst_best = 10000000000
- for c in clusters[i]:
- rst = 0
- for e in clusters[i]:
- rst += dist(e, c)
- if rst < rst_best:
- c_best = c
- rst_best = rst
- cen.append(c_best)
- print(int(((cen[0][0] + cen[1][0] + cen[2][0] + cen[3][0]) / 4) * 10000), int(((cen[0][1] + cen[1][1] + cen[2][1] + cen[3][1]) / 4) * 10000))
Advertisement
Add Comment
Please, Sign In to add comment