python 用隨機森林模型補充數值變數缺失值
阿新 • • 發佈:2021-02-06
技術標籤:work
對資料建模之前,填補缺失值是必不可少的一步,這裡把用隨機森林模型快速預測缺失值的方法總結如下,以方便日後的工作。
# data_df: DataFrame型別的資料
# obj_column:待填補缺失值的列名
# missing_other_column:資料中含義空值的其他列
########## 缺失值處理
def fill_miss_byRandomForest(data_df , obj_column, missing_other_column ):
## 先把有缺失的其他列刪除掉missing_other_column
data_df = data_df.drop(missing_other_column , axis = 1)
# 分成已知該特徵和未知該特徵兩部分
known = data_df[data_df[obj_column].notnull()]
unknown = data_df[data_df[obj_column].isnull()]
# y為結果標籤值
y_know = known[obj_column]
# X為特徵屬性值
X_know= known.drop(obj_column , axis = 1)
from sklearn.ensemble import RandomForestRegressor
rfr = RandomForestRegressor(random_state=0, n_estimators=200,max_depth=3,n_jobs=-1)
rfr.fit(X_know,y_know)
# 用得到的模型進行未知特徵值預測
# X為特徵屬性值
X_unknow= unknown.drop(obj_column , axis = 1)
predicted = rfr.predict(X_unknow).round(0)
data_df.loc[(data_df[obj_column] .isnull()), obj_column] = predicted
return data_df
if __name__ == '__main__':
import pandas as pd
import numpy as np
data = pd.read_csv('data/cs-training.csv')
data.describe()
data.columns.tolist()
### 呼叫fill_miss_byRandomForest函式,補充MonthlyIncome的缺失值
data1_MonthlyIncome=fill_miss_byRandomForest(data , 'MonthlyIncome' , 'NumberOfDependents')#用隨機森林填補MonthlyIncome 的缺失值