資料探勘實戰 資料預處理之缺失值處理

2021-10-03 10:01:37 字數 4140 閱讀 8865

kaggle的titanic 比賽不少題解有標準的處理流程,這裡參考:kaggle titanic 生存** -- 詳細流程**梳理 嘗試提取常用的缺失值處理方法

這裡還是借助google colab 來學習

讀取資料

import pandas as pd

data = pd.read_csv('data/train.csv')

data .info()

age、cabin、embarked、fare幾個特徵缺失值

rangeindex: 891 entries, 0 to 890

data columns (total 12 columns):

passengerid 891 non-null int64

survived 891 non-null int64

pclass 891 non-null int64

name 891 non-null object

*** 891 non-null object

age 714 non-null float64

sibsp 891 non-null int64

parch 891 non-null int64

ticket 891 non-null object

fare 891 non-null float64

cabin 204 non-null object

embarked 889 non-null object

dtypes: float64(2), int64(5), object(5)

memory usage: 83.6+ kb

1、如果資料集很多,但有很少的缺失值,可以刪掉帶缺失值的行;

例如刪除age這一列:

data=data.drop(columns='age')

data.info()

此時age特徵就從dataframe中去掉了

rangeindex: 891 entries, 0 to 890

data columns (total 11 columns):

passengerid 891 non-null int64

survived 891 non-null int64

pclass 891 non-null int64

name 891 non-null object

*** 891 non-null object

sibsp 891 non-null int64

parch 891 non-null int64

ticket 891 non-null object

fare 891 non-null float64

cabin 204 non-null object

embarked 889 non-null object

dtypes: float64(1), int64(5), object(5)

memory usage: 76.7+ kb

2、如果該屬性相對學習來說不是很重要,可以對缺失值賦均值或者眾數。比如在哪兒上船embarked這一屬性(共有三個上船地點),缺失倆值,可以用眾數賦值

data.embarked[data.embarked.isnull()] = data.embarked.dropna().mode().values
mode表示眾數。此時看到embarked 特徵也補齊了

rangeindex: 891 entries, 0 to 890

data columns (total 12 columns):

passengerid 891 non-null int64

survived 891 non-null int64

pclass 891 non-null int64

name 891 non-null object

*** 891 non-null object

age 714 non-null float64

sibsp 891 non-null int64

parch 891 non-null int64

ticket 891 non-null object

fare 891 non-null float64

cabin 204 non-null object

embarked 891 non-null object

dtypes: float64(2), int64(5), object(5)

memory usage: 83.7+ kb

或者用平均數賦值:

data.describe() 可以看到每一列不算入null的平均值

data['age'][data.age.isnull()] = data['age'].mean()
然後再檢視一次,平均值已經填入到age為空的地方了

3、對於標稱屬性,可以賦乙個代表缺失的值,比如『u0』。因為缺失本身也可能代表著一些隱含資訊。比如船艙號cabin這一屬性,缺失可能代表並沒有船艙。

#replace missing value with u0

data['cabin'] = data.cabin.fillna('u0') # data.cabin[data.cabin.isnull()]='u0'

4、使用回歸 隨機森林等模型來**缺失屬性的值。因為age在該資料集裡是乙個相當重要的特徵(先對age進行分析即可得知),所以保證一定的缺失值填充準確率是非常重要的,對結果也會產生較大影響。一般情況下,會使用資料完整的條目作為模型的訓練集,以此來**缺失值。對於當前的這個資料,可以使用隨機森林來**也可以使用線性回歸**。這裡使用隨機森林**模型,選取資料集中的數值屬性作為特徵(因為sklearn的模型只能處理數值屬性,所以這裡先僅選取數值特徵,但在實際的應用中需要將非數值特徵轉換為數值特徵

from sklearn.ensemble import randomforestregressor

#choose training data to predict age

age_df = data[['age','survived','fare', 'parch', 'sibsp', 'pclass']]

age_df_notnull = age_df.loc[(data['age'].notnull())]

age_df_isnull = age_df.loc[(data['age'].isnull())]

x = age_df_notnull.values[:,1:]

y = age_df_notnull.values[:,0]

# use randomforestregression to train data

rfr = randomforestregressor(n_estimators=1000, n_jobs=-1)

rfr.fit(x,y)

predictages = rfr.predict(age_df_isnull.values[:,1:])

data.loc[data['age'].isnull(), ['age']]= predictages

5、使用拉格朗日插值法

參考:某列分組平均值填充

kaggle titanic 生存** -- 詳細流程**梳理

資料預處理之缺失值處理

刪除法 刪除小部分樣本,在樣本量大時 刪除部分所佔比例小於5 時 可以使用 插補法 均值插補 分為定距型 插入均值 和非定距型 眾數或者中值 回歸插補 線性和非線性回歸 極大似然估計mle 正態分佈為例 極大似然原理的直觀想法我們用下面例子說明,在 權力的遊戲 中有個場景,老徒利死的時候,屍體放在穿...

資料預處理 缺失值處理

資料中的缺失值是個非常棘手的問題,有很多文獻都致力於解決這個問題。資料缺失的含義是 假設有 n n 個樣本,每個樣本20個特徵。但在一些樣本中出於某種原因某個特徵無效,則就構不成乙個完整的樣本。對於這樣的問題,有些情況下是不能直接將其拋棄的,對其進行挽救就是缺失值處理 1 使用可用特徵的均值來填補缺...

pandas資料預處理 缺失值

缺失值的分類 按照資料缺失機制可分為 不可忽略的缺失 non ignorable missing nim 或非隨機缺失 not missing at random,nmar,or,missing not at random,mnar 如果不完全變數中資料的缺失既依賴於完全變數又依賴於不完全變數本身,...