0

I have a 2d array that contains other arrays but the arrays are of different sizes. I want to find the mean of each individual array that is contained in that 2d array without using a loop.

I want to take the mean of array_combined in the code below.

import numpy as np
array1=np.array([1,2,3,4,5])
array2=np.array([1,2,3])
array3=np.array([1,2,3,4,5])
list_combined = [array1,array2,array3]
array_combined = np.array(list_combined)
  • How can a 2D array contain arrays of different sizes? – navneethc Jan 20 '23 at 14:38
  • This array in the code above is an example of an array that contains other arrays of different sizes.@navneethc – Syed Muhammad Zain Jan 20 '23 at 14:39
  • i already tried .mean(axis=1) and .mean(axis=0) by taking transpose of array_combined but they didnt work.@AminS – Syed Muhammad Zain Jan 20 '23 at 14:43
  • No, you have 3 separate 1D arrays, then you put them in a list -- that's two different data structures. The mechanics of a 2D array is different: each of its rows must have equal length. If you don't want to go through a for loop, you could just define your combined array as `array_combined = np.array([array1.mean(), array2.mean(), array3.mean()])`. – navneethc Jan 20 '23 at 14:45
  • Im supposed to find the mean of the arrays inside the arrays i cannot change array_combined. @navneethc – Syed Muhammad Zain Jan 20 '23 at 14:47
  • array_combined .mean(axis=1) this should work otherwise i don't know what are u trying to achieve. – Amin S Jan 20 '23 at 14:49
  • @AminS a list does not have a `mean()` method. – navneethc Jan 20 '23 at 14:51
  • @SyedMuhammadZain can you add to your post the `array_combined` you have or a simplified example? – navneethc Jan 20 '23 at 14:52
  • i have to take the mean of array combined not list combined @AminS. I just used list combined as a way to create array combined. – Syed Muhammad Zain Jan 20 '23 at 14:52
  • not list. actually numyp object ( array ) @navneethc – Amin S Jan 20 '23 at 14:53
  • @AminS, yeah your original post contained the list. :) – navneethc Jan 20 '23 at 14:54
  • Basically i have already found the result using np.mean with for loop and appending it in a list or array but i want to do it without using for loop through numpy or array manipulation. The array combined i had is also like this .@navneethc – Syed Muhammad Zain Jan 20 '23 at 14:56

2 Answers2

0

If lengths are equal.

import numpy as np
list_combined=np.array([[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5]])

print(list_combined.mean(axis=1))
#
''' 
array([3., 3., 3.])
'''
#
#

if not

import numpy
import numpy as np
list_combined=np.array([[1,2,3,4,5],[1,2,3,numpy.NAN,numpy.NAN],[1,2,3,4,5]])

print(np.nanmean(list_combined,axis=1))
#
''' 
[3. 2. 3.]
'''
#
#


Amin S
  • 546
  • 1
  • 14
  • The problem here is, the OP's arrays are of unequal lengths. – navneethc Jan 20 '23 at 14:55
  • This is not the answer to the question. I wanted to ask about the unequal length mean without using loops.@Amin S – Syed Muhammad Zain Jan 20 '23 at 14:59
  • This is still not the the solution np.nan are added as elements to the second array. I just want to find the mean of array_combined as it is written above in my question. @AminS – Syed Muhammad Zain Jan 20 '23 at 15:05
  • u cannot use array_combined = np.array(list_combined) if lengths are not equal. @SyedMuhammadZain edit the question then ask precisely or let me know how did u achieve that because as u know should be equal as an array. – Amin S Jan 20 '23 at 15:07
  • This array_combined is actually output from a different code and it is achieved after splitting an array.@AminS – Syed Muhammad Zain Jan 20 '23 at 15:11
  • ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (3,) + inhomogeneous part. "" Your code gives this error. i don't know more. @SyedMuhammadZain – Amin S Jan 20 '23 at 15:12
  • I am not getting any error I am using jupyter notebook. @AminS – Syed Muhammad Zain Jan 20 '23 at 15:17
  • have u run your code fully once in jupyter ? can u print it ? weird actually. that was the whole point in my approach. – Amin S Jan 20 '23 at 15:26
0

This should solve your problem:

import numpy as np
array1=np.array([1,2,3,4,5])
array2=np.array([1,2,3])
array3=np.array([1,2,3,4,5])
list_combined = [array1,array2,array3]
array_combined = np.array(list_combined)

mean_sub_arrays = np.vectorize(np.mean)(array_combined)

Ran it on Google Colab to verify and attached screenshot.1

Copied from solution provided by @vielkind on this link: Average of elements in a subarray