main.py 2.6 KB

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