沿指定轴操作



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

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

和验证:

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

听起来很简单...

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

例如。对于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]  # ???

我该怎么做?

通过沿Python的特定轴播放操作来判断,我认为我可能正在寻找一种创建

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

在索引d处完全有一个:

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

您可以交换轴,以使第一个轴是要标准化的轴。这也应该在现场工作,因为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)

相关内容

  • 没有找到相关文章

最新更新