Remove mean from numpy matrix

后端 未结 4 1274
渐次进展
渐次进展 2021-02-07 02:47

I have a numpy matrix A where the data is organised column-vector-vise i.e A[:,0] is the first data vector, A[:,1] is the second and so on

4条回答
  •  说谎
    说谎 (楼主)
    2021-02-07 03:21

    Yes. pylab.demean:

    In [1]: X = scipy.rand(2,3)
    
    In [2]: X.mean(axis=1)
    Out[2]: array([ 0.42654669,  0.65216704])
    
    In [3]: Y = pylab.demean(X, axis=1)
    
    In [4]: Y.mean(axis=1)
    Out[4]: array([  1.85037171e-17,   0.00000000e+00])
    

    Source:

    In [5]: pylab.demean??
    Type:           function
    Base Class:     
    String Form:    
    Namespace:      Interactive
    File:           /usr/lib/pymodules/python2.7/matplotlib/mlab.py
    Definition:     pylab.demean(x, axis=0)
    Source:
    def demean(x, axis=0):
        "Return x minus its mean along the specified axis"
        x = np.asarray(x)
        if axis == 0 or axis is None or x.ndim <= 1:
            return x - x.mean(axis)
        ind = [slice(None)] * x.ndim
        ind[axis] = np.newaxis
        return x - x.mean(axis)[ind]
    

提交回复
热议问题