plt.hist在预处理.normalize之后显示奇怪的图

时间:2018-09-04 19:45:18

标签: python scikit-learn histogram 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()

并获得此图像:enter image description here

我将数据归一化并再次检查图:

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

enter image description here

新图的形状不同,在直方图上我看到每个数字都代表一次,与第一个图相比,这对我来说很奇怪。所以我希望smth similat可以用于第一个图,但是范围是0到1。 我在哪里误住了?

1 个答案:

答案 0 :(得分:1)

这是一种解决方案。您需要MinMaxScaler的默认标准化范围是(0,1)。有关更多信息,请参阅sklearn的{​​{3}}官方页面。

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)

输出

this