基于pivot_table向量的KD树二维索引搜索出错

人工智能 2026-07-09

我想基于PCA得到的二维索引。它基于pivot_table向量,在我的自定义度量下工作。尽可能快。这也是我使用KDTree的原因。但正因为如此,出现了以下错误:

ValueError Traceback (most recent call last) 
/tmp/ipykernel_9428/753399497.py in <cell line: 0>() 
     50 return 10.0 
     51 
---> 52 matriks_jarak = metrics(Matrix.values, Matrix.values, 5) 
     53 
     54 model = knn(n_neighbors=6, metric="precomputed") /tmp/ipykernel_9428/753399497.py in metrics(x, x2, n_tetangga) 

     31 def metrics(x, x2, n_tetangga): 
     32 global ind, jarak, ind0_nya, rigth_arr, sudah_print 
---> 33 ind_x = pohon.query(x.reshape(1, -1), k=1, return_distance=False)[0][0] 
     34 ind_x2 = pohon.query(x2.reshape(1, -1), k=1, return_distance=False)[0][0] 
     35 jarak_final = jarak.tolist()[0] sklearn/neighbors/_binary_tree.pxi in sklearn.neighbors._kd_tree.BinaryTree64.query() 
ValueError: query data dimension must match training data dimension

我的最小可复现示例(MRE):

import numpy as np
from sklearn.neighbors import KDTree

dummy = np.array([[1.0, 2.0], [7.0, 6.0]])
tree = KDTree(dummy)

def custom(x, x2):
  return tree.query(x.reshape(1, -1), k=1, return_distance=False)[0][0]

custom(np.array([0, 8, 9, 0]), 0)

这个最小可复现示例的错误信息是:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/tmp/ipykernel_10831/3966020442.py in <cell line: 0>()
      8   return tree.query(x.reshape(1, -1), k=1, return_distance=False)[0][0]
      9 
---> 10 custom(np.array([0, 8, 9, 0]), 0)

/tmp/ipykernel_10831/3966020442.py in custom(x, x2)
      6 
      7 def custom(x, x2):
----> 8   return tree.query(x.reshape(1, -1), k=1, return_distance=False)[0][0]
      9 
     10 custom(np.array([0, 8, 9, 0]), 0)

sklearn/neighbors/_binary_tree.pxi in sklearn.neighbors._kd_tree.BinaryTree64.query()

ValueError: query data dimension must match training data dimension

解决方案

错误显示数据形状有问题。

ValueError: query data dimension must match training data dimension

如果你使用 print() 查看 x.reshape(1, -1),你会看到它创建了 [[0, 8, 9, 0]],但它需要 [[0, 8], [9, 0]]

你需要使用 .reshape(-1, 2),而不是 .reshape(1, -1)


带有更多示例数据的最小可运行代码。

import numpy as np
from sklearn.neighbors import KDTree


def custom(x, x2):
    print('--- custom ---')

    print("before:", x.shape)
    print(x)

    # x = x.reshape(1, -1)
    x = x.reshape(-1, 2)

    print("after :", x.shape)
    print(x)

    results = tree.query(x, k=1, return_distance=False)
    print("results:")
    print(results)

    return results[0][0]


# --- main ---

dummy = np.array([[1.0, 2.0], [7.0, 6.0]])
tree = KDTree(dummy)

custom(dummy, 0)
custom(np.array([0, 8]), 0)
custom(np.array([0, 8, 9, 0]), 0)
custom(np.array([0, 8, 9, 0, 3, 0]), 0)

结果:

--- custom ---
before: (2, 2)
[[1. 2.]
 [7. 6.]]
after : (2, 2)
[[1. 2.]
 [7. 6.]]
results:
[[0]
 [1]]
--- custom ---
before: (2,)
[0 8]
after : (1, 2)
[[0 8]]
results:
[[0]]
--- custom ---
before: (4,)
[0 8 9 0]
after : (2, 2)
[[0 8]
 [9 0]]
results:
[[0]
 [1]]
--- custom ---
before: (6,)
[0 8 9 0 3 0]
after : (3, 2)
[[0 8]
 [9 0]
 [3 0]]
results:
[[0]
 [1]
 [0]]
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。

相关文章