在进行李宏毅HW01作业时,需进行特征选择。
选用scikit-learn 中的feature_selection.
参考:
selectkbest
feature selection
实战注意点:
- chi 2 适用于非零的参数, 如果报错,换用 f_classif
model = SelectKBest(f_classif, k=4)
X_new1 = model.fit_transform(x, y)
X_new1.shape
- 计算结果中可能出现inf, 影响最后特征重要性的排序。 建议把model.score_打印出来查看,如果有inf,用零填充。
scores = model.scores_
where_are_inf = np.isinf(scores)
scores[where_are_inf] = 0
3. 将前面的特征索引提取出来
代码
import math
import numpy as np
import pandas as pd
import os
import csv
from sklearn.feature_selection import SelectKBest, chi2, f_classif
from sklearn.preprocessing import MinMaxScaler
def feature_select(feature_data,label_data,k=4,column=None):
model = SelectKBest(f_classif,k=k)
X_new = model.fit(feature_data,label_data)
scores = model.scores_
where_are_inf = np.isinf(scores)
scores[where_are_inf] = 0
indices = np.argsort(scores)[::-1]
if column:
k_best_features = [column[i] for i in indices[0:k].tolist()]
print('k best features are:',k_best_features)
return X_new,indices[:k]+1 # add column id index 原来第一行是 id ,索引要加上
if __name__ == '__main__':
train_data = pd.read_csv('./covid.train.csv').values
x = train_data[:, 1:-1] # pass first column id
y = train_data[:, -1]
x_selected, indices_selected = feature_select(x, y, 4)
print(indices_selected) # array([101, 85, 69, 53])