第一:
# make some z vlues
z = numpy.sin(xy[:,1]-0.2*xy[:,1])
最奇怪的是它相当于:
z = numpy.sin(0.8*xy[:, 1])
所以我不知道为什么会这样写。可能有错别字?
接下来,
# whiten them
z = whiten(z)
whitening 只是对总体方差进行归一化。在这里查看演示:
>>> z = np.sin(.8*xy[:, 1]) # the original z
>>> zw = vq.whiten(z) # save it under a different name
>>> zn = z / z.std() # make another 'normalized' array
>>> map(np.std, [z, zw, zn]) # standard deviations of the three arrays
[0.42645, 1.0, 1.0]
>>> np.allclose(zw, zn) # whitened is the same as normalized
True
这对我来说并不明显为什么它会变白。无论如何,继续前进:
# let scipy do its magic (k==3 groups)
res, idx = kmeans2(numpy.array(zip(xy[:,0],xy[:,1],z)),3)
让我们把它分成两部分:
data = np.array(zip(xy[:, 0], xy[:, 1], z))
这是一种奇怪(且缓慢)的写作方式
data = np.column_stack([xy, z])
无论如何,您从两个数组开始并将它们合并为一个:
>>> xy.shape
(30, 2)
>>> z.shape
(30,)
>>> data.shape
(30, 3)
然后是data 传递给kmeans 算法:
res, idx = vq.kmeans2(data, 3)
所以现在您可以看到传递给算法的是 3d 空间中的 30 个点,而令人困惑的部分是点集是如何创建的。