用pandas_profiling快速探索數(shù)據(jù),算不算EDA(Exploratory Data Analysis)首選工具

拿到數(shù)據(jù)進(jìn)行分析之前,應(yīng)該對(duì)數(shù)據(jù)進(jìn)行探索,所謂的探索性數(shù)據(jù)分析(EDA: Exploratory Data Analysis),了解數(shù)據(jù)集的變量類(lèi)型、大致分布、異常值、缺失值……等等等等。

Report generated with pandas-profiling

在探索Pandas表格時(shí)經(jīng)常會(huì)被用到的命令包括:

  • pandas.info()查看變量數(shù)據(jù)類(lèi)型;
  • pandas.describe()查看數(shù)值型(離散型、日期型)變量的匯總統(tǒng)計(jì)信息。如數(shù)值型變量的非NA值計(jì)數(shù)count、最大值max、最小值min、均值mean、標(biāo)準(zhǔn)差std以及三個(gè)分位數(shù)( [0.25, 0.5, 0.75] );
  • pandas.head()、pandas.tail()查看數(shù)據(jù)集最前(后)5條數(shù)據(jù);
  • pandas.value_counts()查看序列中各個(gè)值的出現(xiàn)頻次;
    此外,可能用到的命令還包括計(jì)算樣本值的偏度skew()和峰度kurt(),此外,單變量的直方圖,雙變量 間的相關(guān)系數(shù)計(jì)算,缺失值的統(tǒng)計(jì)與濾除(替換)等也都是免不了的。
    雖然探索性數(shù)據(jù)分析的方法明確步驟清晰,但要用到這么多命令,難免掛一漏萬(wàn)。何況各個(gè)命令又有一堆參數(shù),要想熟練使用必須經(jīng)過(guò)一段時(shí)間才能熟悉。比如pandas.describe()默認(rèn)只對(duì)數(shù)據(jù)集中的數(shù)值型變量虛擬統(tǒng)計(jì),傳入include參數(shù)才能看到離散型變量的統(tǒng)計(jì)信息。
    pandas.describe()示例

    優(yōu)達(dá)學(xué)城的專(zhuān)欄上看到了pandas_profiling的推薦,似乎找到了快速搞定EDA的利器。正如其名稱(chēng)所示,df.ProfileReport()給出一個(gè)DataFrame表格的全方位快照,便于用戶(hù)了解數(shù)據(jù)集的各類(lèi)信息。

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

不過(guò)在本人的機(jī)器上出了點(diǎn)小插曲。安裝成功后提示錯(cuò)誤,無(wú)法導(dǎo)入pandas_profiling包。

cannot import name 'to_html'.jpg

重新安裝后導(dǎo)入倒是成功了,但無(wú)法運(yùn)行profile_report()命令。
cannot import name 'GridspecLayout'

自己懷疑是版本沖突的原因,在網(wǎng)上搜索了沒(méi)找到直接的答案,不過(guò)看到ImportError: cannot import name 'AppLayout' from 'ipywidgets'一個(gè)類(lèi)似問(wèn)題,提到的解決方法是將ipywidgets制定版本為7.5。照貓畫(huà)虎按此居然將GridspecLayout的importerror也給解決了(竊笑,機(jī)智)。
記下一筆看有沒(méi)有會(huì)碰到同樣問(wèn)題的人。
stackoverflow上關(guān)于ipywidgets錯(cuò)誤的回答

對(duì) pandas 數(shù)據(jù)表進(jìn)行預(yù)覽分析(Profiling)

安裝成功后,使用很簡(jiǎn)單,直接df.profile_report()就行了。以Kaggle上的 ASHRAE 建筑能耗預(yù)測(cè)中的數(shù)據(jù)集為例,本文題圖即為building_metadata.csv中的數(shù)據(jù)快照。

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

有時(shí)候會(huì)遇到Error rendering Jupyter widget: missing widget manager的報(bào)錯(cuò)。

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

pandas_profiling探索報(bào)告示例:

pandas-profiling.gif

