Skip to content Skip to sidebar Skip to footer

Numpy Array Directional Mean Without Dimension Reduction

How would I do the following: With a 3D numpy array I want to take the mean in one dimension and assign the values back to a 3D array with the same shape, with duplicate values of

Solution 1:

You can use the keepdims keyword argument to keep that vanishing dimension, e.g.:

>>> a = np.random.randint(10, size=(4, 4)).astype(np.double)
>>> a
array([[ 7.,  9.,  9.,  7.],
       [ 7.,  1.,  3.,  4.],
       [ 9.,  5.,  9.,  0.],
       [ 6.,  9.,  1.,  5.]])
>>> a[:] = np.mean(a, axis=0, keepdims=True)
>>> a
array([[ 7.25,  6.  ,  5.5 ,  4.  ],
       [ 7.25,  6.  ,  5.5 ,  4.  ],
       [ 7.25,  6.  ,  5.5 ,  4.  ],
       [ 7.25,  6.  ,  5.5 ,  4.  ]])

Solution 2:

You can resize the array after taking the mean:

In [24]: a = np.array([[1, 1, 2, 2],
[2, 2, 1, 0],
[2, 3, 2, 1],
[4, 8, 3, 0]])
In [25]: np.resize(a.mean(axis=0).astype(int), a.shape)
Out[25]: 
array([[2, 3, 2, 0],
       [2, 3, 2, 0],
       [2, 3, 2, 0],
       [2, 3, 2, 0]])

Solution 3:

In order to correctly satisfy the condition that duplicate values of the means appear in the direction they were derived, it's necessary to reshape the mean array to a shape which is broadcastable with the original array.

Specifically, the mean array should have the same shape as the original array except that the length of the dimension along which the mean was taken should be 1.

The following function should work for any shape of array and any number of dimensions:

def fill_mean(arr, axis):
    mean_arr = np.mean(arr, axis=axis)
    mean_shape = list(arr.shape)
    mean_shape[axis] = 1
    mean_arr = mean_arr.reshape(mean_shape)   
    return np.zeros_like(arr) + mean_arr

Here's the function applied to your example array which I've called a:

>>> fill_mean(a, 0)
array([[ 2.25,  3.5 ,  2.  ,  0.75],
       [ 2.25,  3.5 ,  2.  ,  0.75],
       [ 2.25,  3.5 ,  2.  ,  0.75],
       [ 2.25,  3.5 ,  2.  ,  0.75]])

>>> fill_mean(a, 1)
array([[ 1.5 ,  1.5 ,  1.5 ,  1.5 ],
       [ 1.25,  1.25,  1.25,  1.25],
       [ 2.  ,  2.  ,  2.  ,  2.  ],
       [ 3.75,  3.75,  3.75,  3.75]])

Solution 4:

Construct the numpy array

import numpy as np
data = np.array(
    [[1, 1, 2, 2],
     [2, 2, 1, 0],
     [1, 1, 2, 2],
     [4, 8, 3, 0]]
)

Use the axis parameter to get means along a particular axis

>>>means = np.mean(data, axis=0)>>>means
array([ 2.,  3.,  2.,  1.])

Now tile that resulting array into the shape of the original

>>> print np.tile(means, (4,1))
[[ 2.  3.  2.  1.]
 [ 2.  3.  2.  1.]
 [ 2.  3.  2.  1.]
 [ 2.  3.  2.  1.]]

You can replace the 4,1 with parameters from data.shape

Post a Comment for "Numpy Array Directional Mean Without Dimension Reduction"