本文主要是介绍【机器学习 sklearn】特征筛选feature_selection,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
特征筛选更加侧重于寻找那些对模型的性能提升较大的少量特征。
继续沿用Titannic数据集,这次试图通过特征刷选来寻找最佳的特征组合,并且达到提高预测准确性的目标。
#coding:utf-8
from __future__ import division
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import time
start_time = time.time()
import pandas as pd
titanic = pd.read_csv('titanic.txt')# 分离数据特征与预测目标。
y = titanic['survived']
X = titanic.drop(['row.names', 'name', 'survived'], axis = 1)# 对对缺失数据进行填充。
X['age'].fillna(X['age'].mean(), inplace=True)
X.fillna('UNKNOWN', inplace=True)# 分割数据,依然采样25%用于测试。
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=33)# 类别型特征向量化。
from sklearn.feature_extraction import DictVectorizer
vec = DictVectorizer()
X_train = vec.fit_transform(X_train.to_dict(orient='record'))
X_test = vec.transform(X_test.to_dict(orient='record'))# 输出处理后特征向量的维度。
print len(vec.feature_names_)# 使用决策树模型依靠所有特征进行预测,并作性能评估。
from sklearn.tree import DecisionTreeClassifier
dt = DecisionTreeClassifier(criterion='entropy')
dt.fit(X_train, y_train)
print dt.score(X_test, y_test)# 从sklearn导入特征筛选器。
from sklearn import feature_selection
# 筛选前20%的特征,使用相同配置的决策树模型进行预测,并且评估性能。
fs = feature_selection.SelectPercentile(feature_selection.chi2, percentile=20)
X_train_fs = fs.fit_transform(X_train, y_train)
dt.fit(X_train_fs, y_train)
X_test_fs = fs.transform(X_test)
print dt.score(X_test_fs, y_test)# 通过交叉验证(下一节将详细介绍)的方法,按照固定间隔的百分比筛选特征,并作图展示性能随特征筛选比例的变化。
from sklearn.model_selection import cross_val_score
import numpy as nppercentiles = range(1, 100, 2)
results = []for i in percentiles:fs = feature_selection.SelectPercentile(feature_selection.chi2, percentile = i)X_train_fs = fs.fit_transform(X_train, y_train)scores = cross_val_score(dt, X_train_fs, y_train, cv=5)results = np.append(results, scores.mean())
print results# 找到提现最佳性能的特征筛选的百分比。
opt = np.where(results == results.max())[0]print 'Optimal number of features %d' % percentiles[opt]
import pylab as pl
pl.plot(percentiles, results)
pl.xlabel('percentiles of features')
pl.ylabel('accuracy')
pl.show()# 使用最佳筛选后的特征,利用相同配置的模型在测试集上进行性能评估。
from sklearn import feature_selection
fs = feature_selection.SelectPercentile(feature_selection.chi2, percentile=7)X_train_fs = fs.fit_transform(X_train, y_train)dt.fit(X_train_fs, y_train)
X_test_fs = fs.transform(X_test)
print dt.score(X_test_fs, y_test)
474
0.80547112462
0.820668693009
[ 0.85063904 0.85673057 0.87501546 0.88622964 0.86794475 0.874036280.87302618 0.86793445 0.86895485 0.87403628 0.86794475 0.870985360.86998557 0.86487322 0.86284271 0.86996496 0.86586271 0.863852810.86486291 0.85982272 0.86079159 0.8618223 0.86489384 0.867924140.86387343 0.86590394 0.87403628 0.86790353 0.86587302 0.865873020.86792414 0.86690373 0.86794475 0.86588332 0.87197485 0.868923930.86588332 0.87606679 0.87302618 0.86689342 0.87502577 0.868944550.86487322 0.86184292 0.86590394 0.86592455 0.86285302 0.858802310.85977118 0.86488353]
Optimal number of features 7
0.857142857143
这篇关于【机器学习 sklearn】特征筛选feature_selection的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!