第一印象就是生成的報(bào)告內(nèi)容非常全面。包括Overview、Variables、Correlations、Missing values和Sample五個(gè)部分。
Overview概述部分主要就是變量類(lèi)型的分類(lèi)統(tǒng)計(jì),如數(shù)值型變量、日期型變量、離散型變量等分別有幾個(gè)。由于后面還有專(zhuān)門(mén)的Variables報(bào)告部分,所以沒(méi)有像df.info()命令那樣羅列每個(gè)列的數(shù)據(jù)類(lèi)型。
值得一提的是概述部分中的Warnings警告部分。給出了各類(lèi)需要引起注意的提示信息,如下圖官方文檔中提供的NZA (open data from the Dutch Healthcare Authority)報(bào)告所示。
NZA(open data from the Dutch Healthcare Authority)數(shù)據(jù)集快照

包括變量間相關(guān)系數(shù)過(guò)大、NA值(或zero值)的比例過(guò)高、偏度值過(guò)大等等等等,都會(huì)提示warnings。
Variables變量部分的數(shù)據(jù)類(lèi)型及統(tǒng)計(jì)信息,如unique值、NA值、zero值的計(jì)數(shù)及占比等;點(diǎn)開(kāi)Toggle details才是精華所在,單變量分析的各類(lèi)信息基本上都已經(jīng)給出了。
Variables給出各列變量的統(tǒng)計(jì)信息、直方圖等

Correlations部分計(jì)算變量間的(Spearman, Pearson and Kendall)相關(guān)系數(shù):
Correlations結(jié)果

Missing values給出了各列變量中缺失值的相關(guān)信息。Counts是非NA值的計(jì)數(shù);Matrix顯示的時(shí)各變量中NA值出現(xiàn)的位置;Heatmap給出了NA值出現(xiàn)機(jī)率的相關(guān)性,在missingno文檔中將其稱(chēng)為無(wú)效相關(guān)性(Nullity Correlation)。當(dāng)某列出現(xiàn)NA值時(shí)另外一列必定出現(xiàn)NA值,則Nullity Correlation值為1;某列出現(xiàn)NA值時(shí)另外一列必定不出現(xiàn)NA值,則Nullity Correlation值為-1;NA值出現(xiàn)不相干是值為0。Dendrogram部分則是按照NA值繪制的各列的樹(shù)枝狀圖??偟挠∠?,Missing values部分的結(jié)果與另一個(gè)missingno包的結(jié)果非常相像,不知道是不是pandas_profiling作者直接調(diào)用了missingno執(zhí)行的結(jié)果?
Missing values結(jié)果

sample部分最簡(jiǎn)單,相當(dāng)于df.head(10)加df.tail(10)的結(jié)果。
sample部分顯示最前(后)10行數(shù)據(jù)

其它的命令參數(shù)還包括如結(jié)果保存為JSON文件、傳入字典指定直方圖的bins等分?jǐn)?shù)量;對(duì)于大數(shù)據(jù)集指定minimal=True使不進(jìn)行耗時(shí)的相關(guān)系數(shù)計(jì)算等。更詳細(xì)的信息大家可參閱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)

結(jié)論

  • pandas_profiling可以給出DataFrame表格的快照,涵蓋了感興趣的絕大多數(shù)統(tǒng)計(jì)信息,且效率更高;
  • 默認(rèn)參數(shù)下計(jì)算時(shí)間可能會(huì)較長(zhǎng),因此數(shù)據(jù)集較大時(shí)可指定ProfileReport(minimal=True)不進(jìn)行相關(guān)系數(shù)等計(jì)算;
參考資料
  1. pandas.DataFrame.describe
  2. 優(yōu)達(dá)學(xué)城:Python數(shù)據(jù)分析,有哪些不為人知的小技巧?
  3. ImportError: cannot import name 'AppLayout' from 'ipywidgets'
  4. 相關(guān)性分析指標(biāo)-Pearson,Spearman,Kendall,Multual information
  5. https://github.com/ResidentMario/missingno
  6. pandas-profiling官方文檔
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容