forked from HadXu/DeepLearningTutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest3.py
More file actions
35 lines (23 loc) · 846 Bytes
/
test3.py
File metadata and controls
35 lines (23 loc) · 846 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/usr/bin/python
#coding=utf-8
import time
import numpy as np
import pylab as pl
from sklearn.cluster import KMeans
from sklearn.metrics.pairwise import euclidean_distances
from sklearn.datasets.samples_generator import make_blobs
np.random.seed(0)
centers = [[1,1], [-1,-1], [1, -1]]
k = len(centers)
x , labels = make_blobs(n_samples=3000, centers=centers, cluster_std=.7)
kmeans = KMeans(init='k-means++', n_clusters=3, n_init = 10)
t0 = time.time()
kmeans.fit(x)
t_end = time.time() - t0
colors = ['r', 'b', 'g']
for k , col in zip( range(k) , colors):
members = (kmeans.labels_ == k )
pl.plot( x[members, 0] , x[members,1] , 'w', markerfacecolor=col, marker='.')
pl.plot(kmeans.cluster_centers_[k,0], kmeans.cluster_centers_[k,1], 'o', markerfacecolor=col,\
markeredgecolor='k', markersize=10)
pl.show()