【问题标题】:Implement k-means clustering, accelerated using the triangle inequality, in Python (Scikit learn)在 Python (Scikit learn) 中实现 k-means 聚类,使用三角不等式加速
【发布时间】: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


【解决方案1】:

有这个实施 http://mloss.org/software/view/48/ 它会很快更新。

【讨论】:

    【解决方案2】:

    任何工具箱中是否有任何预先编写的功能可以做到这一点?

    没有。有一个 attempt at this algorithm,但它没有合并到 master 中。

    这让它变得很慢

    然后在开始破解复杂算法之前尝试MiniBatchKMeans。它比原版 KMeans 快​​几个数量级,而且通常一样好。

    【讨论】:

    • 感谢您的建议
    猜你喜欢
    • 2018-01-29
    • 2021-01-19
    • 1970-01-01
    • 2017-01-27
    • 2010-11-17
    • 2020-03-10
    • 2013-10-12
    • 2017-10-02
    • 2017-04-06
    相关资源
    最近更新 更多