plt.hist 显示 preprocessing.normalize 之后的诡异剧情

plt.hist showing the strange plot after preprocessing.normalize

我是 Python 的新手,所以我执行了这段代码:

test1 = np.array([95, 91, 104, 93, 85, 107, 97, 90, 86, 93, 86, 90, 88, 89, 94, 96, 89, 99, 104, 101, 84, 84, 94, 87, 99, 85, 83, 107, 102, 80, 89, 88, 93, 101, 87, 100, 82, 90, 106, 81, 95])
plt.hist(test1)
plt.show()

并得到这张图片:

在我标准化数据并再次检查绘图后:

plt.gcf().clear()
test2 = preprocessing.normalize([test1])
    plt.hist(test2)
    plt.show()

新图有不同的形状,在直方图上我看到每个数字代表一次,与第一个图相比,这对我来说看起来很奇怪。所以我希望 smth similat 到第一个情节,但范围从 0 到 1。 我哪里弄错了?

这是一种解决方案。您需要 MinMaxScaler,其标准化的默认范围是 (0,1)。有关详细信息,请参阅 sklearnthis 官方页面。

from sklearn import preprocessing

test1 = np.array([95, 91, 104, 93, 85, 107, 97, 90, 86, 93, 86, 90, 88, 89, 94, 96, 89, 99, 104, 101, 84, 84, 94, 87, 99, 85, 83, 107, 102, 80, 89, 88, 93, 101, 87, 100, 82, 90, 106, 81, 95])
min_max_scaler = preprocessing.MinMaxScaler()
test2 = min_max_scaler.fit_transform(test1.reshape(-1, 1));
plt.hist(test2)

输出