TypeError: iteration over a 0-d array Python

随声附和 提交于 2020-05-13 04:43:32

问题


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 "

How do I fix this please?

...

t = np.array(map(lambda v:
             map(lambda w: distance(v, w, L), x_train.values),
             x_test.values)) 

...

Full trace:


回答1:


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)))

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.

Alternatively, use numpy.fromiter and remember to supply dtype, e.g. dtype=float.



来源:https://stackoverflow.com/questions/48643256/typeerror-iteration-over-a-0-d-array-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!