識(shí)別并解決數(shù)據(jù)質(zhì)量問題的數(shù)據(jù)科學(xué)家指南

大數(shù)據(jù)文摘轉(zhuǎn)載自數(shù)據(jù)派THU
作者:Arunn Thevapalan
翻譯:陳超
校對(duì):王紫岳
本文介紹了Python中的Ydata-quality庫如何應(yīng)用于數(shù)據(jù)質(zhì)量診斷,并給出數(shù)據(jù)實(shí)例進(jìn)行詳細(xì)的一步步解釋。
在你的下一個(gè)項(xiàng)目之前早點(diǎn)這么做將會(huì)讓你免于幾周的辛苦和壓力。
如果你在處理現(xiàn)實(shí)數(shù)據(jù)的AI行業(yè)工作,那么你會(huì)理解這種痛苦。無論數(shù)據(jù)收集過程多么精簡?,我們用于建模的數(shù)據(jù)總是一片狼藉。
就像IBM描述的那樣,80/20規(guī)則在數(shù)據(jù)科學(xué)領(lǐng)域同樣適用。數(shù)據(jù)科學(xué)家80%的寶貴時(shí)間都花費(fèi)在發(fā)現(xiàn)、清洗以及組織數(shù)據(jù)上。僅僅留下了20%的時(shí)間用于真正的數(shù)據(jù)分析。
整理數(shù)據(jù)并不有趣。對(duì)于“垃圾輸入進(jìn)去,垃圾輸出出來”這句話,我知道它的重要性,但是我真的不能享受清洗空白格,修正正則表達(dá)式,并且解決數(shù)據(jù)中無法預(yù)料的問題的過程。
根據(jù)谷歌研究:“每個(gè)人都想做建模工作,而不是數(shù)據(jù)工作”——我對(duì)此感到非常愧疚。另外?,本文介紹了一種叫做數(shù)據(jù)級(jí)聯(lián)(data cascade)的現(xiàn)象,這種現(xiàn)象是指由底層數(shù)據(jù)問題引發(fā)的不利的后續(xù)影響的混合事件。實(shí)際上,該問題目前有三個(gè)方面?:
??絕大多數(shù)數(shù)據(jù)科學(xué)技術(shù)并不喜歡清理和整理數(shù)據(jù); ??只有20%的時(shí)間是在做有用的分析; ??數(shù)據(jù)質(zhì)量問題如果不盡早處理,將會(huì)產(chǎn)生級(jí)聯(lián)現(xiàn)象并影響后續(xù)工作。
只有解決了這些問題才能確保清理數(shù)據(jù)是容易,快捷,自然的。我們需要工具和技術(shù)來幫助我們這些數(shù)據(jù)科學(xué)家快速識(shí)別并解決數(shù)據(jù)質(zhì)量問題,并以此將我們寶貴的時(shí)間投入到分析和AI領(lǐng)域——那些我們真正喜歡的工作當(dāng)中。
在本文當(dāng)中,我將呈現(xiàn)一種幫助我們基于預(yù)期優(yōu)先級(jí)來提前識(shí)別數(shù)據(jù)質(zhì)量問題的開源工具。我很慶幸有這個(gè)工具存在,并且我等不及要跟你們分享它。

Ydata-quality是一個(gè)開源的Python庫,用于數(shù)據(jù)管道發(fā)展的多個(gè)階段來評(píng)估數(shù)據(jù)質(zhì)量。該庫是直觀、易用的,并且你可以直接將其整合進(jìn)入你的機(jī)器學(xué)習(xí)工作流。
對(duì)我個(gè)人而言,這個(gè)庫的好處在于它可以基于數(shù)據(jù)質(zhì)量問題(接下來展開)的優(yōu)先級(jí)排序。在我們時(shí)間有限時(shí),這是很有幫助的,并且我們也想優(yōu)先處理對(duì)我們數(shù)據(jù)質(zhì)量影響較大的問題。
?
讓我向你們展示一下如何使用一團(tuán)亂麻的數(shù)據(jù)的現(xiàn)實(shí)例子。在這個(gè)例子當(dāng)中,我們將會(huì):
??加載一個(gè)混亂的數(shù)據(jù)集; ??分析數(shù)據(jù)質(zhì)量問題; ??進(jìn)一步挖掘警告信息; ??應(yīng)用策略來減輕這些問題; ??檢查在半清洗過后的數(shù)據(jù)的最終質(zhì)量分析報(bào)告。
在安裝任何庫之前,最好使用venv或者conda來為項(xiàng)目創(chuàng)建虛擬環(huán)境,一旦這一步完成,在你的終端輸入下面這行代碼來安裝庫:
pip install?ydata-quality
第一步中,我們將會(huì)加載數(shù)據(jù)集以及必要的庫。注意,這個(gè)庫有多個(gè)模塊(偏差&公正,數(shù)據(jù)期望,數(shù)據(jù)關(guān)系,漂移分析,錯(cuò)誤數(shù)據(jù),標(biāo)簽,缺失值)用于單獨(dú)的數(shù)據(jù)質(zhì)量問題,但是我們可以從DataQuality引擎開始,該引擎把所有的個(gè)體引擎打包成了一個(gè)類。
from?ydata_quality import?DataQualityimport pandas as?pddf = pd.read_csv('../datasets/transformed/census_10k.csv')這是一個(gè)漫長的過程,但是DataQuality引擎在抽取所有細(xì)節(jié)方面確實(shí)做的很好?。只要簡單地創(chuàng)建主類并使用evaluate() 方法。
# create the main class that holds all quality modulesdq = DataQuality(df=df)
# run the testsresults = dq.evaluate()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ì)分析一下這個(gè)報(bào)告:
警告(Warning):其中包括數(shù)據(jù)質(zhì)量分析過程中檢測到的問題細(xì)節(jié)。
優(yōu)先級(jí)(Priority):對(duì)每一個(gè)檢測到的問題,基于該問題預(yù)期的影響來分配一個(gè)優(yōu)先級(jí)(越低的值表明越高的優(yōu)先性)。
模塊(Modules):每個(gè)檢測到的問題與某一個(gè)模塊(例如:數(shù)據(jù)關(guān)系,重復(fù)值,等)執(zhí)行的數(shù)據(jù)質(zhì)量檢驗(yàn)相關(guān)聯(lián)。
把所有的東西聯(lián)系在一起,我們注意到有五個(gè)警告被識(shí)別出來,其中之一就是高優(yōu)先級(jí)問題。它被“重復(fù)值”模塊被檢測出來,這意味著我們有一整個(gè)重復(fù)列需要修復(fù)。為了更深入地處理該問題,我們使用get_warnings()? 方法。
dq.get_warnings(test="DuplicateColumns")[QualityWarning(category='Duplicates', test='Duplicate Columns', description='Found 1 columns with exactly the same feature values as other columns.', priority=, data={'workclass': ['workclass2']})] 數(shù)據(jù)質(zhì)量的全貌需要多個(gè)角度分析,因此我們需要八個(gè)不同的模塊。雖然它們被封裝在DataQuality 類當(dāng)中,但一些模塊并不會(huì)運(yùn)行,除非我們提供特定的參數(shù)。
from ydata_quality.bias_fairness import BiasFairness
#create the main class that holds all quality modulesbf = BiasFairness(df=df, sensitive_features=['race', 'sex'], label='income')
#?run the testsbf_results = bf.evaluate()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%).bf.get_warnings(test='Proxy Identification')[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=2>, data=features
relationship_sex 0.650656
Name: association, dtype: float64)] 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())*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%).
原文標(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

評(píng)論
圖片
表情
