Python np.nditer() - ValueError: Too many operands

2024/11/17 22:44:13

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

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

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

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

ValueError: Too many operands

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

Edit

Small example:

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

indicator_names = ['a', 'b']

Wanted output:

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

Current status:

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

Output:

In: print(indicator_values[0])

Out: [1 2 3 4 5]

In: print(item)

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

In: print(data)

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

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

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

Answer

You are hitting the NPY_MAXARGS limit.

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.

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`.

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}

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

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

and with your function:

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}]

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

Alternative for numpy.choose that allows an arbitrary or at least more than 32 arguments?

Using numpy.array with large number of dimensions

https://en.xdnf.cn/q/120151.html

Related Q&A

Python 3 - Find the Mode of a List

def mode(L):shows = []modeList = []L.sort()length = len(L)for num in L:count = L.count(num)shows.append(count)print List = , LmaxI = shows.index(max(shows))for i in shows:if i == maxI:if modeList == []…

Create list of sublists [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 9 years ago.Improve…

Python error - list index out of range?

Anyone help me see why I keep getting "list index out of range" as an error ?? def printInfo(average):average.sort() # sorts the list of tuples average.reverse() # reverses the list of tu…

Access strings inside a list

A Python list has [12:30,12:45] and I want to access the 12:30 for the first iteration, and on the second iteration I should get 12:45.my_list=[12:30,12:45] for each_value in my_list:print(each_value[0…

How do I install NumPy under Windows 8.1?

How do I install NumPy under Windows 8.1 ? Similar questions/answers on overflow hasnt helped.

Design In-Memory File System - Python - Trie - Leetcode Error

I am trying to solve the below leetcode problem Design a data structure that simulates an in-memory file system. Implement the FileSystem class: FileSystem() Initializes the object of the system. List …

OpenTurns Error when trying to use kernel build [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

How to write data to excel with header using Pandas?

I want to write the excel file with the header isI have data and I want to fill out the excel file. Then the expected result isThis is my code but it does not show as my expected. Could you help me to …

Cooldown format in days discord.py

Im currently making a command which has a cooldown of 5 days, and Im currently using this code for the cooldown. def better_time(self, cd:int):time = f"{cd}s"if cd > 60:minutes = cd - (cd …

From scraper_user.items import UserItem ImportError: No module named scraper_user.items

I am following this guide for scraping data from instagram: http://www.spataru.at/scraping-instagram-scrapy/ but I get this error:mona@pascal:~/computer_vision/instagram/instagram$ ls instagram scrap…