task2 任务要求
特征衍生
特征挑选:分别用IV值和随机森林等进行特征选择
……以及你能想到特征工程处理
使用IV值特征选择
def calcWOE(dataset, col, target):
? ? # 对特征进行统计分组
? ? subdata = df(dataset.groupby(col)[col].count())
? ? # 每个分组中响应客户的数量
? ? suby = df(dataset.groupby(col)[target].sum())
? ? # subdata 与 suby 的拼接
? ? data = df(pd.merge(subdata, suby, how='left', left_index=True, right_index=True))
? ? # 相关统计,总共的样本数量total,响应客户总数b_total,未响应客户数量g_total
? ? b_total = data[target].sum()
? ? total = data[col].sum()
? ? g_total = total - b_total
? ? # WOE公式
? ? data["bad"] = data.apply(lambda x:round(x[target]/b_total, 100), axis=1)
? ? data["good"] = data.apply(lambda x:round((x[col] - x[target])/g_total, 100), axis=1)
? ? data["WOE"] = data.apply(lambda x:log(x.bad / x.good), axis=1)
? ? return data.loc[:, ["bad", "good", "WOE"]]
def calcIV(dataset):
? ? dataset["IV"] = dataset.apply(lambda x:(x["bad"] - x["good"]) * x["WOE"], axis=1)
? ? IV = sum(dataset["IV"])
? ? return IV
y = data.status
x= data.drop('status', axis=1)
col_list = [col for col in data.drop(labels=['Unnamed: 0','status'], axis=1)]
data_IV = df()
fea_iv = []
for col in col_list:
? ? col_WOE = calcWOE(data, col, "status")
? ? # 删除nan、inf、-inf
#? ? col_WOE = col_WOE[~col_WOE.isin([np.nan, np.inf, -np.inf]).any(1)]
? ? col_IV = calcIV(col_WOE)
? ? if col_IV > 0.1:
? ? ? ? data_IV[col] = [col_IV]
? ? ? ? fea_iv.append(col)
data_IV.to_csv('feature.csv', index=0,encoding='gbk')
print(fea_iv)
2. 使用随机森林特征选择
from sklearn.ensemble import RandomForestClassifier
rfc = RandomForestClassifier(random_state=2018)
rfc.fit(x, y)
importance = pd.Series(rfc.feature_importances_, index=x.columns).sort_values(ascending=False)
rfc_result = importance[: 20].index.tolist()
print(rfc_result)