TypeError:在0维数组Python上进行迭代 [英] TypeError: iteration over a 0-d array Python
问题描述
我正在尝试编写一个非常基本的最近邻居计算.我基本上想看看t是什么样子,但出现此类型错误.当我要求功能返回时,它说".当我要求它转向列表时,抛出"TypeError:在0维数组Python上进行迭代"
I am trying to write a very basic nearest neighbor calculation. I basically want to see what t looks like but I got this type error. When I asked the funciton to return just t it said "". When I asked it to turn to list it threw "TypeError: iteration over a 0-d array Python "
请问该如何解决?
...
t = np.array(map(lambda v:
map(lambda w: distance(v, w, L), x_train.values),
x_test.values))
...
完整跟踪:
推荐答案
问题是np.array
不使用迭代器,您需要先将其转换为list
,如下所示:
The problem is np.array
does not take an iterator, you need convert to list
first, as below:
t = np.array(list(map(lambda v: map(lambda w: distance(v, w, L),
x_train.values), x_test.values)))
根据numpy.array
文档 a>,必填参数必须为:
As per numpy.array
documentation, the required parameter must be:
一个数组,任何暴露数组接口的对象,一个对象,其 数组方法返回一个数组或任何(嵌套的)序列.
An array, any object exposing the array interface, an object whose array method returns an array, or any (nested) sequence.
或者,使用 numpy.fromiter
并记住提供dtype
,例如dtype=float
.
这篇关于TypeError:在0维数组Python上进行迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!