我有一个numpy数组,我希望根据行的固定窗口大小的最大值和最小值来规范数组,然后使用x_normal = (x-min)/(max-min)
。
例如,在下面的数组中,我选择每行中每个4列的最大值和最小值。对于第一行,4列(3,5,3,3) min和max为3,5,然后是(3-3/5-3,5-3/5-3, 3-3/5-3, 3-3/5-3)
,然后对于(9,2,2,5)
,min和max为2,9,然后是(9-2/9-2, 2-2/9-2,2-2/9-2, 5-2/9-2)
等等。
import numpy as np
x = np.array([
[3, 5, 3, 3, 9, 2, 2, 5, 1, 0, 7, 2],
[4, 4, 8, 4, 3, 1, 4, 8, 7, 6, 1, 4]
])
output:
x = np.array([
[0, 1, 0, 0, 1, 0, 0, 3/7, 1/6, 0, 1, 2/6],
[0, 0, 1, 0, 2/7, 0, 3/7, 1, 1, 5/6, 0, 3/6]
])
发布于 2022-04-21 23:11:15
import numpy as np
import numpy.typing as npt
def normalize(array: npt.NDArray) -> npt.NDArray:
minimum = np.expand_dims(np.min(array, axis=1), axis=1)
maximum = np.expand_dims(np.max(array, axis=1), axis=1)
return (array - minimum) / (maximum - minimum)
x = np.array([
[3, 5, 3, 3, 9, 2, 2, 5, 1, 0, 7, 2],
[4, 4, 8, 4, 3, 1, 4, 8, 7, 6, 1, 4]
]).astype(float)
windows_size = 4
for i in range(0, x.shape[1], windows_size):
x[:, i: i + windows_size] = normalize(x[:, i: i + windows_size])
这一办法:
<代码>F 210
https://stackoverflow.com/questions/71961646
复制相似问题