如何将一维数据列表规范化到特定范围?

How to normalize 1D list of data to a particular range?

如何将一维数据列表规范化到特定范围 - 从 -1 到 1,好吗? 谢谢

from sklearn import preprocessing

x = [44, -58, -6, 15, -48, -24, -34, -50, -48, 52]
scaler = preprocessing.MinMaxScaler(feature_range=(-1, 1))
d = scaler.fit_transform(x)
print(d)

您可以将 x 的形状更改为 2d,

from sklearn import preprocessing
import numpy as np

x = np.array([44, -58, -6, 15, -48, -24, -34, -50, -48, 52])
scaler = preprocessing.MinMaxScaler(feature_range=(-1, 1))
d = scaler.fit_transform(x.reshape(-1, 1))
print(d)

[[ 0.85454545]
 [-1.        ]
 [-0.05454545]
 [ 0.32727273]
 [-0.81818182]
 [-0.38181818]
 [-0.56363636]
 [-0.85454545]
 [-0.81818182]
 [ 1.        ]]

并且您可以将输出重塑为您要求的形状。如果你想得到一维列表作为输入,你可以做

list(d.reshape(-1))

[0.8545454545454544,
 -1.0,
 -0.05454545454545463,
 0.32727272727272716,
 -0.8181818181818182,
 -0.3818181818181819,
 -0.5636363636363637,
 -0.8545454545454546,
 -0.8181818181818182,
 0.9999999999999999]