TypeError: iteration over a 0-d array Python

前端 未结 2 1200
既然无缘
既然无缘 2020-12-19 05:42

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

2条回答
  •  抹茶落季
    2020-12-19 06:25

    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.

提交回复
热议问题