如何使用最近邻算法在Python中将数组输出[0 0 0]转换为[0,0,0]?

问题描述:

我有一个Ubuntu 14.04 32位虚拟机,并使用默认的Python 2.7。我有一个近邻脚本如下:如何使用最近邻算法在Python中将数组输出[0 0 0]转换为[0,0,0]?

import numpy as np 
from sklearn.neighbors import NearestNeighbors 

X = np.array([[28273, 20866, 29961, 27190, 31790, 19714, 8643, 14482, 5384], 
[12343, 45634, 29961, 27130, 33790, 14714, 7633, 15483, 4484]]) 

knn = NearestNeighbors(algorithm='auto', leaf_size=30, n_neighbors=5, p=2, 
radius=1.0, warn_on_equidistant=True).fit(X) 

distance, indices = knn.kneighbors(X[0]) 
print(indices) 

在脚本运行成功,我得到的输出,以及 - [[0 1]](或类似的东西)
的问题是,有没有任何逗号分离阵列中的每个元素。我在网上看到类似的代码和其它代码的输出是一样 - array([[0, 1]])[[0, 1]]
我已经试过print(', '.join(indices))但它抛出一个错误 -
print(', '.join(indices)) TypeError: sequence item 0: expected string, numpy.ndarray found
如何修改脚本,这样我得到类似的输出到上面提到的那个? ([0,1])
在此先感谢您的帮助:)

+0

你试过'列表(索引)'? – Zinki

+0

什么'打印(索引)'结果,一个字符串? – bhansa

+0

如果我使用列表(索引),我根本没有输出@Zinki – user136819

打印(索引)

(打印[0 1])

B =列表(索引)

印片B

(打印[0,1])

转换到这一点名单

list(indices) 

尝试,

print indices 
>>>[[0 1]] 
print list(indices) 
>>>[array([0, 1])] 
print indices.tolist() 
>>>[[0, 1]] 
+0

这将如何工作?什么是'arr',请具体说明。 – bhansa

+0

如果我使用list(indices),我根本没有输出@MohamedThasinah – user136819

+0

我没有收到任何错误。我在我的系统中尝试过。你得到什么错误? –