9b5d1abb0e18ee3f1b7a4cea76f2a5b48eb87469
[pingpong.git] / python_ml / plotting-dbscan.py
1 from sklearn.cluster import DBSCAN
2 from sklearn import metrics
3 import matplotlib.cm as cm
4 import numpy as np
5 import matplotlib.pyplot as plt
6
7 # Create a subplot with 1 row and 2 columns
8 fig, (ax2) = plt.subplots(1, 1)
9 fig.set_size_inches(7, 7)
10
11
12 # Read from file
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"
18 plt.ylim(0, 2000)
19 plt.xlim(0, 2000)
20
21 # Number of triggers
22 trig = 50
23
24 # Read and create an array of pairs
25 with open(path + filename, "r") as pairs:
26         pairsArr = []
27         for line in 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)]
31                 pairsArr.append(pair)
32
33 # Formed array of pairs         
34 #print(pairsArr)
35 X = np.array(pairsArr);
36
37 # Compute DBSCAN
38 # eps = distances
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
45 labels = db.labels_
46
47 # Number of clusters in labels, ignoring noise if present.
48 n_clusters_ = len(set(labels)) - (1 if -1 in labels else 0)
49
50 #print('Estimated number of clusters: %d' % n_clusters_)
51
52 import matplotlib.pyplot as plt
53
54 # Black removed and is used for noise instead.
55 unique_labels = set(labels)
56 #print("Labels: " + str(labels))
57
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     if k == -1:
62         # Black used for noise.
63         col = [0, 0, 0, 1]
64
65     class_member_mask = (labels == k)
66
67     xy = X[class_member_mask & core_samples_mask]
68     plt.plot(xy[:, 0], xy[:, 1], 'o',
69              markeredgecolor='k', markersize=10)
70
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)
74
75 count = 0
76 for pair in pairsArr:
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)
80
81         if labels[count] == -1:
82                 plt.text(pair[0], pair[1], str(pair[0]) + ", " + str(pair[1]), fontsize=10)
83         else:
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)
87         count = count + 1
88
89         
90 plt.title(device + ' - Clusters: %d' % n_clusters_)
91 plt.show()
92
93