在这篇文章中,我们将学习使用python绘制直方图和ecdf,然后我们将探究为什么将ecdf作为第一个可视化工具是更好的选择。我们将使用iris机器学习数据集绘制setosa花瓣长度的直方图。
导入所有必需的Python库
import pandas as pdimport numpy as npimport matplotlib.pyplot as pltimport seaborn as snsfrom sklearn.datasets import load_iris
![496acf0d5d8e225e04f3211fad3ee1ae.png](https://i-blog.csdnimg.cn/blog_migrate/8d347018a80d29a775d4abeeca5d17a1.jpeg)
获取iris机器学习数据并将其转换为dataframe
data = load_iris()iris = pd.DataFrame(data= np.c_[data['data'], data['target']], columns= data['feature_names'] + ['target'])
![b42f0e7aff4fba0d3667b1d739199b54.png](https://i-blog.csdnimg.cn/blog_migrate/d0d21bae7816490c6aaf0e5681d16ff0.jpeg)
使用setosa类,并用6个bins绘制其花瓣长度的直方图。
#subsettingsetosa = iris[iris['target'] == 0]#histogramsns.set_style('whitegrid')_ = plt.hist(setosa['petal length (cm)'], bins = 6)plt.title('Histogram')plt.xlabel('setosa petal length (cm)')plt.ylabel('counts')plt.show()
![48b1027db8b57143affd9ab961120fbe.png](https://i-blog.csdnimg.cn/blog_migrate/160a141f20fb35b0dbc86ec161218fd0.jpeg)
您可以从上面的Python代码中看到直方图。请注意直方图的结构,条形高度在1.3到1.7之间。
![9c293f03168e09dca68ebb1cbe155f3d.png](https://i-blog.csdnimg.cn/blog_migrate/3308c4ccb088b1df18c4ada8ca9dc748.jpeg)
想象一下5个bins 的相同直方图,并注意分布如何变化。数据的存储方式可能会改变理解数据的方式。这称为Binning Bias。
![a4c553bb5a9492729ad5be2365f32644.png](https://i-blog.csdnimg.cn/blog_migrate/deadcf472ebd0449bfcfc5dba24f5a04.jpeg)
还有别的选择吗?
ECDF代表经验累积分布函数,您应该更多地使用它来理解数据。当应用于setosa dataframe时,自定义ecdf函数的输出包含两个数组