python特徵選擇(一款非常棒的特徵選擇工具:feature-selector)

前言

       FeatureSelector是用於降低機器學習數據集的維數的工具。

       文章介紹地址
       項目地址

       本篇主要介紹一個基礎的特徵選擇工具feature-selector,feature-selector是由Feature Labs的一名數據科學家williamkoehrsen寫的特徵選擇庫。feature-selector主要對以下類型的特徵進行選擇:

       1.具有高missing-values百分比的特徵
       2.具有高相關性的特徵
       3.對模型預測結果無貢獻的特徵(即zero importance)
       4.對模型預測結果只有很小貢獻的特徵(即low importance)
       5.具有單個值的特徵(即數據集中該特徵取值的集合只有一個元素)

       從上面可以看出feature-selector確實是非常基礎的特徵選擇工具,正因爲非常的基礎,所以才非常的常用(這也是爲什麼williamkoehrsen要寫這個特徵選擇庫的原因),在拿到一個數據集的時候,往往都需要將上述類型的特徵從數據集中剔除掉。針對上面五種類型的特徵,feature-selector分別提供以下五個函數來對此處理:

       1.identify_missing()
       2.identify_collinear(
)
       3.identify_zero_importance()
       4.identify_low_importance(
)
       5.identify_single_unique(*)

1.數據集選擇

       在這裏使用自己的訓練數據集。文章末尾附上鍊接。數據集採樣代碼如下:

import pandas as pd
import numpy as np
data = pd.read_csv('base_train_sum.csv')
data

在這裏插入圖片描述

2.feature-selector用法

       導入數據並創建feaure-selector實例
注意:
       作者並沒有把feature-selector發佈到pypi上,所以不能使用pip和conda進行安裝,只能手動從github下載下來,然後把feature_selector.py文件放到當前工作目錄,然後再進行import操作。

# feature-selector用法

# 導入數據並創建feaure-selector實例
import pandas as pd
from feature_selector import FeatureSelector
# 數據集中TARGET字段爲對應樣本的label
train_labels = data.flag
# 獲取all features
train_features = data.drop(columns='flag')
# 創建 feature-selector 實例,並傳入features 和labels
# fs = FeatureSelector(data = train_features,lables = train_labels)
fs = FeatureSelector(data = train_features, labels = train_labels)

3.具有高missing-values百分比的特徵

       該方法用於選擇出missing value 百分比大於60%的特徵用於選擇missing value 百分比大於指定值(通過missing_threshold指定百分比)的feature。該方法能應用於監督學習和非監督學習的特徵選擇。

fs.identify_missing(missing_threshold=0.6)
# 查看選擇出的特徵
# fs.ops['missing']
missing_features = fs.ops['missing']
print(missing_features[:10])
# 繪製所有特徵missing value百分比的直方圖
fs.plot_missing()

在這裏插入圖片描述
       說明咋沒有屬性缺失值達到 60%以上
       該方法內部使用pandas 統計數據集中所有feature的missing value 的百分比,然後選擇出百分比大於閾值的特徵,詳見feature-selector.py的114-136行。
https://github.com/WillKoehrsen/feature-selector/blob/master/feature_selector/feature_selector.py#L114-L136

For detailed information on the missing fractions, we can access the missing_stats attribute, a dataframe of the missing fractions for all features.

fs.missing_stats.head(10)

在這裏插入圖片描述

4.具有高相關性的特徵

       該方法用於選擇相關性大於指定值(通過correlation_threshold指定值)的feature。該方法同樣適用於監督學習和非監督學習。

# 不對feature進行one-hot encoding(默認爲False), 然後選擇出相關性大於0.0000005%的feature, 
fs.identify_collinear(correlation_threshold=0.0000005, one_hot=False)

在這裏插入圖片描述
       查看選擇的feature

correlated_features = fs.ops['collinear']
correlated_features[:5]

在這裏插入圖片描述
中文顯示設置

from pylab import *
mpl.rcParams['font.sans-serif'] = ['SimHei']

繪製選擇的特徵的相關性heatmap

fs.plot_collinear()

在這裏插入圖片描述
To plot all of the correlations in the data, we can pass in plot_all = True to the plot_collinear function.

fs.plot_collinear(plot_all=True)

