【发布时间】:2014-08-14 16:20:27
【问题描述】:
我正在尝试在大型数据集(9106 项,100 维)上运行 k-means 聚类。这使得它非常慢,所以我被推荐使用 Charles Elkan (http://cseweb.ucsd.edu/~elkan/kmeansicml03.pdf) 描述的三角不等式。
任何工具箱中是否有任何预先编写的功能可以做到这一点?
我一直在用scikit learn,我的代码如下:
#implement a numpy array to hold the data
data_array = np.empty([9106,100])
#iterate through the data file anad add it to the numpy array
rownum = 0
for row in reader:
if rownum != 0:
print "rownum",rownum
colnum = 0
for col in row:
if colnum !=0:
data_array[rownum-1,colnum-1] = float(col)
colnum+=1
rownum += 1
n_samples, n_features = data_array.shape
n_digits = len(data_array)
labels = None #digits.target
#most of the code below was taken from the example on the scikit learn site
sample_size = 200
print "n_digits: %d, \t n_samples %d, \t n_features %d" % (n_digits,
n_samples, n_features)
len
print 79 * '_'
print ('% 9s' % 'init'
' time inertia homo compl v-meas ARI AMI silhouette')
def bench_k_means(estimator, name, data):
t0 = time()
estimator.fit(data)
print '% 9s %.2fs %i %.3f %.3f %.3f %.3f %.3f %.3f' % (
name, (time() - t0), estimator.inertia_,
metrics.homogeneity_score(labels, estimator.labels_),
metrics.completeness_score(labels, estimator.labels_),
metrics.v_measure_score(labels, estimator.labels_),
metrics.adjusted_rand_score(labels, estimator.labels_),
metrics.adjusted_mutual_info_score(labels, estimator.labels_),
metrics.silhouette_score(data, estimator.labels_,
metric='euclidean',
sample_size=sample_size),
)
bench_k_means(KMeans(init='k-means++', k=n_digits, n_init=10),
name="k-means++", data=data_array)
bench_k_means(KMeans(init='random', k=n_digits, n_init=10),
name="random", data=data_array)
# in this case the seeding of the centers is deterministic, hence we run the
# kmeans algorithm only once with n_init=1
pca = PCA(n_components=n_digits).fit(data_array)
bench_k_means(KMeans(init=pca.components_, k=n_digits, n_init=1),
name="PCA-based",
data=data_array)
print 79 * '_'
【问题讨论】:
-
我还没有集成三角不等式,因为我不确定我的代码是如何工作的。你有什么建议吗?谢谢
标签: python scikit-learn k-means