任务2:
特征工程(2天)
特征衍生
特征挑选:分别用IV值和随机森林等进行特征选择
……以及你能想到特征工程处理
结果
原数据特征数80, 通过随机森林筛选后的特征数50, 剔除了37.50%的特征
import pandas as pd
import numpy as np
data = pd.read_csv('data.csv',encoding='gbk')
delete = ['Unnamed: 0', 'custid', 'trade_no', 'bank_card_no','id_name','latest_query_time','source','loans_latest_time','first_transaction_time', 'student_feature']
data = data.drop(delete,axis=1)
# 使用众数填充
from sklearn.impute import SimpleImputer
for i in range(data.shape[1]):
feature = data.iloc[:,i].values.reshape(-1,1) #sklearn中特征矩阵必须是二维
imp_mode = SimpleImputer(strategy='most_frequent')
data.iloc[:,i] = imp_mode.fit_transform(feature)
# 处理分类型特征
from sklearn.preprocessing import OrdinalEncoder
data['reg_preference_for_trad'] = OrdinalEncoder().fit_transform(data['reg_preference_for_trad'].values.reshape(-1,1))
# 切分数据集
from sklearn.model_selection import train_test_split
x = data.drop('status',axis=1)
y = data.status
X_train, X_test, y_train, y_test = train_test_split(x,y,test_size = 0.3,random_state =2018,shuffle = True)
# 随机森林
from sklearn.ensemble import RandomForestClassifier
forset = RandomForestClassifier(n_estimators=10000, random_state=0, n_jobs=-1)# 等于-1的时候,表示cpu里的所有core进行工作或者直接填写cpu数
forset.fit(x, y)
# 输出重要特征
importances = forset.feature_importances_
indices = np.argsort(importances)[::-1]
feature_labels = data.columns[1:]
for f in range(X_train.shape[1]):
print("%2d) %-*s %f" % (f + 1, 30, feature_labels[indices[f]], importances[indices[f]]))
FI_RF = pd.DataFrame({"Feature Importance":forset.feature_importances_}, index=X_train.columns)
import matplotlib.pyplot as plt
FI_RF.sort_values('Feature Importance').plot(kind = 'barh', figsize =(15,25))
plt.xticks(rotation = 90)
# 筛选重要特征
threshold = 0.01
x_selected = X_train.loc[:, importances > threshold]
print('原数据特征数{}, 通过随机森林筛选后的特征数{}, 剔除了{:.2%}的特征'\
.format(data.shape[1], x_selected.shape[1], (data.shape[1]-x_selected.shape[1])/data.shape[1]))