Python: Concatenate many dicts of numpy arrays with same keys and size

2024/10/8 20:35:33

I have a function called within a loop that returns a dict (dsst_mean) with roughly 50 variables. All variables are numpy arrays of length 10.

The loop iterates roughly 3000 times. I'm current concatenating towards the end of each loop so that I have an 'dsst_mean_all' dict that grows larger on each iteration.

source = [dsst_mean_all, dsst_mean]                
for key in source[0]:                    dsst_mean_all[key] = np.concatenate([d[key] for d in source])

It works, but I know this isn't efficient. I also have problems with initialization of the 'dsst_mean_all' dict. (I'm current using dict.fromkeys() to do this.)

My question is: what are some options to do this more efficiently? I'm thinking I could store the dsst_mean dicts in a list and then do one concatenate at the end. But I'm not sure if holding 3000+ dicts of numpy arrays in memory is a good idea. I know this depends on the size, but unfortunately right now I dont have an estimate of the size of each 'dsst_mean' dict.

Thanks.

Answer

Normally we recommend collecting values in a list, and making an array once, at the end. The new thing here is we need to iterate on the keys of a dictionary to do this collection.

For example:

A function to make the individual dictionaries:

In [804]: def foo(i):...:     return {k:np.arange(5) for k in ['A','B','C']}...: 
In [805]: foo(0)
Out[805]: 
{'A': array([0, 1, 2, 3, 4]),'B': array([0, 1, 2, 3, 4]),'C': array([0, 1, 2, 3, 4])}

A collector dictionary:

In [806]: dd = {k:[] for k in ['A','B','C']}

Iteration, collecting arrays in the lists:

In [807]: for _ in range(3):...:     x = foo(None)...:     for k,v in dd.items():...:         v.append(x[k])...:         
In [808]: dd
Out[808]: 
{'A': [array([0, 1, 2, 3, 4]), array([0, 1, 2, 3, 4]), array([0, 1, 2, 3, 4])],'B': [array([0, 1, 2, 3, 4]), array([0, 1, 2, 3, 4]), array([0, 1, 2, 3, 4])],'C': [array([0, 1, 2, 3, 4]), array([0, 1, 2, 3, 4]), array([0, 1, 2, 3, 4])]}

Another iteration on the dictionary to turn the lists into some sort of array (stack, concatenate, your choice):

In [810]: for k,v in dd.items():...:     dd[k]=np.stack(v,axis=0)...:     
In [811]: dd
Out[811]: 
{'A': array([[0, 1, 2, 3, 4],[0, 1, 2, 3, 4],[0, 1, 2, 3, 4]]), 'B': array([[0, 1, 2, 3, 4],[0, 1, 2, 3, 4],[0, 1, 2, 3, 4]]), 'C': array([[0, 1, 2, 3, 4],[0, 1, 2, 3, 4],[0, 1, 2, 3, 4]])}

A list of 3000 arrays of length 10 will take up somewhat more memory than one array of 30,000 numbers, but not drastically more.

You could collect the whole dictionaries in one list the first time around, but you still need to combine those into on dictionary like this.

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

Related Q&A

intersection of 2 objects of different types

i want to take the intersection of: ((e, 13.02338360095244), (a, 11.820318700775383), (o, 9.20172171683253), (s, 7.635081506807498), (n, 7.547469320471335), (i, 7.219915745772025), (r, 6.70492704072287…

Enemy Projectiles Attack Way To Fast Problem

I am trying to make my enemy bullets attack the player but its attacking way to fast I dont know why VIDEO my enemy bullets class# enemys bulletsksud = pygame.image.load("heart.png")class Boo…

How to find the maximum digit of an integer? [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 10 years ago.This p…

How to use python to generate a magazine cover? [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

Why do I get this error? NoneType object has no attribute shape in opencv

Im working on real-time clothing detection. so i borrowed the code from GitHub like this:https://github.com/rajkbharali/Real-time-clothes-detection but (H, W) = frame.shape[:2]:following error in last …

Efficiently concatenate two strings from tuples in a list?

I want to concatenate the two string elements in a list of tuplesI have this:mylist = [(a, b), (c, d), (e, f), (g, h)] myanswer = []for tup1 in mylist:myanswer.append(tup1[0] + tup[1])Its working but i…

How to assert that a function call does not return an error with unittest?

Is there anyway with unittest to just assert that a function call does not result in an error, whether it is a TypeError, IOError, etc.example:assert function(a,b) is not errororif not assertRaises fun…

How to calculate Python float-number-th root of float number

I found the following answer here on Stackoverflow:https://stackoverflow.com/a/356187/1829329But it only works for integers as n in nth root:import gmpy2 as gmpyresult = gmpy.root((1/0.213), 31.5).real…

Need help making a Hilbert Curve using numbers in Python

I want to make a function that will create a Hilbert Curve in python using numbers. The parameters for the function would be a number and that will tell the function how many times it should repeat. To…

How do I separate each list [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…