anchors/limes/lime_base.py
def feature_selection(self, data, labels, weights, num_features, method, 
                model_regressor=None):
        """Selects features for the model. see explain_instance_with_data to
           understand the parameters."""
        if model_regressor is None:
            model_regressor = Ridge(alpha=0, fit_intercept=True, random_state=self.random_state)
        if method == 'none':
            return np.array(range(data.shape[1]))
        elif method == 'forward_selection':
            return self.forward_selection(data, labels, weights, num_features, model_regressor=model_regressor)
        elif method == 'highest_weights':
            clf = model_regressor
            clf.fit(data, labels, sample_weight=weights)
            if clf.coef_.ndim > 1:
                clf.coef_ = clf.coef_[0]
            feature_weights = sorted(zip(range(data.shape[0]),
                                         clf.coef_ * data[0]),
                                     key=lambda x: np.abs(x[1]),
                                     reverse=True)
            return np.array([x[0] for x in feature_weights[:num_features]])
        elif method == 'lasso_path':
            weighted_data = ((data - np.average(data, axis=0, weights=weights))
                             * np.sqrt(weights[:, np.newaxis]))
            weighted_labels = ((labels - np.average(labels, weights=weights))
                               * np.sqrt(weights))
            nonzero = range(weighted_data.shape[1])
            _, coefs = self.generate_lars_path(weighted_data,
                                               weighted_labels)
            for i in range(len(coefs.T) - 1, 0, -1):
                nonzero = coefs.T[i].nonzero()[0]
                if len(nonzero) <= num_features:
                    break
            used_features = nonzero
            return used_features
        elif method == 'auto':
            if num_features <= 6:
                n_method = 'forward_selection'
            else:
                n_method = 'highest_weights'
            return self.feature_selection(data, labels, weights,
                                          num_features, n_method, model_regressor=model_regressor)
Selects features for the model. see explain_instance_with_data to
           understand the parameters.
Selects features for the model. see explain_instance_with_data to
understand the parameters.
Selects features for the model . see explain_instance_with_data to understand the parameters .
def feature_selection(self, data, labels, weights, num_features, method, 
                model_regressor=None):
        if model_regressor is None:
            model_regressor = Ridge(alpha=0, fit_intercept=True, random_state=self.random_state)
        if method == 'none':
            return np.array(range(data.shape[1]))
        elif method == 'forward_selection':
            return self.forward_selection(data, labels, weights, num_features, model_regressor=model_regressor)
        elif method == 'highest_weights':
            clf = model_regressor
            clf.fit(data, labels, sample_weight=weights)
            if clf.coef_.ndim > 1:
                clf.coef_ = clf.coef_[0]
            feature_weights = sorted(zip(range(data.shape[0]),
                                         clf.coef_ * data[0]),
                                     key=lambda x: np.abs(x[1]),
                                     reverse=True)
            return np.array([x[0] for x in feature_weights[:num_features]])
        elif method == 'lasso_path':
            weighted_data = ((data - np.average(data, axis=0, weights=weights))
                             * np.sqrt(weights[:, np.newaxis]))
            weighted_labels = ((labels - np.average(labels, weights=weights))
                               * np.sqrt(weights))
            nonzero = range(weighted_data.shape[1])
            _, coefs = self.generate_lars_path(weighted_data,
                                               weighted_labels)
            for i in range(len(coefs.T) - 1, 0, -1):
                nonzero = coefs.T[i].nonzero()[0]
                if len(nonzero) <= num_features:
                    break
            used_features = nonzero
            return used_features
        elif method == 'auto':
            if num_features <= 6:
                n_method = 'forward_selection'
            else:
                n_method = 'highest_weights'
            return self.feature_selection(data, labels, weights,
                                          num_features, n_method, model_regressor=model_regressor)
def feature_selection ( self , data , labels , weights , num_features , method , model_regressor = None ) : if model_regressor is None : model_regressor = Ridge ( alpha = 0 , fit_intercept = True , random_state = self . random_state ) if method == 'none' : return np . array ( range ( data . shape [ 1 ] ) ) elif method == 'forward_selection' : return self . forward_selection ( data , labels , weights , num_features , model_regressor = model_regressor ) elif method == 'highest_weights' : clf = model_regressor clf . fit ( data , labels , sample_weight = weights ) if clf . coef_ . ndim > 1 : clf . coef_ = clf . coef_ [ 0 ] feature_weights = sorted ( zip ( range ( data . shape [ 0 ] ) , clf . coef_ * data [ 0 ] ) , key = lambda x : np . abs ( x [ 1 ] ) , reverse = True ) return np . array ( [ x [ 0 ] for x in feature_weights [ : num_features ] ] ) elif method == 'lasso_path' : weighted_data = ( ( data - np . average ( data , axis = 0 , weights = weights ) ) * np . sqrt ( weights [ : , np . newaxis ] ) ) weighted_labels = ( ( labels - np . average ( labels , weights = weights ) ) * np . sqrt ( weights ) ) nonzero = range ( weighted_data . shape [ 1 ] ) _ , coefs = self . generate_lars_path ( weighted_data , weighted_labels ) for i in range ( len ( coefs . T ) - 1 , 0 , - 1 ) : nonzero = coefs . T [ i ] . nonzero ( ) [ 0 ] if len ( nonzero ) <= num_features : break used_features = nonzero return used_features elif method == 'auto' : if num_features <= 6 : n_method = 'forward_selection' else : n_method = 'highest_weights' return self . feature_selection ( data , labels , weights , num_features , n_method , model_regressor = model_regressor )
Selects features for the model.
Selects features for the model .
"""Selects features for the model. see explain_instance_with_data to
           understand the parameters."""
self
data
labels
weights
num_features
method
model_regressor