numpy - 沿指定轴进行操作

时间:2017-03-03 08:23:17

标签: python numpy statistics linear-algebra numpy-broadcasting

所以我想实现矩阵标准化方法。 为此,我被告知

  

减去平均值并除以每个维度的标准差

并验证:

  

在此处理之后,每个维度的均值和单位方差均为零。

这听起来很简单......

import numpy as np
def standardize(X : np.ndarray,inplace=True,verbose=False,check=False):

    ret = X
    if not inplace:
        ret = X.copy()

    ndim = np.ndim(X)

    for d in range(ndim):
        m = np.mean(ret,axis=d)
        s = np.std(ret,axis=d)

        if verbose:
            print(f"m{d} =",m)
            print(f"s{d} =",s)

        # TODO: handle zero s
        # TODO: subtract m along the correct axis
        # TODO: divide by s along the correct axis

    if check:    
        means = [np.mean(X,axis=d) for d in range(ndim)]
        stds  = [np.std(X,axis=d)  for d in range(ndim)]
        if verbose:
            print("means=\n",means)
            print("stds=\n",stds)

        assert all(all(m < 1e-15 for m in mm) for mm in means)
        assert all(all(s == 1.0 for s in ss) for ss in stds)

    return ret

e.g。对于ndim == 2,我们可以得到类似

的内容
A=
 [[ 0.40923704  0.91397416  0.62257397]
  [ 0.15614258  0.56720836  0.80624135]]
m0 = [ 0.28268981  0.74059126  0.71440766]  # can broadcast with ret -= m0
s0 = [ 0.12654723  0.1733829   0.09183369]  # can broadcast with ret /= s0
m1 = [ 0.33333333 -0.33333333]  # ???
s1 = [ 0.94280904  0.94280904]  # ???

我该怎么做?

根据Broadcast an operation along specific axis in python判断,我以为我可能正在寻找创建

的方法
m[None, None, None, .., None, : , None, None, .., None]

索引:中只有一个d

但即使我知道如何做到这一点,我也不确定它是否有效。

1 个答案:

答案 0 :(得分:1)

您可以交换轴,使第一个轴成为您要标准化的轴。这也应该适用,因为swapaxes只返回数据视图。

使用numpy命令swapaxes:

for d in range(ndim):

    m = np.mean(ret,axis=d)
    s = np.std(ret,axis=d)

    ret = np.swapaxes(ret, 0, d)

    # Perform Normalisation of Axis
    ret -= m
    ret /= s

    ret = np.swapaxes(ret, 0, d)