>>> print(X)
[array([0, 2, 4, ..., 1, 1, 1], dtype=uint8)
array([209, 209, 209, ..., 166, 149, 80], dtype=uint8)
array([161, 159, 167, ..., 192, 186, 194], dtype=uint8)
array([ 49, 48, 30, ..., 169, 197, 222], dtype=uint8)
array([175, 173, 165, ..., 95, 153, 77], dtype=uint8)
array([ 98, 100, 98, ..., 244, 244, 246], dtype=uint8)
array([ 98, 99, 98, ..., 214, 221, 223], dtype=uint8)
array([158, 165, 179, ..., 36, 34, 33], dtype=uint8)
array([177, 168, 166, ..., 185, 183, 178], dtype=uint8)
array([ 46, 45, 50, ..., 240, 237, 246], dtype=uint8)]
当我将数组作为最近邻函数的参数传递时,得到以下错误:
>>> nbrs = NearestNeighbors(n_neighbors=3, algorithm='ball_tree').fit(X)
>>> _, indices = nbrs.kneighbors(X)
ValueError: setting an array element with a sequence.
我读过,当输入数组的形状不是一个可以转换成多维数组的(通用的)“盒子”时,就会发生这种情况。考虑到我的工作,我觉得这很奇怪,但我想这可能是因为我的数组不是我想要的普通“数组数组”。
new_X = []
for i in range(10):
new_X.append(X[i])
new_X = np.array(new_X)
print(new_X)
这是我的结果:
[[ 0 2 4 ... 1 1 1]
[209 209 209 ... 166 149 80]
[161 159 167 ... 192 186 194]
...
[158 165 179 ... 36 34 33]
[177 168 166 ... 185 183 178]
[ 46 45 50 ... 240 237 246]]
还要注意的是,在variable explorer中
new_X
unit8
类型,它显然是一个数组数组。
>>> nbrs = NearestNeighbors(n_neighbors=3, algorithm='ball_tree').fit(new_X)
>>> _, indices = nbrs.kneighbors(new_X)
>>> print(indices)
[[0 1 3]
[1 7 2]
[2 1 3]
[3 6 8]
[4 5 8]
[5 9 4]
[6 3 8]
[7 1 0]
[8 6 3]
[9 5 6]]
NearestNeighbor
功能开启
X
不需要像我在for循环中那样进行操作。有什么办法我可以把我的