Last active
August 29, 2015 14:01
-
-
Save demiurg/f3689b78c354c5a5a578 to your computer and use it in GitHub Desktop.
simple clusters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #FOR DBSCAN | |
| from sklearn.cluster import DBSCAN | |
| from sklearn import metrics | |
| from sklearn.datasets.samples_generator import make_blobs | |
| from sklearn.preprocessing import StandardScaler | |
| from random import randrange | |
| import math | |
| from numpy import array, argwhere | |
| #For Kmeans | |
| from numpy import ndarray, array | |
| from scipy.cluster import vq | |
| def kmeans_cluster(data, num_clusters): | |
| features = array(data) | |
| whitened = vq.whiten(features) | |
| #book = array((whitened[0],whitened[2])) | |
| centers, _ = vq.kmeans(whitened, num_clusters) | |
| cluster, _ = vq.vq(whitened, centers) | |
| clusters = {} | |
| for data_idx, cluster_idx in enumerate(cluster): | |
| items = clusters.get(cluster_idx, []) | |
| items.append(features[data_idx]) | |
| clusters[cluster_idx] = items | |
| return clusters.values() | |
| def dbscan_cluster(data): | |
| data = [[d, 0] for d in data] | |
| X = StandardScaler().fit_transform(data) | |
| db = DBSCAN(eps=0.3, min_samples=3).fit(X) | |
| core_samples = db.core_sample_indices_ | |
| labels = db.labels_ | |
| n_clusters_ = len(set(labels)) - (1 if -1 in labels else 0) | |
| clusters = [[] for i in range(len(set(labels)))] | |
| for cluster_idx, l in enumerate(set(labels)): | |
| members = [idx[0] for idx in argwhere(labels == l)] | |
| samples = [idx for idx in core_samples if labels[idx] == l] | |
| for i in members: | |
| x = data[i] | |
| core = i in samples and l != -1 | |
| #clusters[cluster_idx].append([x, core, l]) | |
| clusters[cluster_idx].append(x[0]) | |
| return clusters | |
| if __name__ == '__main__': | |
| '''ex: | |
| [4, 6, 6, 6, 1, 21, 23, 22, 22, 28, 48, 46, 48, 45, 48] | |
| [[21.0, 23.0, 22.0, 22.0, 28.0], [48.0, 46.0, 48.0, 45.0, 48.0], [4.0, 6.0, 6.0, 6.0, 1.0]] | |
| ''' | |
| data = [randrange(1, 10) for i in range(5)] | |
| data += [randrange(20, 30) for i in range(5)] | |
| data += [randrange(40, 50) for i in range(5)] | |
| cluster = dbscan_cluster | |
| print data | |
| print cluster(data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment