如何正确可视化劳埃德算法
How to properly visualize lloyds algorithm
对于一个计算机科学项目,我必须实施 Lloyds-Algorithm,它似乎工作得很好。我想可视化迭代。这也已经可以工作了:
import numpy
import matplotlib.pyplot as plt
# Variables to test
centroids = [[[2, 3],[6, 7]],
[[1, 2],[7, 8]]]
nearest_centroid_of_samples = [[0, 0, 1, 1, 0],
[0, 1, 1 , 1, 0]]
quant_error = [2.123,1.789]
# Actual code
i=0
for c,ncos, qe in zip(centroids, nearest_centroid_of_samples, quant_error):
# My x and y values
xx = [0, 4, 3, 8, 2]
yy = [1, 3, 9, 5, 2]
title = "Iteration Nr.%d" % (i)
plt.title(title)
# A text I would like to appear
text = "Quantisierungsfehler: %f" % (qe)
plt.text(12.5, 3.5, text)
# Adding my clusters to the plot, ncos encodes the color
plt.scatter(xx, yy, c=ncos, marker='o', alpha=1)
# Here I'm adding my centroids (the middle of each cluster) to the plot
for pos in c:
plt.scatter(pos[0], pos[1], c="red", marker="+")
i = i+1
plt.pause(0.25)
plt.show()
这基本上已经给了我想要的。只有一个小问题:它似乎是在前一个迭代之上绘制每个迭代。这对我的数据来说没有问题,因为它们完美匹配而且你看不到这一点。但是质心的红色标记有点错误,并且一直可见 - 更糟糕的是,我想添加的文本有一个更长的十进制数字,变得不可读。
我需要如何绘制它,它绘制了所有新的东西,但仍然在同一张图中?
此致
doofesohr
根据建议编辑了一些代表值。这些不代表 Lloyds-Algorithm 的结果,但仍应显示我的问题所在 is/was.
在您的代码中进行以下更改-
# to control the number of decimal places in the quantisation error
# change %f ----> %.2f for 2 decimal places or how many ever you want
text = "Quantisierungsfehler: %.2f" % (qe)
# to clear the contents of a figure and plot freshly add plt.cla() after plt.pause(0.25) i.e
plt.pause(0.25)
plt.cla()
我会使用 matplotlib 的面向对象接口,您可以在其中直接作用于对象:
import numpy
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
xx = samples[:, 0]
yy = samples[:, 1]
dots = ax.scatter(xx, yy, c='k', marker='o', alpha=1)
text = ax.text(12.5, 3.5, "placeholder")
for i, (c, ncos, qe) in enumerate(zip(centroids, nearest_centroid_of_samples, quant_error)):
ax.set_title("Iteration Nr.%d" % (i))
text.set_text("Quantisierungsfehler: %f" % (qe))
dots.set_color(ncos)
pos = numpy.array([[p[0], p[1]] for p in c])
ax.scatter(pos[:, 0], pos[:, 1], c="red", marker="+")
plt.pause(0.25)
plt.show()
对于一个计算机科学项目,我必须实施 Lloyds-Algorithm,它似乎工作得很好。我想可视化迭代。这也已经可以工作了:
import numpy
import matplotlib.pyplot as plt
# Variables to test
centroids = [[[2, 3],[6, 7]],
[[1, 2],[7, 8]]]
nearest_centroid_of_samples = [[0, 0, 1, 1, 0],
[0, 1, 1 , 1, 0]]
quant_error = [2.123,1.789]
# Actual code
i=0
for c,ncos, qe in zip(centroids, nearest_centroid_of_samples, quant_error):
# My x and y values
xx = [0, 4, 3, 8, 2]
yy = [1, 3, 9, 5, 2]
title = "Iteration Nr.%d" % (i)
plt.title(title)
# A text I would like to appear
text = "Quantisierungsfehler: %f" % (qe)
plt.text(12.5, 3.5, text)
# Adding my clusters to the plot, ncos encodes the color
plt.scatter(xx, yy, c=ncos, marker='o', alpha=1)
# Here I'm adding my centroids (the middle of each cluster) to the plot
for pos in c:
plt.scatter(pos[0], pos[1], c="red", marker="+")
i = i+1
plt.pause(0.25)
plt.show()
这基本上已经给了我想要的。只有一个小问题:它似乎是在前一个迭代之上绘制每个迭代。这对我的数据来说没有问题,因为它们完美匹配而且你看不到这一点。但是质心的红色标记有点错误,并且一直可见 - 更糟糕的是,我想添加的文本有一个更长的十进制数字,变得不可读。
我需要如何绘制它,它绘制了所有新的东西,但仍然在同一张图中?
此致
doofesohr
根据建议编辑了一些代表值。这些不代表 Lloyds-Algorithm 的结果,但仍应显示我的问题所在 is/was.
在您的代码中进行以下更改-
# to control the number of decimal places in the quantisation error
# change %f ----> %.2f for 2 decimal places or how many ever you want
text = "Quantisierungsfehler: %.2f" % (qe)
# to clear the contents of a figure and plot freshly add plt.cla() after plt.pause(0.25) i.e
plt.pause(0.25)
plt.cla()
我会使用 matplotlib 的面向对象接口,您可以在其中直接作用于对象:
import numpy
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
xx = samples[:, 0]
yy = samples[:, 1]
dots = ax.scatter(xx, yy, c='k', marker='o', alpha=1)
text = ax.text(12.5, 3.5, "placeholder")
for i, (c, ncos, qe) in enumerate(zip(centroids, nearest_centroid_of_samples, quant_error)):
ax.set_title("Iteration Nr.%d" % (i))
text.set_text("Quantisierungsfehler: %f" % (qe))
dots.set_color(ncos)
pos = numpy.array([[p[0], p[1]] for p in c])
ax.scatter(pos[:, 0], pos[:, 1], c="red", marker="+")
plt.pause(0.25)
plt.show()