我使用管道和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)

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