在sklearn的BallTree中使用加权闵可夫斯基度量



我一直在与Balltree及其提供的不同指标一起玩。但是,当我使用Wminkowski时,重量似乎对结果没有任何影响:

df = pd.DataFrame()
num_features = 4
num_samples = 100
for i in range(num_features):
    df['A_%s'%(i+1)] = np.random.rand(num_samples)
    df['A_%s'%(i+1)] = df['A_%s'%(i+1)].apply(lambda x: 500 - (1000 * x ** 3))
point = np.array([int(1000 * r ** 3) for r in np.random.rand(num_features)]).reshape(1, -1)
weights = [int(10000 * r ** 2) for r in np.random.rand(num_features)]
tree1 = sklearn.neighbors.BallTree(df, metric='minkowski')
tree2 = sklearn.neighbors.BallTree(df, metric='wminkowski', p=2, w=[1] * num_features) # Should be just like tree1
tree3 = sklearn.neighbors.BallTree(df, metric='wminkowski', p=2, w=weights)
d1, i1 = tree1.query(point, k=5)
d2, i2 = tree2.query(point, k=5)
d3, i3 = tree2.query(point, k=5)
print 'Point:'
print point
print 'Weights:'
print weights
print 'Distances:'
print d1
print d2
print d3
print 'Indices:'
print i1
print i2
print i3

,输出为:

Point:
[[ 16  58   0 884]]
Weights:
[2869, 46, 1558, 5835]
Distances:
[[ 451.55203926  537.61234492  601.29840519  601.74059138  647.46934474]]
[[ 451.55203926  537.61234492  601.29840519  601.74059138  647.46934474]]
[[ 451.55203926  537.61234492  601.29840519  601.74059138  647.46934474]]
Indices:
[[61 31 86 43 93]]
[[61 31 86 43 93]]
[[61 31 86 43 93]]

我试图运行上述代码,并使用不同数量的功能和样本来运行上述代码,并且每次三棵树都返回完全相同的输出时,我希望Tree3返回的输出不同。这是为什么?我正在使用Sklearn版本0.18.1。

我的猜测是因为在您将tree2分配给d3,i3的示例中 - 有问题的行:

d3, i3 = tree2.query(point, k=5)

当您的意思是:

d3, i3 = tree3.query(point, k=5)

tree2更改为tree3,在tree3中提供了不同的结果。

相关内容

  • 没有找到相关文章

最新更新