Python:Numpy 数组中的居中特征

Python: Centering Features in Numpy Array

我正在尝试将 numpy 数组中的某些值归一化为以下形状:

import numpy as np
X = np.random.rand(100, 20, 3)

该数据表示 20 个观测值中的每一个都有 100 个时间戳,其中每个观测值具有 3 个维度属性(x、y、z)。我想通过以下方式规范化 x、y、z 维度属性。对于每个维度,我想减去最小值,然后除以得到的最大值(以“居中”维度值)。

我试图通过以下方式做到这一点:

# center all features
for i in range(3):
  X[:][:][i] -= np.min(X[:][:][i])
  X[:][:][i] /= np.max(X[:][:][i])

但是,这不会改变 ith 维度的所有值。

如何以这种方式使我的特征居中?其他人可以提供的任何帮助将不胜感激!

X -= np.amin(X, axis=(0, 1))
X /= np.amax(X, axis=(0, 1))

注意:根据 numpy.amin() 文档(类似于 amax()):

Axis or axes along which to operate. By default, flattened input is used. If this is a tuple of ints, the minimum is selected over multiple axes, instead of a single axis or all the axes as before.

通过指定 axis=(0, 1),我要求 numpy.amin() 通过查看每个 "depth"(第 3 轴)元素的所有行和列来找到最小值。


分步说明:

In [1]: import numpy as np
   ...: np.random.seed(0)
   ...: X = np.random.rand(2, 4, 3)
   ...: print("\nOriginal X:\n%s" % X)
   ...: xmin = np.amin(X, axis=(0, 1))
   ...: print("\nxmin = %s" % xmin)
   ...: X -= xmin
   ...: print("\nSubtracted X:\n%s" % X)
   ...: xmax = np.amax(X, axis=(0, 1))
   ...: X /= xmax
   ...: print("\nDivided X:\n%s" % X)
   ...: 
   ...: 

Original X:
[[[0.5488135  0.71518937 0.60276338]
  [0.54488318 0.4236548  0.64589411]
  [0.43758721 0.891773   0.96366276]
  [0.38344152 0.79172504 0.52889492]]

 [[0.56804456 0.92559664 0.07103606]
  [0.0871293  0.0202184  0.83261985]
  [0.77815675 0.87001215 0.97861834]
  [0.79915856 0.46147936 0.78052918]]]

xmin = [0.0871293  0.0202184  0.07103606]

Subtracted X:
[[[0.4616842  0.69497097 0.53172732]
  [0.45775388 0.4034364  0.57485805]
  [0.35045791 0.8715546  0.8926267 ]
  [0.29631222 0.77150664 0.45785886]]

 [[0.48091526 0.90537824 0.        ]
  [0.         0.         0.76158379]
  [0.69102745 0.84979375 0.90758228]
  [0.71202926 0.44126096 0.70949312]]]

xmax = [0.71202926 0.90537824 0.90758228]

Divided X:
[[[0.64840622 0.76760291 0.5858723 ]
  [0.64288633 0.44559984 0.63339497]
  [0.49219594 0.96264143 0.98352151]
  [0.41615174 0.85213738 0.50448193]]

 [[0.67541502 1.         0.        ]
  [0.         0.         0.8391347 ]
  [0.97050428 0.93860633 1.        ]
  [1.         0.48737748 0.78173972]]]

X[:] 是 python 语法,基本上浅拷贝列表中的每个元素。因此,您要复制矩阵两次,然后尝试按 i 进行索引。你需要X[:, :, i]。有关数组的多维索引的更多信息,请参阅 numpy indexing