1 from sklearn.cluster import DBSCAN
2 from sklearn import metrics
3 import matplotlib.cm as cm
5 import matplotlib.pyplot as plt
7 # Create a subplot with 1 row and 2 columns
8 fig, (ax2) = plt.subplots(1, 1)
9 fig.set_size_inches(7, 7)
13 # TODO: Just change the following path and filename
14 # when needed to read from a different file
15 path = "/scratch/July-2018/Pairs2/"
16 device = "dlink-siren-device-off"
17 filename = device + ".txt"
24 # Read and create an array of pairs
25 with open(path + filename, "r") as pairs:
28 # We will see a pair and we need to split it into xpoint and ypoint
29 xpoint, ypoint = line.split(", ")
30 pair = [int(xpoint), int(ypoint)]
33 # Formed array of pairs
35 X = np.array(pairsArr);
39 # min_samples = minimum number of members of a cluster
40 #db = DBSCAN(eps=20, min_samples=trig - 5).fit(X)
41 # TODO: This is just for seeing more clusters
42 db = DBSCAN(eps=20, min_samples=trig - 45).fit(X)
43 core_samples_mask = np.zeros_like(db.labels_, dtype=bool)
44 core_samples_mask[db.core_sample_indices_] = True
47 # Number of clusters in labels, ignoring noise if present.
48 n_clusters_ = len(set(labels)) - (1 if -1 in labels else 0)
50 #print('Estimated number of clusters: %d' % n_clusters_)
52 import matplotlib.pyplot as plt
54 # Black removed and is used for noise instead.
55 unique_labels = set(labels)
56 #print("Labels: " + str(labels))
58 colors = [plt.cm.Spectral(each)
59 for each in np.linspace(0, 1, len(unique_labels))]
60 for k, col in zip(unique_labels, colors):
62 # Black used for noise.
65 class_member_mask = (labels == k)
67 xy = X[class_member_mask & core_samples_mask]
68 plt.plot(xy[:, 0], xy[:, 1], 'o',
69 markeredgecolor='k', markersize=10)
71 xy = X[class_member_mask & ~core_samples_mask]
72 plt.plot(xy[:, 0], xy[:, 1], 'o', markerfacecolor=tuple(col),
73 markeredgecolor='k', markersize=6)
77 #if labels[count] != -1:
78 # If this is not a noise (i.e.,real data)
79 # plt.text(pair[0], pair[1], "Freq: " + str(labels.tolist().count(labels[count])), fontsize=10)
81 if labels[count] == -1:
82 plt.text(pair[0], pair[1], str(pair[0]) + ", " + str(pair[1]), fontsize=10)
84 # Only print the frequency when this is a real cluster
85 plt.text(pair[0], pair[1], str(pair[0]) + ", " + str(pair[1]) +
86 " - Freq: " + str(labels.tolist().count(labels[count])), fontsize=10)
90 plt.title(device + ' - Clusters: %d' % n_clusters_)