Kaggle Data Challenge 第一天

Kaggle_logo.png

Abstract:18年復(fù)活節(jié)前的五天,kaggle舉辦了數(shù)據(jù)預(yù)處理的五個挑戰(zhàn)。這里做每天學(xué)習(xí)到的技術(shù)要點的回顧。這篇是第一天的內(nèi)容,主要是有關(guān)數(shù)據(jù)缺失的分析和操作。


1. 導(dǎo)入原始數(shù)據(jù),檢視一小部分

這里用到的library是pandasnumpy。

# modules we'll use
import pandas as pd
import numpy as np

# read in all our data
nfl_data = pd.read_csv("../input/nflplaybyplay2009to2016/NFL Play by Play 2009-2017 (v4).csv")
sf_permits = pd.read_csv("../input/building-permit-applications-data/Building_Permits.csv")

檢視5個數(shù)據(jù)樣本

sf_permits.sample(5)

2. 統(tǒng)計缺失數(shù)據(jù)數(shù)量和所占比重

利用pandas的查缺功能

# get the number of missing data points per column
missing_values_count = nfl_data.isnull().sum()

# look at the # of missing points in the first ten columns
missing_values_count[0:10]

計算缺失數(shù)據(jù)的數(shù)量在總數(shù)據(jù)中的占比

# how many total missing values do we have?
total_cells = np.product(nfl_data.shape)
total_missing = missing_values_count.sum()

# percent of data that is missing
(total_missing/total_cells) * 100

3. 分析為什么這里的數(shù)據(jù)會缺失

數(shù)據(jù)缺失的原因有兩個:一是這個數(shù)據(jù)不存在。比如在做人口統(tǒng)計的時候有一問是家里小孩的年齡,如果家庭還沒有生育,那這里就只有空著、填NA或者不適用;二是這個數(shù)據(jù)沒有被錄入。比如招聘問卷里問對薪資的預(yù)期,有些人會選擇不填。

對于這兩種情況的數(shù)據(jù)缺失,處理的方法也應(yīng)該是不一樣的。情況二中,由于這些數(shù)據(jù)缺失的原因是沒有被錄入,我們可以根據(jù)現(xiàn)有的信息來推測缺失數(shù)據(jù)的值,以便讓和它同宗同族的其他數(shù)據(jù)不至于被連坐刪除。而情況一中,對于不存在的數(shù)據(jù),應(yīng)該讓它保持為一種缺失的獨特狀態(tài),標(biāo)記為0或者“都不是”,這樣才能保留真實信息。

4. 缺失數(shù)據(jù)的處理手段

  • 暴力刪除。雖然快捷便利,但是會丟失大量有用的數(shù)據(jù)信息。使用pandas里的.dropna()功能。

    最省事的辦法就是把所有帶有NaN的數(shù)據(jù)行都刪掉:

    # remove all the rows that contain a missing value
    nfl_data.dropna()
    

    但在這個例子中,所有的行里都或多或少有缺失項,結(jié)果就是所有數(shù)據(jù)都被刪 除了。

    退而求其次,嘗試刪掉所有帶有缺失項的列:

    # remove all columns with at least one missing value
    columns_with_na_dropped = nfl_data.dropna(axis=1)
    
    # just how much data did we lose?
    print("Columns in original dataset: %d \n" % nfl_data.shape[1])
    print("Columns with na's dropped: %d" %     columns_with_na_dropped.shape[1])
    

    Columns in original dataset: 102
    Columns with na's dropped: 41

    加入axis = 1以后,可以在列的方向上刪掉含NaN的項。結(jié)果雖然還剩一部分?jǐn)?shù)據(jù),但是和原本數(shù)據(jù)比損失慘重。

  • 自動填充。根據(jù)上下文,平均值,常見值,推測值等,推斷該填入什么數(shù)據(jù)。

    首先嘗試以特定值(0)填充所有空檔。這樣可能填的太假太硬。

    # replace all NA's with 0
    nfl_data.fillna(0
    

    接下來可以使用后續(xù)值填充前面的缺失。填不上的再用0填

    # replace all NA's the value that comes directly after it in the same column, 
    # then replace all the reamining na's with 0
    subset_nfl_data.fillna(method = 'bfill', axis=0).fillna("0")
    

于是乎,所有的NaN都被替換掉了。


總結(jié):第一天的內(nèi)容主要講如何分析和處理原始數(shù)據(jù)中的缺失項。介紹了如何查看,統(tǒng)計和替換缺失項。內(nèi)容比較初級,大佬輕拍。

感受:數(shù)據(jù)預(yù)處理在機器學(xué)習(xí)的程序中占比別我想象的要大。早些在Coursera上deep learning課的時候都是老師給喂好數(shù)據(jù),只要編寫算法結(jié)構(gòu)就行。等接觸了真實數(shù)據(jù)就會發(fā)現(xiàn)很多問題,比如數(shù)據(jù)缺失,數(shù)據(jù)格式不對等。數(shù)據(jù)預(yù)處理就是在保留更多信息的前提下將數(shù)據(jù)轉(zhuǎn)變?yōu)闄C器可以接受的形態(tài),然后才能放心的喂給算法,以保證預(yù)測的準(zhǔn)確率。


如果這篇文章對你有所幫助,還請幫忙點贊打賞評論分享~謝謝??


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

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