3

Let's say I have a function (called numpyarrayfunction) that outputs an array every time I run it. I would like to run the function multiple times and store the resulting arrays. Obviously, the current method that I am using to do this -

numpyarray = np.zeros((5))
for i in range(5):
    numpyarray[i] = numpyarrayfunction

generates an error message since I am trying to store an array within an array.

Eventually, what I would like to do is to take the average of the numbers that are in the arrays, and then take the average of these averages. But for the moment, it would be useful to just know how to store the arrays!

Thank you for your help!

5
  • 2
    You can use a regular python list to store these arrays. Commented Aug 31, 2017 at 18:27
  • 3
    If you have to use array : np.zeros((5), dtype=object). Commented Aug 31, 2017 at 18:29
  • So is numpyarrayfunction a function or an array? Commented Aug 31, 2017 at 18:46
  • It's a function, and when I run the function it outputs an array Commented Aug 31, 2017 at 18:48
  • Divakar's solution seems to do exactly what you want. Commented Aug 31, 2017 at 19:06

2 Answers 2

5

As comments and other answers have already laid out, a good way to do this is to store the arrays being returned by numpyarrayfunction in a normal Python list.

If you want everything to be in a single numpy array (for, say, memory efficiency or computation speed), and the arrays returned by numpyarrayfunction are of a fixed length n, you could make numpyarray multidimensional:

numpyarray = np.empty((5, n))
for i in range(5):
    numpyarray[i, :] = numpyarrayfunction

Then you could do np.average(numpyarray, axis = 1) to average over the second axis, which would give you back a one-dimensional array with the average of each array you got from numpyarrayfunction. np.average(numpyarray) would be the average over all the elements, or np.average(np.average(numpyarray, axis = 1)) if you really want the average value of the averages.

More on numpy array indexing.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for your answer! I'm pretty sure I understand your method - but what would n be in this case?
Oh, sorry - it's the length of the arrays returned by numpyarrayfunction. I've edited to make that clear (and therefore also the condition that numpyarrayfunction needs to return arrays of the same size).
Thanks for your help! Last question - each of the arrays that the function outputs has 4500 rows and 2 columns, what value of n should I use in this case?
I believe the correct shape would be numpyarray = np.empty((5, 4500, 2)), but you can determine it programatically by getting a sample output from numpyarrayfunction and then do np.empty((5, *sampleoutput.shape)). The * operator unpacks the shape tuple in the tuple you're constructing for the shape of the np.empty array.
3

I initially misread what was going on inside the for loop there. The reason you're getting an error is because numpy arrays will only store numeric types by default, and numpyarrayfunction is returning a non-numeric value (from the name, probably another numpy array). If that function already returns a full numpy array, then you can do something more like this:

arrays = []
for i in range(5):
  arrays.append(numpyarrayfunction(args))

Then, you can take the average like so:

avgarray = np.zeros((len(arrays[0])))
for array in arrays:
  avgarray += array
avgarray = avgarray/len(arrays)

1 Comment

Thank you for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.