python特征选择后显示选取的特征名_scikit-learn – 管道中的python特征选择:如何确定特征名称?...

我使用管道和grid_search来选择最佳参数,然后使用这些参数来拟合最佳管道(‘best_pipe’).但是,由于feature_selection(SelectKBest)在管道中,所以没有适用于SelectKBest.

我需要知道’k’所选功能的功能名称.有任何想法如何检索它们?先感谢您

from sklearn import (cross_validation, feature_selection, pipeline,

preprocessing, linear_model, grid_search)

folds = 5

split = cross_validation.StratifiedKFold(target, n_folds=folds, shuffle = False, random_state = 0)

scores = []

for k, (train, test) in enumerate(split):

X_train, X_test, y_train, y_test = X.ix[train], X.ix[test], y.ix[train], y.ix[test]

top_feat = feature_selection.SelectKBest()

pipe = pipeline.Pipeline([('scaler', preprocessing.StandardScaler()),

('feat', top_feat),

('clf', linear_model.LogisticRegression())])

K = [40, 60, 80, 100]

C = [1.0, 0.1, 0.01, 0.001, 0.0001, 0.00001]

penalty = ['l1', 'l2']

param_grid = [{'feat__k': K,

'clf__C': C,

'clf__penalty': penalty}]

scoring = 'precision'

gs = grid_search.GridSearchCV(estimator=pipe, param_grid = param_grid, scoring = scoring)

gs.fit(X_train, y_train)

best_score = gs.best_score_

scores.append(best_score)

print "Fold: {} {} {:.4f}".format(k+1, scoring, best_score)

print gs.best_params_

best_pipe = pipeline.Pipeline([('scale', preprocessing.StandardScaler()),

('feat', feature_selection.SelectKBest(k=80)),

('clf', linear_model.LogisticRegression(C=.0001, penalty='l2'))])

best_pipe.fit(X_train, y_train)

best_pipe.predict(X_test)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值