我在一个大型数据集上运行kmeans,并且我总是得到以下错误:
Error using kmeans (line 145)
Some points have small relative magnitudes, making them effectively zero.
Either remove those points, or choose a distance other than 'cosine'.
Error in runkmeans (line 7)
[L, C]=kmeans(data, 10, 'Distance', 'cosine', 'EmptyAction', 'drop')我的问题是,即使我把1加到所有的向量上,我仍然会得到这个误差。我希望它能通过,但是很明显,还有太多的零的剩余(这就是导致它的原因,对吗?)
我的问题是:什么条件使Matlab决定一个点有一个“小的相对大小”和“有效的零”?
在将数据交给Matlab之前,我希望使用python从数据集中删除所有这些点,因为我需要将我的结果与我在python中处理的黄金标准进行比较。
提前感谢!
编辑-应答
下面给出了正确的答案,但是如果有人通过Google发现了这个问题,下面是如何从python的矩阵中删除“有效的零向量”。每一行(!)是一个数据点,所以如果您运行kmeans,您希望在python或Matlab中进行转置:
def getxnorm(data):
return np.sqrt(np.sum(data ** 2, axis=1))
def remove_zero_vector(data, startxnorm, excluded=[]):
eps = 2.2204e-016
xnorm = getxnorm(data)
if np.min(xnorm) <= (eps * np.max(xnorm)):
local_index=np.transpose(np.where(xnorm == np.min(xnorm)))[0][0]
global_index=np.transpose(np.where(startxnorm == np.min(xnorm)))[0][0]
data=np.delete(data, local_index, 0) # data with zero vector removed
excluded.append(global_index) # add global index to list of excluded vectors
return remove_zero_vector(data, startxnorm, excluded)
else:
return (data, excluded)我肯定有一种更灵活的方法来做这件事,但它会做到的:-)
发布于 2012-05-09 02:49:21
如果使用的是这意味着,那么抛出错误的相关代码是:
case 'cosine'
Xnorm = sqrt(sum(X.^2, 2));
if any(min(Xnorm) <= eps * max(Xnorm))
error(['Some points have small relative magnitudes, making them ', ...
'effectively zero.\nEither remove those points, or choose a ', ...
'distance other than ''cosine''.'], []);
end这就是你的测试。正如您所看到的,重要的是相对大小,因此在所有事情中添加一个只会使事情变得更糟(max(Xnorm)也在变大)。一个很好的解决方法可能是用一个常量来缩放所有数据。
发布于 2012-05-09 06:00:15
在您的其他问题中,数据似乎是标量的。如果您的输入向量只有一个特征/维度,它们之间的余弦距离将始终是未定义的(或零),因为根据定义,它们指向相同的方向(沿单轴)。余弦测度给出了两个矢量之间的夹角,如果向量指向不同的方向(即维数> 1),则只能是非零角。
https://stackoverflow.com/questions/10509167
复制相似问题