在這裏插入圖片描述
To view the details of the corelations above the threshold, we access the record_collinear attribute which is a dataframe. The drop_feature will be removed and for each feature that will be removed, there may be several correlations it has with the corr_feature that are above the correlation_threshold.

fs.identify_collinear(correlation_threshold=0.0000005)
fs.plot_collinear()

在這裏插入圖片描述

5.對模型預測結果無貢獻的特徵

       該方法用於選擇對模型預測結果毫無貢獻的feature(即zero importance,從數據集中去除或者保留該feature對模型的結果不會有任何影響)。

       該方法以及之後的identify_low_importance都只適用於監督學習(即需要label,這也是爲什麼實例化feature-selector時需要傳入labels參數的原因)。feature-selector通過用數據集訓練一個梯度提升機(Gradient Boosting machine, GBM),然後由GBM得到每一個feature的重要性分數,對所有特徵的重要性分數進行歸一化處理,選擇出重要性分數等於零的feature。

       爲了使計算得到的feature重要性分數具有很小的方差,identify_zero_importance內部會對GBM訓練多次,取多次訓練的平均值,得到最終的feature重要性分數。同時爲了防止過擬合,identify_zero_importance內部從數據集中抽取一部分作爲驗證集,在訓練GBM的時候,計算GBM在驗證集上的某一metric,當metric滿足一定條件時,停止GBM的訓練。

# 選擇zero importance的feature,
#
# 參數說明:
#          task: 'classification' / 'regression', 如果數據的模型是分類模型選擇'classificaiton',
#                否則選擇'regression'
#          eval_metric: 判斷提前停止的metric. for example, 'auc' for classification, and 'l2' for regression problem
#          n_iteration: 訓練的次數
#          early_stopping: True/False, 是否需要提前停止
fs.identify_zero_importance(task = 'classification', eval_metric = 'auc', 
                            n_iterations = 10, early_stopping = True)

在這裏插入圖片描述
Running the gradient boosting model requires one hot encoding the features. These features are saved in the one_hot_features attribute of the FeatureSelector. The original features are saved in the base_features..

one_hot_features = fs.one_hot_features
base_features = fs.base_features
print('There are %d original features' % len(base_features))
print('There are %d one-hot features' % len(one_hot_features))

在這裏插入圖片描述

fs.data_all.head(10)

保存本地

fs.data_all.to_csv('xx.csv',index=False,index_label=False,encoding='utf-8-sig')

在這裏插入圖片描述
查看選擇出的zero importance feature

zero_importance_features = fs.ops['zero_importance']
zero_importance_features

在這裏插入圖片描述

# 繪製feature importance 關係圖
# 參數說明:
#          plot_n: 指定繪製前plot_n個最重要的feature的歸一化importance條形圖,如圖4所示
#          threshold: 指定importance分數累積和的閾值,用於指定圖4中的藍色虛線.
#              藍色虛線指定了importance累積和達到threshold時,所需要的feature個數。
#              注意:在計算importance累積和之前,對feature列表安裝feature importance的大小
#                   進行了降序排序
fs.plot_feature_importances(threshold=0.99, plot_n=12)

在這裏插入圖片描述
在這裏插入圖片描述
21 features required for 0.99 of cumulative importance

All of the feature importances are accessible in the feature_importances attribute of the FeatureSelector

fs.feature_importances.head(21)

在這裏插入圖片描述
We could use these results to select only the ‘n’ most important features. For example, if we want the top 100 most importance, we could do the following.

one_hundred_features = list(fs.feature_importances.loc[:99, 'feature'])
one_hundred_features

在這裏插入圖片描述

len(one_hundred_features)

在這裏插入圖片描述

6.對模型預測結果只有很小貢獻的特徵

       該方法是使用identify_zero_importance計算的結果,選擇出對importance累積和達到指定閾值沒有貢獻的feature(這樣說有點拗口),即圖5中藍色虛線之後的feature。該方法只適用於監督學習。identify_low_importance有點類似於PCA中留下主要分量去除不重要的分量。

# 選擇出對importance累積和達到99%沒有貢獻的feature
fs.identify_low_importance(cumulative_importance=0.99)

# 查看選擇出的feature
fs.ops['low_importance']

在這裏插入圖片描述

7.具有單個值的特徵

       該方法用於選擇只有單個取值的feature,單個值的feature的方差爲0,對於模型的訓練不會有任何作用(從信息熵的角度看,該feature的熵爲0)。該方法可應用於監督學習和非監督學習。

