main.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. from sklearn.cluster import DBSCAN
  2. from sklearn import metrics
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5. from itertools import groupby
  6. import csv_parser
  7. import recog
  8. import metric
  9. if __name__ == '__main__':
  10. data = csv_parser.parse_data_from_csv('test0714.csv')
  11. groups = groupby(data, key = lambda x: x.hwid)
  12. entries = []
  13. for k, grp in groups:
  14. data1 = list(grp)
  15. entries1 = recog.recognize_entries(data)
  16. for i in entries1:
  17. entries.append(i)
  18. print(entries)
  19. x = []
  20. for e in entries:
  21. x.append([e.lon, e.lat])
  22. x = np.array(x)
  23. db = DBSCAN(eps = 10/6400000, min_samples = 3,
  24. metric = lambda x,y:metric.spherical_distance(x,y)).fit(x)
  25. labels = db.labels_
  26. core_samples_mask = np.zeros_like(db.labels_, dtype = bool)
  27. core_samples_mask[db.core_sample_indices_] = True
  28. n_clusters_ = len(set(labels)) - (1 if -1 in labels else 0)
  29. n_noise_ = list(labels).count(-1)
  30. print('Estimated number of clusters: %d' % n_clusters_)
  31. print('Estimated number of noise points: %d' % n_noise_)
  32. if n_clusters_ == 0:
  33. print('can not get any clusters')
  34. plt.plot(x[:,0], x[:,1], 'o')
  35. plt.show()
  36. exit(0)
  37. print("Silhouette Coefficient: %0.3f"
  38. % metrics.silhouette_score(x, labels))
  39. unique_labels = set(labels)
  40. colors = [plt.cm.Spectral(each)
  41. for each in np.linspace(0, 1, len(unique_labels))]
  42. for k, col in zip(unique_labels, colors):
  43. if k == -1:
  44. # Black used for noise.
  45. col = [0, 0, 0, 1]
  46. class_member_mask = (labels == k)
  47. xy = x[class_member_mask & core_samples_mask]
  48. plt.plot(xy[:, 0], xy[:, 1], 'o', markerfacecolor=tuple(col),
  49. markeredgecolor='k', markersize=14)
  50. xy = x[class_member_mask & ~core_samples_mask]
  51. plt.plot(xy[:, 0], xy[:, 1], 'o', markerfacecolor=tuple(col),
  52. markeredgecolor='k', markersize=6)
  53. plt.show()