TypeError: iteration over a 0-d array Python(TypeError:迭代 0-d 数组 Python)
问题描述
我正在尝试编写一个非常基本的最近邻计算.我基本上想看看 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:TypeError:迭代 0-d 数组 Python


- padding='same' 转换为 PyTorch padding=# 2022-01-01
- python-m http.server 443--使用SSL? 2022-01-01
- 沿轴计算直方图 2022-01-01
- pytorch 中的自适应池是如何工作的? 2022-07-12
- 分析异常:路径不存在:dbfs:/databricks/python/lib/python3.7/site-packages/sampleFolder/data; 2022-01-01
- 如何将一个类的函数分成多个文件? 2022-01-01
- 如何在 python3 中将 OrderedDict 转换为常规字典 2022-01-01
- 使用Heroku上托管的Selenium登录Instagram时,找不到元素';用户名'; 2022-01-01
- 如何在 Python 的元组列表中对每个元组中的第一个值求和? 2022-01-01
- python check_output 失败,退出状态为 1,但 Popen 适用于相同的命令 2022-01-01