Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
76 views
in Technique[技术] by (71.8m points)

python - mlxtend.feature_selection forward selection not working with SVM linear kernel?

So I'm performing a feature selection using SVM with the mlxtend packege. X is a dataframe with the features, y is the target variable. This is part of my code.

from sklearn.svm import SVC
from mlxtend.feature_selection import SequentialFeatureSelector as SFS

def SFFS(X, y, C_GS, gamma_GS, kernel_GS):
    sfs = SFS(SVC(kernel = kernel_GS, C = C_GS, gamma = gamma_GS),
         k_features = (1, num_of_features),
          forward= True,
          floating = False,
          verbose= 2,
          scoring= 'roc_auc',
          #scoring= 'accuracy',
          cv = 10,
          n_jobs= -1
         ).fit(X, y)

    return sfs

def SFFS_lin(X, y, C_GS, kernel_GS):
    sfs = SFS(SVC(kernel = kernel_GS, C = C_GS),
         k_features = (1, num_of_features),
          forward= True,
          floating = False,
          verbose= 2,
          scoring= 'roc_auc',
          cv = 10,
          n_jobs= -1
         ).fit(X, y)
    return sfs

def featureNames(sfs):
    Feature_Names = sfs.k_feature_names_
    return Feature_Names


sfs_lin = SFFS_lin(X, y, 1,'linear')
#sfs_rbf = SFFS(X, y, 1, 'auto', 'rbf')
names = featureNames(sfs_lin)
print(names)

The code starts running, but shortly it freezes here:

[Parallel(n_jobs=-1)]: Using backend LokyBackend with 8 concurrent workers. [Parallel(n_jobs=-1)]: Done 28 out of 28 | elapsed: 2.5s remaining: 0.0s [Parallel(n_jobs=-1)]: Done 28 out of 28 | elapsed: 2.5s finished

[2021-01-24 00:01:57] Features: 1/28 -- score: 0.6146428161908037[Parallel(n_jobs=-1)]: Using backend LokyBackend with 8 concurrent workers.

When using rbf kernel, the code runs beautifully. If I change the function to perform a backward elimination by setting the forward parameter to False, it runs beautifully

forward=False,

it runs beautifully. The freezing problem seems to appear when doing forward selection with linear kernel. Is this a stupid bug or I'm missing something trivial?

System info:

Python 3.8.5
scikit-learn 0.24.1
mlxtend 0.18.0
question from:https://stackoverflow.com/questions/65865518/mlxtend-feature-selection-forward-selection-not-working-with-svm-linear-kernel

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It seems that this is just a stupid bug.

Swiched the cross-validation

cv = 10

parameter to 9 and it runs ..


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...