Python np.nditer()-ValueError:操作数过多 [英] Python np.nditer() - ValueError: Too many operands

查看:80
本文介绍了Python np.nditer()-ValueError:操作数过多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些方法可以将不同数量的混乱数据传递给此函数,以将标头与数据组合并返回字典列表:

I have a few methods which pass different amount of messy data to this function to combine headers with data and return a list of dictionaries:

def zip_data(self, indicator_names, indicator_values):
    values = [[float(elem) for elem in item] for item in np.nditer(indicator_values)]
    return [dict(zip(indicator_names, row)) for row in values]

基本上就像(他们做的一样):

It's basically something like (they do the same):

def zip_large_data(self, indicator_names, indicator_values):
    data = []
    for item in np.nditer(indicator_values):
        values = []
        values.append(int(item[0]))
        for elem in item[1:]:
            values.append(float(elem))
        data.append(dict(zip(indicator_names, values)))
    return data

问题是,如果传递了20个元素的列表,则效果很好,但是对于40个元素,它会给出错误消息:

The thing is, it works great if a list of 20 elements is passed, but for like 40 it gives the error:

"xy.py"文件,第205行,位于动量指标中 返回self.zip_large_data(momentum_indicator_names,动量_indicator_values)

File "xy.py", line 205, in momentum_indicators return self.zip_large_data(momentum_indicator_names, momentum_indicator_values)

文件"xy.py",第51行,位于zip_large_data中 对于np.nditer(indicator_values)中的项目:

File "xy.py", line 51, in zip_large_data for item in np.nditer(indicator_values):

ValueError:操作数太多

ValueError: Too many operands

np.nditer()可以迭代多少个值?有什么办法可以避免这种情况?

How many values can np.nditer() iterate over? Is there any way to avoid this?

小例子:

indicator_values = [array([1,2,3,4,5]),array([5,10,15,20, 25])]

indicator_values = [array([1, 2, 3, 4, 5]), array([5, 10, 15, 20, 25])]

indicator_names = ['a','b']

indicator_names = ['a', 'b']

想要的输出:

data = [{'a':1,'b':5},{'a':2,'b':10},{'a':3,'b':15},{'一种': 4,'b':20},{'a':5,'b':25}]

data = [{'a': 1, 'b': 5}, {'a': 2, 'b': 10}, {'a': 3, 'b': 15}, {'a': 4, 'b': 20}, {'a': 5, 'b': 25}]

当前状态:

def zip_large_data(self, indicator_names, indicator_values):
    data = []
    print(indicator_values[0])
    for item in np.nditer(indicator_values):
        print(item)
        values = []
        values.append(int(item[0]))
        for elem in item[1:]:
            values.append(float(elem))
        data.append(dict(zip(indicator_names, values)))
        print(data)
        break
    return data

输出:

输入:print(indicator_values [0])

In: print(indicator_values[0])

出局:[1 2 3 4 5]

Out: [1 2 3 4 5]

在:打印(项目)

输出:(array(1),array(5))

Out:(array(1), array(5))

在:打印(数据)

出局:[{'a':1,'b':5}]

Out: [{'a': 1, 'b': 5}]

所以基本上我不想顺序遍历indicator_values,而是每个数组的第一个元素,然后每个数组的第二个元素,等等.我想避免nditer,但不知道怎么做

So basically I do not want to iterate through the indicator_values sequentially, but first elements of every array, then the second elements of every array etc.. I want to avoid nditer, but don't see how

抱歉,英语不是我的母语,第一次使用numpy令人困惑.

Sorry English is not my first language, first time working with numpy, it's confusing.

推荐答案

您正在达到NPY_MAXARGS限制.

我还没有看到nditer这样的用法,所以花了我一些时间才弄清楚发生了什么.然后,我使用python会话测试了我的想法.一个可行的例子会有所帮助.

I haven't seen a use like this for nditer, so it took me a bit to figure out what was happening. And then I had use a python session to test my ideas. A worked example would have helped.

通常,发帖人使用nditer作为简单地遍历数组并进行一些计算的方式.简单迭代(无需nditer) is usually faster. nditer is mainly a stepping stone toward implementing the idea in cython`.

Usually posters use nditer as a way of simply iterating through an array and do some calculation. Simple iteration (without nditer) is usually faster.nditeris mainly a stepping stone toward implementing the idea incython`.

使用数组列表,nditer一起广播它们,然后遍历匹配的元素.类似于常见的Python列表zip习惯用法(函数名暗含).

With a list of arrays, nditer broadcasts them together, and then iterates through the matching elements. It's akin to the common Python list zip idiom (as implied by your function name).

In [152]: list(zip('abc',[1,2,3]))
Out[152]: [('a', 1), ('b', 2), ('c', 3)]
In [153]: {k:v for k,v in zip('abc',[1,2,3])}
Out[153]: {'a': 1, 'b': 2, 'c': 3}

定义3个可以相互广播的小阵列:

Defining 3 small arrays that can be broadcast against each other:

In [136]: a = np.array([[1,2],[3,4]])
In [137]: b = np.array([[4],[5]])
In [138]: c = np.array([10])
In [140]: np.broadcast_arrays(a,b,c)
Out[140]: 
[array([[1, 2],
        [3, 4]]), array([[4, 4],
        [5, 5]]), array([[10, 10],
        [10, 10]])]

使用nditer:

In [143]: for x in np.nditer([a,b,c]):
     ...:     print(x)
     ...:     
(array(1), array(4), array(10))
(array(2), array(4), array(10))
(array(3), array(5), array(10))
(array(4), array(5), array(10))

并带有您的功能:

In [155]: zip_large_data('abc',[a,b,c])
Out[155]: 
[{'a': 1, 'b': 4.0, 'c': 10.0},
 {'a': 2, 'b': 4.0, 'c': 10.0},
 {'a': 3, 'b': 5.0, 'c': 10.0},
 {'a': 4, 'b': 5.0, 'c': 10.0}]

如果运行ok,但我用32个操作数执行相同的迭代,但失败,则返回33

If I do same sort of iteration with 32 operands if runs ok, but fails with 33

In [160]: for x in np.nditer([a,b,c]*11):
     ...:     pass 

ValueError: Too many operands

numpy的操作数限制为32(尺寸限制为32).它没有很好的文档记录,并且不会经常出现.我只在使用np.choose的问题中看到过它.

numpy has a 32 operand limit (and 32 dimension limit). It isn't well documented, and doesn't come up often. I've only seen it in questions using np.choose.

替代numpy.choose,允许任意或至少32个以上的参数?

使用具有大量尺寸的numpy.array

这篇关于Python np.nditer()-ValueError:操作数过多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