【數(shù)據(jù)競(jìng)賽】百賽百試,十試九靈的特征篩選策略-Pearson Correlation
Kaggle競(jìng)賽知識(shí)點(diǎn)--Pearson Correlation!

Pearson相關(guān)性在采用線性模型建模時(shí)是必不可少的一種建模策略,它不僅可以用來判斷特征與目標(biāo)變量之間的關(guān)系,與此同時(shí)還可以對(duì)冗余特征進(jìn)行篩選,其對(duì)于特征之間相關(guān)性的篩選在我嘗試的所有競(jìng)賽中基本都帶來了很不錯(cuò)的提升。即使沒有提升,效果在刪去了很多特征之后效果并沒有下降。Pearson相關(guān)系數(shù)的計(jì)算是非常簡(jiǎn)單而且實(shí)用的。
在一些統(tǒng)計(jì)學(xué)的博客中,大家認(rèn)為:
完美:如果值接近± 1,那么這就是一個(gè)完美的相關(guān)性:當(dāng)一個(gè)變量增加時(shí),另一個(gè)變量也趨向于增加(如果是正的)或減少(如果是負(fù)的)。 高度相關(guān):如果系數(shù)值介于± 0.50和± 1,那么就說這是一個(gè)很強(qiáng)的相關(guān)性。 中度相關(guān):如果值介于± 0.30和± 0.49,則稱為中等相關(guān)性。 低度相關(guān):當(dāng)值低于± 0.29時(shí),則表示相關(guān)性很小。 無相關(guān)性:當(dāng)值為零時(shí)。
到目前為止,Pearson Correlation的方法依然是我所有競(jìng)賽中都必須會(huì)嘗試的策略之一。

Pearson Correlation系數(shù)的計(jì)算
表示兩個(gè)特征,為第個(gè)樣本對(duì)應(yīng)X特征的值,為第個(gè)樣本對(duì)應(yīng)Y特征的值;

1. 特征標(biāo)簽的篩選
計(jì)算與標(biāo)簽的相關(guān)性進(jìn)行特征的篩選
import pandas as pd
import numpy as np
# 構(gòu)建數(shù)據(jù)集
df = pd.DataFrame()
df['f1'] = [1,0,0,0,0] * 10
df['f2'] = [1,1,0,0,0] * 10
df['f3'] = [-1,-2,-3,-4,-5] * 10
df['y'] = [1,1,0,0,0] * 10
def Pearson_cor_Selector(X, y,num_feats):
cor_list = []
feature_name = X.columns.tolist()
# calculate the correlation with y for each feature
for i in X.columns.tolist():
cor = np.corrcoef(X[i], y)[0, 1]
cor_list.append(cor)
scores = pd.DataFrame({'fea':feature_name, 'score':cor_list})
# replace NaN with 0
cor_list = [0 if np.isnan(i) else i for i in cor_list]
# feature name
cor_feature = X.iloc[:,np.argsort(np.abs(cor_list))[-num_feats:]].columns.tolist()
# feature selection? 0 for not select, 1 for select
cor_support = [True if i in cor_feature else False for i in feature_name]
return scores, cor_feature
scores, cor_feature = Pearson_cor_Selector(df[['f1','f2','f3']], y = df['y'].values,num_feats = 2)
print(str(len(cor_feature)), 'selected features')
2 selected features
cor_feature
['f3', 'f2']
scores
| fea | score | |
|---|---|---|
| 0 | f1 | 0.612372 |
| 1 | f2 | 1.000000 |
| 2 | f3 | 0.866025 |
2. 標(biāo)簽標(biāo)簽的篩選
計(jì)算與標(biāo)簽之間的相關(guān)性,進(jìn)行特征冗余度篩選。
df['f2_bar'] = df['f2'].values
def Pearson_cor_Selector2(X1, X2, threshold = 0.99):
'''
X1,X2:兩個(gè)不同的特征值
threshold:閾值
'''
is_redundancy = 0
cor = np.corrcoef(X1, X2)[0, 1]
if cor >= threshold:
is_redundancy = 1
return is_redundancy
Pearson_cor_Selector2(df['f2'].values, df['f2_bar'].values)
1
Pearson_cor_Selector2(df['f3'].values, df['f2_bar'].values)
0
Pearson Correlation目前一般多用于連續(xù)特征變量之間的相關(guān)性計(jì)算,針對(duì)類別特征,尤其是無序的類別特征,需要進(jìn)行特定的編碼轉(zhuǎn)化之后效果會(huì)好些,如果是使用線性模型進(jìn)行的建模,Pearson Correlation的使用基本就成了必然,而且大多數(shù)情況下,都是有不錯(cuò)提升的。

The 5 Feature Selection Algorithms every Data Scientist should know https://www.youtube.com/watch?v=lVOzlHx_15s
往期精彩回顧
本站qq群851320808,加入微信群請(qǐng)掃碼:
評(píng)論
圖片
表情
