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/Pairs/"
16 device1 = "tplink-bulb-on"
17 device2 = "tplink-bulb-off"
18 filename1 = device1 + ".txt"
19 filename2 = device2 + ".txt"
24 # PLOTTING FOR DEVICE ON EVENT
25 # Read and create an array of pairs
26 with open(path + filename1, "r") as pairs:
29 # We will see a pair and we need to split it into xpoint and ypoint
30 xpoint, ypoint = line.split(", ")
31 pair = [int(xpoint), int(ypoint)]
34 # Formed array of pairs
36 X = np.array(pairsArr);
40 # min_samples = minimum number of members of a cluster
41 db = DBSCAN(eps=30, min_samples=trig - 5).fit(X)
42 core_samples_mask = np.zeros_like(db.labels_, dtype=bool)
43 core_samples_mask[db.core_sample_indices_] = True
46 # Number of clusters in labels, ignoring noise if present.
47 n_clusters_ = len(set(labels)) - (1 if -1 in labels else 0)
48 #print('Estimated number of clusters: %d' % n_clusters_)
50 # Black removed and is used for noise instead.
51 unique_labels = set(labels)
52 #print("Labels: " + str(labels))
54 colors = [plt.cm.Spectral(each)
55 for each in np.linspace(0, 1, len(unique_labels))]
56 for k, col in zip(unique_labels, colors):
58 # Black used for noise.
61 class_member_mask = (labels == k)
63 print("Unique label: " + str(k) + " with freq: " + str(labels.tolist().count(k)))
64 xy = X[class_member_mask & core_samples_mask]
65 plt.plot(xy[:, 0], xy[:, 1], 'o',
66 markeredgecolor='k', markersize=10)
68 xy = X[class_member_mask & ~core_samples_mask]
69 plt.plot(xy[:, 0], xy[:, 1], 'o', markerfacecolor=tuple(col),
70 markeredgecolor='k', markersize=6)
74 plt.text(pair[0], pair[1], str(pair[0]) + ", " + str(pair[1]) +
75 "\nFreq: " + str(labels.tolist().count(labels[count])), fontsize=10)
78 #====================================================================================================
80 # PLOTTING FOR DEVICE ON EVENT
81 # Read and create an array of pairs
82 with open(path + filename2, "r") as pairs:
85 # We will see a pair and we need to split it into xpoint and ypoint
86 xpoint, ypoint = line.split(", ")
87 pair = [int(xpoint), int(ypoint)]
90 # Formed array of pairs
92 X = np.array(pairsArr);
96 # min_samples = minimum number of members of a cluster
97 db = DBSCAN(eps=10, min_samples=trig - 5).fit(X)
98 core_samples_mask = np.zeros_like(db.labels_, dtype=bool)
99 core_samples_mask[db.core_sample_indices_] = True
102 # Number of clusters in labels, ignoring noise if present.
103 n_clusters_ = len(set(labels)) - (1 if -1 in labels else 0)
104 #print('Estimated number of clusters: %d' % n_clusters_)
106 import matplotlib.pyplot as plt
108 # Black removed and is used for noise instead.
109 unique_labels = set(labels)
110 #print("Labels: " + str(labels))
112 colors = [plt.cm.Spectral(each)
113 for each in np.linspace(0, 1, len(unique_labels))]
114 for k, col in zip(unique_labels, colors):
116 # Black used for noise.
119 class_member_mask = (labels == k)
121 print("Unique label: " + str(k) + " with freq: " + str(labels.tolist().count(k)))
122 xy = X[class_member_mask & core_samples_mask]
123 plt.plot(xy[:, 0], xy[:, 1], 'o',
124 markeredgecolor='k', markersize=10)
126 xy = X[class_member_mask & ~core_samples_mask]
127 plt.plot(xy[:, 0], xy[:, 1], 'o', markerfacecolor=tuple(col),
128 markeredgecolor='k', markersize=6)
131 for pair in pairsArr:
132 plt.text(pair[0], pair[1], str(pair[0]) + ", " + str(pair[1]) +
133 "\nFreq: " + str(labels.tolist().count(labels[count])), fontsize=10)
138 plt.title(device1 + ' & ' + device2 + ' - Estimated number of clusters: %d' % n_clusters_)