問題描述
我正在嘗試編寫一個非常基本的最近鄰計算.我基本上想看看 t 的樣子,但我得到了這種類型的錯誤.當我要求功能返回時,它說".當我要求它轉入列表時,它拋出TypeError:迭代 0-d 數組 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 "
請問我該如何解決這個問題?
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))
...
完整的跟蹤:
推薦答案
問題是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
文檔,必填參數必須是:
As per numpy.array
documentation, the required parameter must be:
一個數組,任何暴露數組接口的對象,一個對象array 方法返回一個數組,或任何(嵌套)序列.
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-d 數組 Python的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!