Scikit-Learn基于.coef_值的百分位使用SVC进行特征选择

时间:2016-04-26 18:33:42

标签: python list python-2.7 numpy scikit-learn

我正在尝试编写一个Python类,以便使用.coef_属性值来选择scikit-learn 0.17.1中的功能。我想只选择.coef_值位于第10百分位及以上(第10,11,12,13,14,15,16,......,第94,第95,第96,第97,第98 ,99th,100th)。

我无法使用SelectFromModels()执行此操作,因此我尝试为此功能选择编写名为ChooseCoefPercentile()的自定义类。我正在尝试使用以下函数根据.coef_的百分位数选择要素:

from sklearn.datasets import load_iris
from sklearn.cross_validation import train_test_split
X_train, X_test, y_train, y_test = train_test_split(load_iris().data,
                                   load_iris().target, test_size=0.33, random_state=42)

def percentile_sep(coefs,p):
    from numpy import percentile as pc
    gt_p = coefs[coefs>pc(coefs,p)].argsort()
    return list(gt_p)

from sklearn.base import BaseEstimator, TransformerMixin
class ChooseCoefPercentile(BaseEstimator, TransformerMixin):
    def __init__(self, est_, perc=50):
        self.perc = perc
        self.est_ = est_
    def fit(self, *args, **kwargs):
        self.est_.fit(*args, **kwargs)
        return self
    def transform(self, X):
        perc_i = percentile_sep(self.est_.coef_,self.perc)
        i_ = self.est_.coef_.argsort()[::-1][perc_i[:]]
        X_tr = X[:,i_]
        self.coef_ = self.est_.coef_[i_]
        return X_tr

# Import modules
from sklearn import svm,ensemble,pipeline,grid_search

# Instantiate feature selection estimator and classifier
f_sel = ChooseCoefPercentile(svm.SVC(kernel='linear'),perc=10)
clf = ensemble.RandomForestClassifier(random_state=42,oob_score=False)

CustPipe = pipeline.Pipeline([("feat_s",f_sel),("Clf",clf)])
bf_est = grid_search.GridSearchCV(CustPipe,cv=2,param_grid={'Clf__n_estimators':[100,200]})
bf_est.fit(X_train, y_train)

我收到以下错误:

Traceback (most recent call last):
  File "C:\Python27\test.py", line 35, in <module>
    bf_est.fit(X_train, y_train)
  File "C:\Python27\lib\site-packages\sklearn\grid_search.py", line 804, in fit
    return self._fit(X, y, ParameterGrid(self.param_grid))
  File "C:\Python27\lib\site-packages\sklearn\grid_search.py", line 553, in _fit
    for parameters in parameter_iterable
  File "C:\Python27\lib\site-packages\sklearn\externals\joblib\parallel.py", line 800, in __call__
    while self.dispatch_one_batch(iterator):
  File "C:\Python27\lib\site-packages\sklearn\externals\joblib\parallel.py", line 658, in dispatch_one_batch
    self._dispatch(tasks)
  File "C:\Python27\lib\site-packages\sklearn\externals\joblib\parallel.py", line 566, in _dispatch
    job = ImmediateComputeBatch(batch)
  File "C:\Python27\lib\site-packages\sklearn\externals\joblib\parallel.py", line 180, in __init__
    self.results = batch()
  File "C:\Python27\lib\site-packages\sklearn\externals\joblib\parallel.py", line 72, in __call__
    return [func(*args, **kwargs) for func, args, kwargs in self.items]
  File "C:\Python27\lib\site-packages\sklearn\cross_validation.py", line 1531, in _fit_and_score
    estimator.fit(X_train, y_train, **fit_params)
  File "C:\Python27\lib\site-packages\sklearn\pipeline.py", line 164, in fit
    Xt, fit_params = self._pre_transform(X, y, **fit_params)
  File "C:\Python27\lib\site-packages\sklearn\pipeline.py", line 145, in _pre_transform
    Xt = transform.fit_transform(Xt, y, **fit_params_steps[name])
  File "C:\Python27\lib\site-packages\sklearn\base.py", line 458, in fit_transform
    return self.fit(X, y, **fit_params).transform(X)
  File "C:\Python27\test.py", line 21, in transform
    i_ = self.est_.coef_.argsort()[::-1][perc_i[:]]
IndexError: index 6 is out of bounds for axis 0 with size 3

以下行中的.coef_值的NumPy数组似乎存在问题:

i_ = self.est_.coef_.argsort()[::-1][perc_i[:]]

在这一行中,我试图仅根据其索引选择那些位于第10个百分位数以上的.coef_值。索引存储在列表perc_i中。我似乎无法使用此列表正确地索引.coef_数组。

是否发生此错误,因为需要将数组划分为多行?或者我应该使用其他方法根据百分位数提取.coef_值?

1 个答案:

答案 0 :(得分:1)

我建议使用基于行数的模运算来计算系数矩阵的相关列:

def transform(self, X):
    perc_i = percentile_sep(self.est_.coef_,self.perc)
    nclass=self.est_.coef_.shape[0]
    i_ = list(set(map(lambda x:x%nclass,perc_i)))
    X_tr = X[:,i_]
    self.coef_ = self.est_.coef_[i_]
    return X_tr