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):
61 cluster_col = [1, 0, 0, 1]
63 # Black used for noise.
66 class_member_mask = (labels == k)
68 xy = X[class_member_mask & core_samples_mask]
69 plt.plot(xy[:, 0], xy[:, 1], 'o', markerfacecolor=tuple(cluster_col),
70 markeredgecolor='k', markersize=10)
72 xy = X[class_member_mask & ~core_samples_mask]
73 plt.plot(xy[:, 0], xy[:, 1], 'o', markerfacecolor=tuple(col),
74 markeredgecolor='k', markersize=6)
78 #if labels[count] != -1:
79 # If this is not a noise (i.e.,real data)
80 # plt.text(pair[0], pair[1], "Freq: " + str(labels.tolist().count(labels[count])), fontsize=10)
82 if labels[count] == -1:
83 plt.text(pair[0], pair[1], str(pair[0]) + ", " + str(pair[1]), fontsize=10)
85 # Only print the frequency when this is a real cluster
86 plt.text(pair[0], pair[1], str(pair[0]) + ", " + str(pair[1]) +
87 " : " + str(labels.tolist().count(labels[count])), fontsize=10)
91 plt.title(device + ' - Clusters: %d' % n_clusters_)