Browse Source

Update of RFE using SVM with linear kernel

Jérôme BUISINE 8 months ago
parent
commit
510d6666ab
2 changed files with 19 additions and 7 deletions
  1. 6 7
      models.py
  2. 13 0
      test_rfe.py

+ 6 - 7
models.py

@@ -67,14 +67,11 @@ def rfe_svm_model(X_train, y_train, n_components=1):
 
     Cs = [0.001, 0.01, 0.1, 1, 10, 100, 1000]
     gammas = [0.001, 0.01, 0.1, 1, 5, 10, 100]
-    param_grid = {'estimator__kernel':['rbf'], 'estimator__C': Cs, 'estimator__gamma' : gammas}
-
-    svc = svm.SVC(probability=True)
-
-    rfe_model = RFECV(svc, step=1, cv=10, verbose=0)
-
-    clf = GridSearchCV(rfe_model, param_grid, cv=10, scoring='accuracy', verbose=1)
+    param_grid = [{'estimator__C': Cs, 'estimator__gamma' : gammas}]
 
+    estimator = svm.SVC(kernel="linear")
+    selector = RFECV(estimator, step=1, cv=4, verbose=0)
+    clf = GridSearchCV(selector, param_grid, cv=5, verbose=1)
     clf.fit(X_train, y_train)
 
     print(clf.best_estimator_)
@@ -83,6 +80,8 @@ def rfe_svm_model(X_train, y_train, n_components=1):
     print('------------------------------')
     print(clf.best_estimator_.ranking_)
     print('------------------------------')
+    print(clf.best_estimator_.support_)
+    print('------------------------------')
     print(clf.best_estimator_.grid_scores_)
 
     return clf.best_estimator_.estimator_

+ 13 - 0
test_rfe.py

@@ -0,0 +1,13 @@
+from sklearn.datasets import make_friedman1
+from sklearn.feature_selection import RFECV
+from sklearn.model_selection import GridSearchCV
+from sklearn.svm import SVR
+X, y = make_friedman1(n_samples=50, n_features=10, random_state=0)
+param_grid = [{'estimator__C': [0.01, 0.1, 1.0, 10.0, 100.0, 1000.0]}]
+estimator = SVR(kernel="linear")
+selector = RFECV(estimator, step=1, cv=4)
+clf = GridSearchCV(selector, param_grid, cv=7)
+clf.fit(X, y)
+print(clf.best_estimator_.estimator_)
+print(clf.best_estimator_.grid_scores_)
+print(clf.best_estimator_.ranking_)