数目字生存

用pandas_profiling快速探索数据,算不算EDA(E

2020-01-09  本文已影响0人  askka

拿到数据进行分析之前,应该对数据进行探索,所谓的探索性数据分析(EDA: Exploratory Data Analysis),了解数据集的变量类型、大致分布、异常值、缺失值……等等等等。

Report generated with pandas-profiling
在探索Pandas表格时经常会被用到的命令包括:

pandas_profiling的安装

pandas-profiling官方文档中的安装方法如下:

pip install pandas-profiling
# 直接从github上安装
pip install <https://github.com/pandas-profiling/pandas-profiling/archive/master.zip>
# conda安装
conda install -c conda-forge pandas-profiling

不过在本人的机器上出了点小插曲。安装成功后提示错误,无法导入pandas_profiling包。

cannot import name 'to_html'.jpg
重新安装后导入倒是成功了,但无法运行profile_report()命令。
cannot import name 'GridspecLayout'
自己怀疑是版本冲突的原因,在网上搜索了没找到直接的答案,不过看到ImportError: cannot import name 'AppLayout' from 'ipywidgets'一个类似问题,提到的解决方法是将ipywidgets制定版本为7.5。照猫画虎按此居然将GridspecLayout的importerror也给解决了(窃笑,机智)。
记下一笔看有没有会碰到同样问题的人。
stackoverflow上关于ipywidgets错误的回答

对 pandas 数据表进行预览分析(Profiling)

安装成功后,使用很简单,直接df.profile_report()就行了。以Kaggle上的 ASHRAE 建筑能耗预测中的数据集为例,本文题图即为building_metadata.csv中的数据快照。

weather_train = pd.read_csv(f'weather_train.csv',  encoding = "utf-8", 
                            parse_dates = ['timestamp'],  index_col = 'timestamp')
weather_train.profile_report()

有时候会遇到Error rendering Jupyter widget: missing widget manager的报错。

profile = weather_train.profile_report(title = "Pandas Profiling Reprot")
profile.to_notebook_iframe()
#保存快照为独立的html文件
profile.to_file(output_file="your_report.html")

pandas_profiling探索报告示例:

pandas-profiling.gif
第一印象就是生成的报告内容非常全面。包括Overview、Variables、Correlations、Missing values和Sample五个部分。
Overview概述部分主要就是变量类型的分类统计,如数值型变量、日期型变量、离散型变量等分别有几个。由于后面还有专门的Variables报告部分,所以没有像df.info()命令那样罗列每个列的数据类型。
值得一提的是概述部分中的Warnings警告部分。给出了各类需要引起注意的提示信息,如下图官方文档中提供的NZA (open data from the Dutch Healthcare Authority)报告所示。
NZA(open data from the Dutch Healthcare Authority)数据集快照
包括变量间相关系数过大、NA值(或zero值)的比例过高、偏度值过大等等等等,都会提示warnings。
Variables变量部分的数据类型及统计信息,如unique值、NA值、zero值的计数及占比等;点开Toggle details才是精华所在,单变量分析的各类信息基本上都已经给出了。
Variables给出各列变量的统计信息、直方图等
Correlations部分计算变量间的(Spearman, Pearson and Kendall)相关系数:
Correlations结果
Missing values给出了各列变量中缺失值的相关信息。Counts是非NA值的计数;Matrix显示的时各变量中NA值出现的位置;Heatmap给出了NA值出现机率的相关性,在missingno文档中将其称为无效相关性(Nullity Correlation)。当某列出现NA值时另外一列必定出现NA值,则Nullity Correlation值为1;某列出现NA值时另外一列必定不出现NA值,则Nullity Correlation值为-1;NA值出现不相干是值为0。Dendrogram部分则是按照NA值绘制的各列的树枝状图。总的印象,Missing values部分的结果与另一个missingno包的结果非常相像,不知道是不是pandas_profiling作者直接调用了missingno执行的结果?
Missing values结果
sample部分最简单,相当于df.head(10)加df.tail(10)的结果。
sample部分显示最前(后)10行数据

其它的命令参数还包括如结果保存为JSON文件、传入字典指定直方图的bins等分数量;对于大数据集指定minimal=True使不进行耗时的相关系数计算等。更详细的信息大家可参阅pandas-profiling官方文档

# As a string
json_data = profile.to_json()
# As a file
profile.to_file(output_file="your_report.json")

profile = ProfileReport(df, title='Pandas Profiling Report', style={'full_width':True})
profile

profile = df.profile_report(title='Pandas Profiling Report', plot={'histogram': {'bins': 8}})
profile.to_file(output_file="output.html")

profile = ProfileReport(large_dataset, minimal=True)

结论

参考资料
  1. pandas.DataFrame.describe
  2. 优达学城:Python数据分析,有哪些不为人知的小技巧?
  3. ImportError: cannot import name 'AppLayout' from 'ipywidgets'
  4. 相关性分析指标-Pearson,Spearman,Kendall,Multual information
  5. https://github.com/ResidentMario/missingno
  6. pandas-profiling官方文档
上一篇 下一篇

猜你喜欢

热点阅读