Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python K-均值中的质心_Python_Matplotlib - Fatal编程技术网

Python K-均值中的质心

Python K-均值中的质心,python,matplotlib,Python,Matplotlib,代码工作正常,但我有一个问题。我不知道如何在这张图上显示簇的质心。我知道我需要使用变量centoids,但我不知道具体如何使用。请给我一个提示。我不是100%确定您想要什么,但我认为您只是想在这些簇的组合散点图上过度绘制簇的质心,所有这些都在一个图形中(每个簇都有自己的颜色) 按照这些思路可以做到: import math, random, os, operator, matplotlib, matplotlib.pyplot from string import split def Eu

代码工作正常,但我有一个问题。我不知道如何在这张图上显示簇的质心。我知道我需要使用变量centoids,但我不知道具体如何使用。请给我一个提示。

我不是100%确定您想要什么,但我认为您只是想在这些簇的组合散点图上过度绘制簇的质心,所有这些都在一个图形中(每个簇都有自己的颜色)

按照这些思路可以做到:

import math, random, os, operator, matplotlib, matplotlib.pyplot

from string import split


def EuDist(vecA, vecB):
    return math.sqrt(sum(map(lambda x: x * x, [i - j for i, j in zip(vecA, vecB)])))

filename = "points.txt"
FILE = open(filename, "w")
for i in range(33):
    line = str(random.uniform(1, 2) + random.uniform(-1, 1)) + "\t" + str(random.uniform(4, 5) + random.uniform(-1, 1)) + "\n"
    FILE.write(line)
for i in range(33):
line = str(random.uniform(4, 6) + random.uniform(-1, 1)) + "\t" + str(random.uniform(4, 6) + random.uniform(-1, 1)) + "\n"
    FILE.write(line)
for i in range(34):
    line = str(random.uniform(2, 3) + random.uniform(-1, 1)) + "\t" + str(random.uniform(2, 3) + random.uniform(-1, 1)) + "\n"
    FILE.write(line)
FILE.close()

dataFile = open("points.txt")
dataset = []
for line in dataFile:
    lineSplit = split(line[: -2], "\t")
    dataset.append([float(value) for value in lineSplit])

maxIters = input("Enter the maximum number of iterations: ")
center = input("Enter a number of clusters: ")

centoids = random.sample(dataset, center)
m = len(dataset)
cluster = [[] for i in range(len(centoids))]
for i in range(maxIters):
    cluster = [[] for v in range(len(centoids))]
    for j in range(m):
    minK = 0
    minDis = 100
    for k in range(len(centoids)):
        if operator.le(EuDist(dataset[j], centoids[k]), minDis):
            minDis = EuDist(dataset[j], centoids[k])
            minK = k
    cluster[minK].append(j)
for t in range(len(centoids)):
    x0 = sum([dataset[x][0] for x in cluster[t]])
    y0 = sum([dataset[x][1] for x in cluster[t]])
    centoids[k] = [x0 / len(cluster[t]), y0 / len(cluster[t])]

matplotlib.pyplot.plot(hold = False)
colorarr=["b", "r", "y", "g", "p"]
for k in range(len(cluster)):
    clusterPoint = [dataset[x] for x in cluster[k]]
    x0 = [x[0] for x in clusterPoint]
    y0 = [x[1] for x in clusterPoint]
    center = [(x0, y0) for x in clusterPoint]
    matplotlib.pyplot.show(centoids)
    matplotlib.pyplot.hold(True)
    matplotlib.pyplot.scatter(x0, y0, center, c = colorarr[k])
picname = "picture_number_" + str(i + 1) + ".png"
matplotlib.pyplot.savefig(picname)
只需使用此处显示的少数
plt.
行;您不需要更多,当然也不需要
hold
变量或
show
。基本上,你只是简单地在前一个簇的基础上对每个簇进行过拼接,再加上簇的质心

在上一个
scatter
中,我向
color
关键字提供了完整的
colorarr
:这样,每个质心都会获得簇的相应颜色


在您的代码中,它看起来像这样:

from matplotlib import pyplot as plt
import numpy as np

data = {
    'x': np.random.rand(4, 100),
    'y': np.random.rand(4, 100),
}
centoids = {
    'x': np.random.rand(4),
    'y': np.random.rand(4),
}
colorarr = ["b", "r", "y", "g"]

for i, cluster in enumerate(zip(data['x'], data['y'])):
    plt.scatter(cluster[0], cluster[1], s=50, c=colorarr[i])
plt.grid(True)
plt.scatter(centoids['x'], centoids['y'], marker='+', color=colorarr, s=330)
plt.savefig("random.png")

(关于稍微少一点混乱的提示:使用matplotlib中的
导入pyplot
,然后在以后的代码中使用
pyplot.show
(不使用
matplotlib
);我之所以提到它,是因为这样做比较普遍,而且
pyplot
名称空间仍然很清楚(而且非常有名)。
hold=False/True
命令为什么使用?你也许可以把它们去掉;
show
命令也是如此(它一开始不把
centoids
作为参数)。不完全是这样。使用我在问题中输入的代码,我收到了如下内容:。我只想在同一个图形上显示这些簇的质心,没有别的(最好是不同的颜色)。我的示例代码中的最后一行
plt.plot(…)
正好做到了这一点:它在簇上过度绘制了质心。它没有添加任何其他内容。您可以根据需要更改颜色、符号和大小。@MarkTrait我已经更新了我的答案:现在,您应该用各自的簇颜色绘制质心(请记住,它在这里显示得并不好,因为我使用的是随机数据,没有合适的簇或质心)。您的代码非常好,但是我不知道如何在我的脚本中实现它。@MarkTrait好的,我认为这是最简单的部分;查看新的更新。
colorarr=["b", "r", "y", "g", "p"]
for k in range(len(cluster)):
    clusterPoint = [dataset[x] for x in cluster[k]]
    x0 = [x[0] for x in clusterPoint]
    y0 = [x[1] for x in clusterPoint]
    center = [(x0, y0) for x in clusterPoint]
    matplotlib.pyplot.scatter(x0, y0, center, c = colorarr[k])
xcentoids, ycentoids = zip(*centoids)
matplotlib.pyplot.scatter(xcentoids, ycentoids, marker='+', color=colorarr, s=330)
picname = "picture_number_" + str(i + 1) + ".png"
matplotlib.pyplot.savefig(picname)