# 選擇出只有單個值的feature
fs.identify_single_unique()

# 查看選擇出的feature
fs.ops['single_unique']

#繪製所有feature unique value的直方圖
fs.plot_unique()

在這裏插入圖片描述

8.從數據集去除選擇的特徵

       上面介紹了feature-selector提供的特徵選擇方法,這些方法從數據集中識別了feature,但並沒有從數據集中將這些feature去除。feature-selector中提供了remove方法將選擇的特徵從數據集中去除,並返回去除特徵之後的數據集。

# 去除所有類型的特徵
#    參數說明:
#       methods: 
#               desc:  需要去除哪些類型的特徵
#               type:  string / list-like object
#             values:  'all' 或者是 ['missing', 'single_unique', 'collinear', 'zero_importance', 'low_importance']
#                      中多個方法名的組合
#      keep_one_hot: 
#              desc: 是否需要保留one-hot encoding的特徵
#              type: boolean
#              values: True/False
#              default: True
# train_removed = fs.remove(methods = 'all', keep_one_hot=False)
train_no_missing = fs.remove(methods = ['missing'])

在這裏插入圖片描述

train_no_missing_zero = fs.remove(methods = ['missing', 'zero_importance'])

在這裏插入圖片描述
To remove the features from all of the methods, pass in method=‘all’. Before we do this, we can check how many features will be removed using check_removal. This returns a list of all the features that have been idenfitied for removal.

all_to_remove = fs.check_removal()
all_to_remove[:25]

在這裏插入圖片描述
Now we can remove all of the features idenfitied.

train_removed = fs.remove(methods = 'all')

在這裏插入圖片描述
去除不需要的特徵生成新的文件
在這裏插入圖片描述
在這裏插入圖片描述
Handling One-Hot Features

If we look at the dataframe that is returned, we may notice several new columns that were not in the original data. These are created when the data is one-hot encoded for machine learning. To remove all the one-hot features, we can pass in keep_one_hot = False to the remove method.

train_removed_all = fs.remove(methods = 'all', keep_one_hot=False)

在這裏插入圖片描述

print('Original Number of Features', train_features.shape[1])
print('Final Number of Features: ', train_removed_all.shape[1])

在這裏插入圖片描述
一次性選擇所有類型的特徵

feature-selector除了能每次運行一個identify_*函數來選擇一種類型特徵外,還可以使用identify_all函數一次性選擇5種類型的特徵選。

fs = FeatureSelector(data = train_features, labels = train_labels)

fs.identify_all(selection_params = {'missing_threshold': 0.6, 'correlation_threshold': 0.98, 
                                    'task': 'classification', 'eval_metric': 'auc', 
                                     'cumulative_importance': 0.99})

在這裏插入圖片描述

train_removed_all_once = fs.remove(methods = 'all', keep_one_hot = True)

在這裏插入圖片描述

fs.feature_importances.head(100)

在這裏插入圖片描述

在這裏插入圖片描述

在這裏插入圖片描述

個人微信公衆號,專注於學習資源、筆記分享,歡迎關注。我們一起成長,一起學習。一直純真着,善良着,溫情地熱愛生活,,如果覺得有點用的話,請不要吝嗇你手中點讚的權力,謝謝我親愛的讀者朋友
五角錢的程序員,專注於學習資源、筆記分享。

Wisdom is often times nearer when we stoop than when we soar.
– William Wordsworth
「比起高飛的時候,智慧往往在我們俯身時更接近我們。」– 威廉‧華茲華斯

參考:https://cloud.tencent.com/developer/article/1374205

數據鏈接:https://pan.baidu.com/s/1YOYmaBlxaYD4Ouztwm_K_w
提取碼:g2gt

9.結論

本筆記演示瞭如何使用FeatureSelector類從數據集中刪除功能。此實現中有幾個重要注意事項:

•在機器學習模型的多次運行中,屬性重要性將發生變化

•決定是否保留從一個onehot創建的額外功能

•爲各種參數嘗試幾個不同的值,以確定哪些參數最適合機器學習任務

•對於相同的參數,缺失、單一唯一和共線的輸出將保持不變

•特徵選擇是機器學習工作流的關鍵步驟,可能需要多次迭代來優化

XiangLin
2020年3月13日於重慶城口
好好學習,天天向上,終有所獲

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章