python - plt.hist 显示 preprocessing.normalize 后奇怪的图

标签 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

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

最佳答案

这是一种解决方案。您需要 MinMaxScaler,其标准化的默认范围是 (0,1)。欲了解更多信息,请参阅this来自 sklearn 的官方页面。

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)

输出

enter image description here

关于python - plt.hist 显示 preprocessing.normalize 后奇怪的图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52173064/

相关文章:

Python:来自字典的直方图类型字典

python - 如何在 Jupyter 中抑制回溯?

python - 如何使用 Pandas 将两个时间列加在一起

python - 如何设置 curve_fit 的初始值以找到最佳优化,而不仅仅是局部优化?

python - 在 numpy 中沿一个轴对数据进行分箱

python - Numpy直方图,如何在每个bin中取最大值

python - 列表中项目的重复索引

android - 如何让kivy小部件暂停

python - BIC(贝叶斯信息准则)应该更低还是更高

python - 如何仅使用 scikit-learn 消除停用词?