Mercurial > repos > bgruening > sklearn_svm_classifier
annotate feature_selectors.py @ 9:dcc487a1ed3e draft
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 49522db5f2dc8a571af49e3f38e80c22571068f4
| author | bgruening | 
|---|---|
| date | Tue, 09 Jul 2019 19:05:51 -0400 | 
| parents | f7f54b24d091 | 
| children | 
| rev | line source | 
|---|---|
| 8 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 1 """ | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 2 DyRFE | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 3 DyRFECV | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 4 MyPipeline | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 5 MyimbPipeline | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 6 check_feature_importances | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 7 """ | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 8 import numpy as np | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 9 | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 10 from imblearn import under_sampling, over_sampling, combine | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 11 from imblearn.pipeline import Pipeline as imbPipeline | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 12 from sklearn import (cluster, compose, decomposition, ensemble, | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 13 feature_extraction, feature_selection, | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 14 gaussian_process, kernel_approximation, | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 15 metrics, model_selection, naive_bayes, | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 16 neighbors, pipeline, preprocessing, | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 17 svm, linear_model, tree, discriminant_analysis) | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 18 | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 19 from sklearn.base import BaseEstimator | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 20 from sklearn.base import MetaEstimatorMixin, clone, is_classifier | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 21 from sklearn.feature_selection.rfe import _rfe_single_fit, RFE, RFECV | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 22 from sklearn.model_selection import check_cv | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 23 from sklearn.metrics.scorer import check_scoring | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 24 from sklearn.utils import check_X_y, safe_indexing, safe_sqr | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 25 from sklearn.utils._joblib import Parallel, delayed, effective_n_jobs | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 26 | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 27 | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 28 class DyRFE(RFE): | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 29 """ | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 30 Mainly used with DyRFECV | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 31 | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 32 Parameters | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 33 ---------- | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 34 estimator : object | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 35 A supervised learning estimator with a ``fit`` method that provides | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 36 information about feature importance either through a ``coef_`` | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 37 attribute or through a ``feature_importances_`` attribute. | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 38 n_features_to_select : int or None (default=None) | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 39 The number of features to select. If `None`, half of the features | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 40 are selected. | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 41 step : int, float or list, optional (default=1) | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 42 If greater than or equal to 1, then ``step`` corresponds to the | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 43 (integer) number of features to remove at each iteration. | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 44 If within (0.0, 1.0), then ``step`` corresponds to the percentage | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 45 (rounded down) of features to remove at each iteration. | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 46 If list, a series of steps of features to remove at each iteration. | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 47 Iterations stops when steps finish | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 48 verbose : int, (default=0) | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 49 Controls verbosity of output. | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 50 | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 51 """ | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 52 def __init__(self, estimator, n_features_to_select=None, step=1, | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 53 verbose=0): | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 54 super(DyRFE, self).__init__(estimator, n_features_to_select, | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 55 step, verbose) | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 56 | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 57 def _fit(self, X, y, step_score=None): | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 58 | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 59 if type(self.step) is not list: | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 60 return super(DyRFE, self)._fit(X, y, step_score) | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 61 | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 62 # dynamic step | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 63 X, y = check_X_y(X, y, "csc") | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 64 # Initialization | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 65 n_features = X.shape[1] | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 66 if self.n_features_to_select is None: | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 67 n_features_to_select = n_features // 2 | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 68 else: | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 69 n_features_to_select = self.n_features_to_select | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 70 | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 71 step = [] | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 72 for s in self.step: | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 73 if 0.0 < s < 1.0: | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 74 step.append(int(max(1, s * n_features))) | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 75 else: | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 76 step.append(int(s)) | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 77 if s <= 0: | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 78 raise ValueError("Step must be >0") | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 79 | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 80 support_ = np.ones(n_features, dtype=np.bool) | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 81 ranking_ = np.ones(n_features, dtype=np.int) | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 82 | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 83 if step_score: | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 84 self.scores_ = [] | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 85 | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 86 step_i = 0 | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 87 # Elimination | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 88 while np.sum(support_) > n_features_to_select and step_i < len(step): | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 89 | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 90 # if last step is 1, will keep loop | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 91 if step_i == len(step) - 1 and step[step_i] != 0: | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 92 step.append(step[step_i]) | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 93 | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 94 # Remaining features | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 95 features = np.arange(n_features)[support_] | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 96 | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 97 # Rank the remaining features | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 98 estimator = clone(self.estimator) | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 99 if self.verbose > 0: | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 100 print("Fitting estimator with %d features." % np.sum(support_)) | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 101 | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 102 estimator.fit(X[:, features], y) | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 103 | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 104 # Get coefs | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 105 if hasattr(estimator, 'coef_'): | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 106 coefs = estimator.coef_ | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 107 else: | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 108 coefs = getattr(estimator, 'feature_importances_', None) | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 109 if coefs is None: | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 110 raise RuntimeError('The classifier does not expose ' | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 111 '"coef_" or "feature_importances_" ' | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 112 'attributes') | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 113 | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 114 # Get ranks | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 115 if coefs.ndim > 1: | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 116 ranks = np.argsort(safe_sqr(coefs).sum(axis=0)) | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 117 else: | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 118 ranks = np.argsort(safe_sqr(coefs)) | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 119 | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 120 # for sparse case ranks is matrix | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 121 ranks = np.ravel(ranks) | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 122 | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 123 # Eliminate the worse features | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 124 threshold =\ | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 125 min(step[step_i], np.sum(support_) - n_features_to_select) | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 126 | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 127 # Compute step score on the previous selection iteration | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 128 # because 'estimator' must use features | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 129 # that have not been eliminated yet | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 130 if step_score: | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 131 self.scores_.append(step_score(estimator, features)) | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 132 support_[features[ranks][:threshold]] = False | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 133 ranking_[np.logical_not(support_)] += 1 | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 134 | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 135 step_i += 1 | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 136 | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 137 # Set final attributes | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 138 features = np.arange(n_features)[support_] | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 139 self.estimator_ = clone(self.estimator) | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 140 self.estimator_.fit(X[:, features], y) | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 141 | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 142 # Compute step score when only n_features_to_select features left | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 143 if step_score: | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 144 self.scores_.append(step_score(self.estimator_, features)) | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 145 self.n_features_ = support_.sum() | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 146 self.support_ = support_ | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 147 self.ranking_ = ranking_ | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 148 | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 149 return self | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 150 | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 151 | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 152 class DyRFECV(RFECV, MetaEstimatorMixin): | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 153 """ | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 154 Compared with RFECV, DyRFECV offers flexiable `step` to eleminate | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 155 features, in the format of list, while RFECV supports only fixed number | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 156 of `step`. | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 157 | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 158 Parameters | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 159 ---------- | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 160 estimator : object | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 161 A supervised learning estimator with a ``fit`` method that provides | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 162 information about feature importance either through a ``coef_`` | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 163 attribute or through a ``feature_importances_`` attribute. | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 164 step : int or float, optional (default=1) | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 165 If greater than or equal to 1, then ``step`` corresponds to the | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 166 (integer) number of features to remove at each iteration. | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 167 If within (0.0, 1.0), then ``step`` corresponds to the percentage | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 168 (rounded down) of features to remove at each iteration. | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 169 If list, a series of step to remove at each iteration. iteration stopes | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 170 when finishing all steps | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 171 Note that the last iteration may remove fewer than ``step`` features in | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 172 order to reach ``min_features_to_select``. | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 173 min_features_to_select : int, (default=1) | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 174 The minimum number of features to be selected. This number of features | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 175 will always be scored, even if the difference between the original | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 176 feature count and ``min_features_to_select`` isn't divisible by | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 177 ``step``. | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 178 cv : int, cross-validation generator or an iterable, optional | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 179 Determines the cross-validation splitting strategy. | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 180 Possible inputs for cv are: | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 181 - None, to use the default 3-fold cross-validation, | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 182 - integer, to specify the number of folds. | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 183 - :term:`CV splitter`, | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 184 - An iterable yielding (train, test) splits as arrays of indices. | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 185 For integer/None inputs, if ``y`` is binary or multiclass, | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 186 :class:`sklearn.model_selection.StratifiedKFold` is used. If the | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 187 estimator is a classifier or if ``y`` is neither binary nor multiclass, | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 188 :class:`sklearn.model_selection.KFold` is used. | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 189 Refer :ref:`User Guide <cross_validation>` for the various | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 190 cross-validation strategies that can be used here. | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 191 .. versionchanged:: 0.20 | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 192 ``cv`` default value of None will change from 3-fold to 5-fold | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 193 in v0.22. | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 194 scoring : string, callable or None, optional, (default=None) | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 195 A string (see model evaluation documentation) or | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 196 a scorer callable object / function with signature | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 197 ``scorer(estimator, X, y)``. | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 198 verbose : int, (default=0) | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 199 Controls verbosity of output. | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 200 n_jobs : int or None, optional (default=None) | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 201 Number of cores to run in parallel while fitting across folds. | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 202 ``None`` means 1 unless in a :obj:`joblib.parallel_backend` context. | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 203 ``-1`` means using all processors. See :term:`Glossary <n_jobs>` | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 204 for more details. | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 205 """ | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 206 def __init__(self, estimator, step=1, min_features_to_select=1, cv='warn', | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 207 scoring=None, verbose=0, n_jobs=None): | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 208 super(DyRFECV, self).__init__( | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 209 estimator, step=step, | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 210 min_features_to_select=min_features_to_select, | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 211 cv=cv, scoring=scoring, verbose=verbose, | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 212 n_jobs=n_jobs) | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 213 | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 214 def fit(self, X, y, groups=None): | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 215 """Fit the RFE model and automatically tune the number of selected | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 216 features. | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 217 Parameters | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 218 ---------- | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 219 X : {array-like, sparse matrix}, shape = [n_samples, n_features] | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 220 Training vector, where `n_samples` is the number of samples and | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 221 `n_features` is the total number of features. | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 222 y : array-like, shape = [n_samples] | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 223 Target values (integers for classification, real numbers for | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 224 regression). | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 225 groups : array-like, shape = [n_samples], optional | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 226 Group labels for the samples used while splitting the dataset into | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 227 train/test set. | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 228 """ | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 229 if type(self.step) is not list: | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 230 return super(DyRFECV, self).fit(X, y, groups) | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 231 | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 232 X, y = check_X_y(X, y, "csr") | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 233 | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 234 # Initialization | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 235 cv = check_cv(self.cv, y, is_classifier(self.estimator)) | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 236 scorer = check_scoring(self.estimator, scoring=self.scoring) | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 237 n_features = X.shape[1] | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 238 | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 239 step = [] | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 240 for s in self.step: | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 241 if 0.0 < s < 1.0: | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 242 step.append(int(max(1, s * n_features))) | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 243 else: | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 244 step.append(int(s)) | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 245 if s <= 0: | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 246 raise ValueError("Step must be >0") | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 247 | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 248 # Build an RFE object, which will evaluate and score each possible | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 249 # feature count, down to self.min_features_to_select | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 250 rfe = DyRFE(estimator=self.estimator, | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 251 n_features_to_select=self.min_features_to_select, | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 252 step=self.step, verbose=self.verbose) | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 253 | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 254 # Determine the number of subsets of features by fitting across | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 255 # the train folds and choosing the "features_to_select" parameter | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 256 # that gives the least averaged error across all folds. | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 257 | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 258 # Note that joblib raises a non-picklable error for bound methods | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 259 # even if n_jobs is set to 1 with the default multiprocessing | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 260 # backend. | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 261 # This branching is done so that to | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 262 # make sure that user code that sets n_jobs to 1 | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 263 # and provides bound methods as scorers is not broken with the | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 264 # addition of n_jobs parameter in version 0.18. | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 265 | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 266 if effective_n_jobs(self.n_jobs) == 1: | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 267 parallel, func = list, _rfe_single_fit | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 268 else: | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 269 parallel = Parallel(n_jobs=self.n_jobs) | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 270 func = delayed(_rfe_single_fit) | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 271 | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 272 scores = parallel( | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 273 func(rfe, self.estimator, X, y, train, test, scorer) | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 274 for train, test in cv.split(X, y, groups)) | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 275 | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 276 scores = np.sum(scores, axis=0) | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 277 diff = int(scores.shape[0]) - len(step) | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 278 if diff > 0: | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 279 step = np.r_[step, [step[-1]] * diff] | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 280 scores_rev = scores[::-1] | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 281 argmax_idx = len(scores) - np.argmax(scores_rev) - 1 | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 282 n_features_to_select = max( | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 283 n_features - sum(step[:argmax_idx]), | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 284 self.min_features_to_select) | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 285 | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 286 # Re-execute an elimination with best_k over the whole set | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 287 rfe = DyRFE(estimator=self.estimator, | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 288 n_features_to_select=n_features_to_select, step=self.step, | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 289 verbose=self.verbose) | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 290 | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 291 rfe.fit(X, y) | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 292 | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 293 # Set final attributes | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 294 self.support_ = rfe.support_ | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 295 self.n_features_ = rfe.n_features_ | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 296 self.ranking_ = rfe.ranking_ | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 297 self.estimator_ = clone(self.estimator) | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 298 self.estimator_.fit(self.transform(X), y) | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 299 | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 300 # Fixing a normalization error, n is equal to get_n_splits(X, y) - 1 | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 301 # here, the scores are normalized by get_n_splits(X, y) | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 302 self.grid_scores_ = scores[::-1] / cv.get_n_splits(X, y, groups) | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 303 return self | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 304 | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 305 | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 306 class MyPipeline(pipeline.Pipeline): | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 307 """ | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 308 Extend pipeline object to have feature_importances_ attribute | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 309 """ | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 310 def fit(self, X, y=None, **fit_params): | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 311 super(MyPipeline, self).fit(X, y, **fit_params) | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 312 estimator = self.steps[-1][-1] | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 313 if hasattr(estimator, 'coef_'): | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 314 coefs = estimator.coef_ | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 315 else: | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 316 coefs = getattr(estimator, 'feature_importances_', None) | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 317 if coefs is None: | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 318 raise RuntimeError('The estimator in the pipeline does not expose ' | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 319 '"coef_" or "feature_importances_" ' | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 320 'attributes') | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 321 self.feature_importances_ = coefs | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 322 return self | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 323 | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 324 | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 325 class MyimbPipeline(imbPipeline): | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 326 """ | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 327 Extend imblance pipeline object to have feature_importances_ attribute | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 328 """ | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 329 def fit(self, X, y=None, **fit_params): | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 330 super(MyimbPipeline, self).fit(X, y, **fit_params) | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 331 estimator = self.steps[-1][-1] | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 332 if hasattr(estimator, 'coef_'): | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 333 coefs = estimator.coef_ | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 334 else: | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 335 coefs = getattr(estimator, 'feature_importances_', None) | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 336 if coefs is None: | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 337 raise RuntimeError('The estimator in the pipeline does not expose ' | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 338 '"coef_" or "feature_importances_" ' | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 339 'attributes') | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 340 self.feature_importances_ = coefs | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 341 return self | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 342 | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 343 | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 344 def check_feature_importances(estimator): | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 345 """ | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 346 For pipeline object which has no feature_importances_ property, | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 347 this function returns the same comfigured pipeline object with | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 348 attached the last estimator's feature_importances_. | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 349 """ | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 350 if estimator.__class__.__module__ == 'sklearn.pipeline': | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 351 pipeline_steps = estimator.get_params()['steps'] | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 352 estimator = MyPipeline(pipeline_steps) | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 353 elif estimator.__class__.__module__ == 'imblearn.pipeline': | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 354 pipeline_steps = estimator.get_params()['steps'] | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 355 estimator = MyimbPipeline(pipeline_steps) | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 356 else: | 
| 
f7f54b24d091
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
 bgruening parents: diff
changeset | 357 return estimator | 
