本文整理汇总了Python中sklearn.cluster.KMeans.cluster_centers_[i,:]方法的典型用法代码示例。如果您正苦于以下问题:Python KMeans.cluster_centers_[i,:]方法的具体用法?Python KMeans.cluster_centers_[i,:]怎么用?Python KMeans.cluster_centers_[i,:]使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.cluster.KMeans
的用法示例。
在下文中一共展示了KMeans.cluster_centers_[i,:]方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: KMeans
# 需要导入模块: from sklearn.cluster import KMeans [as 别名]
# 或者: from sklearn.cluster.KMeans import cluster_centers_[i,:] [as 别名]
# weight = np.ones(num_data)
start = time.clock()
agent = KMeans(num_cluster, init="k-means++",\
max_iter=num_iter_init, precompute_distances=True)
agent.fit(data)
for i in range(num_iter):
membership = agent.predict(data)
for i in range(num_cluster):
# the return is a tuple, we need to use [0]
member_id = np.where(membership == i)[0]
weight_member = weight[member_id]
weight_member = weight_member / weight_member.sum()
weight_member = np.tile(weight_member, [dim, 1]).transpose()
data_member = data[member_id, :]
agent.cluster_centers_[i, :] = \
np.sum(weight_member * data_member, axis=0)
finish = time.clock()
sys.stderr.write(str(data.shape[0]) + " is clustered in " + str(finish -start) + "s\n")
# output the centroid to file
for i in range(num_cluster):
centroid = agent.cluster_centers_[i,:]
data_str = "%f" % centroid[0]
data_str += "".join([",%f" % value for value in centroid[1:]])
print data_str
# # test output
# membership = agent.predict(data)
# for i in range(num_cluster):
# print "center ", i, agent.cluster_centers_[i, :]
# for i in range(num_data):