數(shù)據(jù)挖掘?qū)嵺`任務(wù)2

任務(wù)2:

特征工程(2天)

特征衍生
特征挑選:分別用IV值和隨機(jī)森林等進(jìn)行特征選擇
……以及你能想到特征工程處理

結(jié)果

原數(shù)據(jù)特征數(shù)80, 通過(guò)隨機(jī)森林篩選后的特征數(shù)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)

# 使用眾數(shù)填充
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))

# 切分?jǐn)?shù)據(jù)集
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)

# 隨機(jī)森林
from sklearn.ensemble import RandomForestClassifier
forset = RandomForestClassifier(n_estimators=10000, random_state=0, n_jobs=-1)# 等于-1的時(shí)候,表示cpu里的所有core進(jìn)行工作或者直接填寫cpu數(shù)
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('原數(shù)據(jù)特征數(shù){}, 通過(guò)隨機(jī)森林篩選后的特征數(shù){}, 剔除了{(lán):.2%}的特征'\
      .format(data.shape[1], x_selected.shape[1], (data.shape[1]-x_selected.shape[1])/data.shape[1]))
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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