獨(dú)家 | 識別并解決數(shù)據(jù)質(zhì)量問題的數(shù)據(jù)科學(xué)家指南
作者:Arunn Thevapalan
翻譯:陳超
校對:王紫岳
在你的下一個項(xiàng)目之前早點(diǎn)這么做將會讓你免于幾周的辛苦和壓力。
如果你在處理現(xiàn)實(shí)數(shù)據(jù)的AI行業(yè)工作,那么你會理解這種痛苦。無論數(shù)據(jù)收集過程多么精簡 ,我們用于建模的數(shù)據(jù)總是一片狼藉。
就像IBM描述的那樣,80/20規(guī)則在數(shù)據(jù)科學(xué)領(lǐng)域同樣適用。數(shù)據(jù)科學(xué)家80%的寶貴時間都花費(fèi)在發(fā)現(xiàn)、清洗以及組織數(shù)據(jù)上。僅僅留下了20%的時間用于真正的數(shù)據(jù)分析。
整理數(shù)據(jù)并不有趣。對于“垃圾輸入進(jìn)去,垃圾輸出出來”這句話,我知道它的重要性,但是我真的不能享受清洗空白格,修正正則表達(dá)式,并且解決數(shù)據(jù)中無法預(yù)料的問題的過程。
根據(jù)谷歌研究:“每個人都想做建模工作,而不是數(shù)據(jù)工作”——我對此感到非常愧疚。另外 ,本文介紹了一種叫做數(shù)據(jù)級聯(lián)(data cascade)的現(xiàn)象,這種現(xiàn)象是指由底層數(shù)據(jù)問題引發(fā)的不利的后續(xù)影響的混合事件。實(shí)際上,該問題目前有三個方面 :
絕大多數(shù)數(shù)據(jù)科學(xué)技術(shù)并不喜歡清理和整理數(shù)據(jù);
只有20%的時間是在做有用的分析;
數(shù)據(jù)質(zhì)量問題如果不盡早處理,將會產(chǎn)生級聯(lián)現(xiàn)象并影響后續(xù)工作。
只有解決了這些問題才能確保清理數(shù)據(jù)是容易,快捷,自然的。我們需要工具和技術(shù)來幫助我們這些數(shù)據(jù)科學(xué)家快速識別并解決數(shù)據(jù)質(zhì)量問題,并以此將我們寶貴的時間投入到分析和AI領(lǐng)域——那些我們真正喜歡的工作當(dāng)中。
在本文當(dāng)中,我將呈現(xiàn)一種幫助我們基于預(yù)期優(yōu)先級來提前識別數(shù)據(jù)質(zhì)量問題的開源工具(https://github.com/ydataai/ydata-quality)。我很慶幸有這個工具存在,并且我等不及要跟你們分享它。
ydata-quality庫拯救數(shù)據(jù)質(zhì)量
作者截圖
Ydata-quality是一個開源的Python庫,用于數(shù)據(jù)管道發(fā)展的多個階段來評估數(shù)據(jù)質(zhì)量。該庫是直觀、易用的,并且你可以直接將其整合進(jìn)入你的機(jī)器學(xué)習(xí)工作流。
對我個人而言,這個庫的好處在于它可以基于數(shù)據(jù)質(zhì)量問題(接下來展開)的優(yōu)先級排序。在我們時間有限時,這是很有幫助的,并且我們也想優(yōu)先處理對我們數(shù)據(jù)質(zhì)量影響較大的問題。
讓我向你們展示一下如何使用一團(tuán)亂麻的數(shù)據(jù)的現(xiàn)實(shí)例子。在這個例子當(dāng)中,我們將會:
加載一個混亂的數(shù)據(jù)集;
分析數(shù)據(jù)質(zhì)量問題;
進(jìn)一步挖掘警告信息;
應(yīng)用策略來減輕這些問題;
檢查在半清洗過后的數(shù)據(jù)的最終質(zhì)量分析報告。
在安裝任何庫之前,最好使用venv或者conda來為項(xiàng)目創(chuàng)建虛擬環(huán)境,一旦這一步完成,在你的終端輸入下面這行代碼來安裝庫:
pip install ydata-quality
現(xiàn)在你的環(huán)境已經(jīng)準(zhǔn)備就緒,讓我們轉(zhuǎn)移到數(shù)據(jù)上吧。
現(xiàn)實(shí)生活中混亂的數(shù)據(jù)
在這個例子當(dāng)中我們將會使用變換的人口普查數(shù)據(jù),你可以從Github 庫(https://github.com/ydataai/ydata-quality/blob/master/datasets/transformed/census_10k.csv)下載。你可以在這個Jupyter Notebook(https://github.com/ydataai/ydata-quality/blob/master/tutorials/main.ipynb)當(dāng)中找到本教程當(dāng)中所有的代碼。我推薦你復(fù)制這個庫或者下載這個notebook來跟上這個例子。
第一步:加載數(shù)據(jù)集
第一步中,我們將會加載數(shù)據(jù)集以及必要的庫。注意,這個庫有多個模塊(偏差&公正,數(shù)據(jù)期望,數(shù)據(jù)關(guān)系,漂移分析,錯誤數(shù)據(jù),標(biāo)簽,缺失值)用于單獨(dú)的數(shù)據(jù)質(zhì)量問題,但是我們可以從DataQuality引擎開始,該引擎把所有的個體引擎打包成了一個類。
from ydata_quality import DataQuality import pandas as pd df = pd.read_csv('../datasets/transformed/census_10k.csv')
第二步:分析數(shù)據(jù)質(zhì)量問題
這是一個漫長的過程,但是DataQuality引擎在抽取所有細(xì)節(jié)方面確實(shí)做的很好 。只要簡單地創(chuàng)建主類并使用evaluate() 方法。
# create the main class that holds all quality modules dq = DataQuality(df=df) # run the tests results = dq.evaluate()
我們將收到一個數(shù)據(jù)質(zhì)量問題的報告。
Warnings: TOTAL: 5 warning(s) Priority 1: 1 warning(s) Priority 2: 4 warning(s) Priority 1 - heavy impact expected: * [DUPLICATES - DUPLICATE COLUMNS] Found 1 columns with exactly the same feature values as other columns. Priority 2 - usage allowed, limited human intelligibility: * [DATA RELATIONS - HIGH COLLINEARITY - NUMERICAL] Found 3 numerical variables with high Variance Inflation Factor (VIF>5.0). The variables listed in results are highly collinear with other variables in the dataset. These will make model explainability harder and potentially give way to issues like overfitting. Depending on your end goal you might want to remove the highest VIF variables. * [ERRONEOUS DATA - PREDEFINED ERRONEOUS DATA] Found 1960 ED values in the dataset. * [DATA RELATIONS - HIGH COLLINEARITY - CATEGORICAL] Found 10 categorical variables with significant collinearity (p-value < 0.05). The variables listed in results are highly collinear with other variables in the dataset and sorted descending according to propensity. These will make model explainability harder and potentially give way to issues like overfitting. Depending on your end goal you might want to remove variables following the provided order. * [DUPLICATES - EXACT DUPLICATES] Found 3 instances with exact duplicate feature values.
讓我們來仔細(xì)分析一下這個報告:
警告(Warning):其中包括數(shù)據(jù)質(zhì)量分析過程中檢測到的問題細(xì)節(jié)。
優(yōu)先級(Priority):對每一個檢測到的問題,基于該問題預(yù)期的影響來分配一個優(yōu)先級(越低的值表明越高的優(yōu)先性)。
模塊(Modules):每個檢測到的問題與某一個模塊(例如:數(shù)據(jù)關(guān)系,重復(fù)值,等)執(zhí)行的數(shù)據(jù)質(zhì)量檢驗(yàn)相關(guān)聯(lián)。
把所有的東西聯(lián)系在一起,我們注意到有五個警告被識別出來,其中之一就是高優(yōu)先級問題。它被“重復(fù)值”模塊被檢測出來,這意味著我們有一整個重復(fù)列需要修復(fù)。為了更深入地處理該問題,我們使用get_warnings() 方法。
輸入下方內(nèi)容:
dq.get_warnings(test="DuplicateColumns")
我們可以看到針對我們想解決問題的詳細(xì)輸出。
[QualityWarning(category='Duplicates', test='Duplicate Columns', description='Found 1 columns with exactly the same feature values as other columns.', priority=<Priority.P1: 1>, data={'workclass': ['workclass2']})]
根據(jù)這一結(jié)果,我們可以看到列workclass和workclass2是完全重復(fù)的[37] ,這可能會產(chǎn)生嚴(yán)重后果。
第三步:使用特定的模塊分析數(shù)據(jù)質(zhì)量問題
數(shù)據(jù)質(zhì)量的全貌需要多個角度分析,因此我們需要八個不同的模塊。雖然它們被封裝在DataQuality 類當(dāng)中,但一些模塊并不會運(yùn)行,除非我們提供特定的參數(shù)。
例如,DataQuality類不會執(zhí)行偏差與公正(Bias & Fairness)質(zhì)量檢驗(yàn),因?yàn)槲覀儾]有指出敏感性特征。但是這個庫的妙處在于,我們可以將其作為獨(dú)立的檢驗(yàn)并執(zhí)行它。
讓我們來通過運(yùn)行偏差與公正檢驗(yàn)來更好地理解它。
from ydata_quality.bias_fairness import BiasFairness #create the main class that holds all quality modules bf = BiasFairness(df=df, sensitive_features=['race', 'sex'], label='income') # run the tests bf_results = bf.evaluate()
當(dāng)我們運(yùn)行以上代碼的時候,我們將會收到一份針對選定模塊的相似的報告。
Warnings: TOTAL: 2 warning(s) Priority 2: 2 warning(s) Priority 2 - usage allowed, limited human intelligibility: * [BIAS&FAIRNESS - PROXY IDENTIFICATION] Found 1 feature pairs of correlation to sensitive attributes with values higher than defined threshold (0.5). * [BIAS&FAIRNESS - SENSITIVE ATTRIBUTE REPRESENTATIVITY] Found 2 values of 'race' sensitive attribute with low representativity in the dataset (below 1.00%).
從該報告中,我們了解到我們可能有一個泄露一種敏感性屬性信息的代理特征,并且敏感性屬性的特征嚴(yán)重不足。為了調(diào)查第一個Warning,我們可以利用篩選特定測試的get_warnings()方法來獲得更多詳細(xì)信息。
bf.get_warnings(test='Proxy Identification')
我們可以看到我們想要解決的問題的詳細(xì)描述:
[QualityWarning(category='Bias&Fairness', test='Proxy Identification', description='Found 1 feature pairs of correlation to sensitive attributes with values higher than defined threshold (0.5).', priority=<Priority.P2: 2>, data=features relationship_sex 0.650656 Name: association, dtype: float64)]
基于詳細(xì)的Warning,我們檢查了relationship和sex列,并且注意到一些關(guān)系狀態(tài)(例如,丈夫,妻子)是基于特定性別的,從而影響了相關(guān)性。我們可以將這些分類值改成性別中立(例如,已婚)。
第四步:解決識別的問題
讓我們實(shí)際一點(diǎn)兒。我們永遠(yuǎn)不會擁有100%清洗過的數(shù)據(jù)。我們所要做的是在有限的時間內(nèi)解決對數(shù)據(jù)影響最大的問題。對數(shù)據(jù)科學(xué)家來說,這是一個需要你根據(jù)當(dāng)前情景的限制做出的決策。
對這個例子來說,讓我們聚焦于消滅高優(yōu)先級(P1)問題并解決至少一個偏差和公正Warning。基于warning的簡單的數(shù)據(jù)清理函數(shù)如下:
def improve_quality(df: pd.DataFrame): """Clean the data based on the Data Quality issues found previously.""" # Bias & Fairness df = df.replace({'relationship': {'Husband': 'Married', 'Wife': 'Married'}}) # Substitute gender-based 'Husband'/'Wife' for generic 'Married' # Duplicates df = df.drop(columns=['workclass2']) # Remove the duplicated column df = df.drop_duplicates() # Remove exact feature value duplicates return df clean_df = improve_quality(df.copy())
我們可以放棄重復(fù)列work_class2并且替代 relationship 列的值為更通用且性別中立的。
如果你想進(jìn)一步地數(shù)據(jù)清理,請繼續(xù)你的工作。如果你選擇繼續(xù),我想看看數(shù)據(jù)清理是什么樣的。記住,你是數(shù)據(jù)科學(xué)家——決策掌握在你的手中。
第五步:運(yùn)行最后的質(zhì)量檢驗(yàn)
你可能會跳過這一步,但是當(dāng)我使用額外的最終檢驗(yàn)來檢查我處理過的數(shù)據(jù)時,我會感到安心。我非常推薦你也這樣做,這樣你會知道在完成數(shù)據(jù)清洗轉(zhuǎn)換之后,你的數(shù)據(jù)的狀態(tài)。
你可以先簡單地調(diào)用質(zhì)量引擎,然后調(diào)用evaluate()方法來再次檢索示例報告。下面是清理數(shù)據(jù)后DataQuality引擎和BiasFairness引擎的報告。
*DataQuality Engine Report:* Warnings: TOTAL: 3 warning(s) Priority 2: 3 warning(s) Priority 2 - usage allowed, limited human intelligibility: * [ERRONEOUS DATA - PREDEFINED ERRONEOUS DATA] Found 1360 ED values in the dataset. * [DATA RELATIONS - HIGH COLLINEARITY - NUMERICAL] Found 3 numerical variables with high Variance Inflation Factor (VIF>5.0). The variables listed in results are highly collinear with other variables in the dataset. These will make model explainability harder and potentially give way to issues like overfitting. Depending on your end goal you might want to remove the highest VIF variables. * [DATA RELATIONS - HIGH COLLINEARITY - CATEGORICAL] Found 9 categorical variables with significant collinearity (p-value < 0.05). The variables listed in results are highly collinear with other variables in the dataset and sorted descending according to propensity. These will make model explainability harder and potentially give way to issues like overfitting. Depending on your end goal you might want to remove variables following the provided order. *Bias & Fairness Report:* Warnings: TOTAL: 1 warning(s) Priority 2: 1 warning(s) Priority 2 - usage allowed, limited human intelligibility: * [BIAS&FAIRNESS - SENSITIVE ATTRIBUTE REPRESENTATIVITY] Found 2 values of 'race' sensitive attribute with low representativity in the dataset (below 1.00%).
我們可以從上面的兩個報告當(dāng)中推斷出,我們的高優(yōu)先級問題已經(jīng)被解決了,并且另外一個低優(yōu)先級問題已經(jīng)如我們所預(yù)料地解決了。
結(jié)束寄語
因?yàn)閥data-quality的存在我太輕松了(圖片由Cookie_studio制作來自Freepik)
看,雖然我們痛恨清理數(shù)據(jù),但是這并不意味著我們不會做這項(xiàng)工作。這就是為什么它是機(jī)器學(xué)習(xí)工作流(https://towardsdatascience.com/the-machine-learning-workflow-explained-557abf882079)當(dāng)中的一個不可分割的階段,而它的解決方法就是整合像ydata-quality(https://github.com/ydataai/ydata-quality)這樣有價值的工具和庫進(jìn)入到我們的工作流里。
在這篇文章中,我們學(xué)到如何使用開源包來評估數(shù)據(jù)集的數(shù)據(jù)質(zhì)量,用 DataQuality 主引擎和特定的模塊引擎(例如BiasFairness)。進(jìn)一步地,我們看到QualityWarning 如何提供高水平的嚴(yán)重性評定并向我們指出產(chǎn)生Warning的原始數(shù)據(jù)。
在這之后,我們基于數(shù)據(jù)質(zhì)量問題定義數(shù)據(jù)清理管道,同時對混亂的數(shù)據(jù)進(jìn)行轉(zhuǎn)換,并觀察它如何解決我們的目標(biāo)Warning的。
YData團(tuán)隊(duì)開發(fā)了這個庫,這個團(tuán)隊(duì)的使命就是改善人工智能行業(yè)的數(shù)據(jù)質(zhì)量。更多問題請加入友好的slack社區(qū)并然后直接向開發(fā)團(tuán)隊(duì)詢問所有問題(你也可以在那里找到我?。?/p>
我們一起肯定可以改善這個庫,你的反饋將意味著這個庫解決了你在未來的大部分緊迫問題。我迫不及待地想看到你使用這個ydata-Quality,并得到你在社區(qū)內(nèi)的反饋。
原文標(biāo)題:
A Data Scientist’s Guide to Identifyand Resolve Data Quality Issues
原文鏈接:
https://towardsdatascience.com/a-data-scientists-guide-to-identify-and-resolve-data-quality-issues-1fae1fc09c8d?gi=cbccd2061ee2
*博客內(nèi)容為網(wǎng)友個人發(fā)布,僅代表博主個人觀點(diǎn),如有侵權(quán)請聯(lián)系工作人員刪除。