Extract numpy arrays from a dictionary iteratively

Asked

Viewed 157 times

0

I have a dictionary of the following kind:

data = {'neuron4':Numpy array, 'stim':Numpy array, 'neuron3':Numpy array, 'neuron2':Numpy array, 'neuron1':Numpy array}

I tried to do something like

for k,v in data.items():
   ...

However, I’m not sure how to associate the new variables (with the same names as the Keys in the dictionary) with the arrays (values). I know what I can do

neuron4 = data['neuron4']

But I wanted to avoid writing one by one.

My intention is to create these variables, take the average data['neuron4']. Mean(Axis=0) of each one and then visualize them in a graph.

Thank you very much!!

P.S.: As the user jfaccioni commented below, I don’t need to create variables. I can directly use Dict values.

  • The idea of a dictionary is precisely to associate values with names (keys), which you can work with as being "variables" within the same structure. There’s nothing you can do with the variable neuron4 of expression neuron4 = data['neuron4'] that you could no longer do by accessing data['neuron4'] - and better yet, iteratively.

  • Got it... so you advise me to use directly data['neuron4'] in plt.Plot as something of the kind plt.plot(data['stim'], data['neuron4'].mean(axis=0))? With only 4 variables (keys) is easy, but what would you tell me to do if I had 100 of them? Writing one by one wouldn’t be appropriate.

  • It depends on what/how you want to plot the data. Writing keys one by one would be laborious, but the point I raised is that writing variables one by one would also give the same job. On the other hand, if you always want to plot data['stim'] as values of X and data[neuron<algum_número>] as the values of Y, it makes sense to write xs = data['stim'], iterate over the key-value pairs of the dictionary and plot each value as y: for k, v in data.items(): if k != 'stim': plt.plot(xs, v.mean(axis=0)). But that was not clear in your question.

  • It worked!! Thank you very much. (I edited the question with that remark of yours.)

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.