Zero scaling without downsizing

How would I do the following:
With a 3-D numpy array, I want to take the mean in one dimension and assign the values ​​back to a 3-D array with the same shape with duplicate means values ​​in the direction they were received ...
I am struggling to work out example in 3D, but in 2D (4x4) it will look something like this I think

array[[1, 1, 2, 2]     
      [2, 2, 1, 0]  
      [1, 1, 2, 2]  
      [4, 8, 3, 0]] 

      

becomes

array[[2, 3, 2, 1]     
      [2, 3, 2, 1]  
      [2, 3, 2, 1]  
      [2, 3, 2, 1]]   

      

I am struggling with np.mean

size loss with average averaging.

+3


source to share


4 answers


You can resize the array after accepting the value:



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]])

      

+1


source


You can use a keyword argument keepdims

to keep this disappearing dimension, for example:



>>> 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.  ]])

      

+3


source


To correctly satisfy the condition that duplicate funds appear in the direction in which they were received, it is necessary that the reshape

middle array has a shape that is broadcast with the original array.

In particular, the middle array must have the same shape as the original array, except that the length of the measurement over which the mean was taken must be 1.

The following function should work for any array shape 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 a function applied to your array of examples, which I named 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]])

      

+1


source


Construct 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 an axis parameter to get funds along a specific axis

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

      

Now drag the resulting array to 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 4,1 with parameters from data.shape

0


source







All Articles