0

Here's a non Negative MLP regressor I created

def relu(x):
    return np.maximum(x, 0)

class NonNegativeRegressor(MLPRegressor):
    def __init__(self, hidden_layer_sizes=(100,), activation='relu', solver='adam',   #hidden_layer_sizes=(1,100) this means(1 layer of 1 neuron and 2nd layer of 100 neurons)
                 alpha=0.0001, batch_size='auto', learning_rate='constant', learning_rate_init=0.001,
                 power_t=0.5, max_iter=200, shuffle=True, random_state=None, tol=1e-4, verbose=False,
                 warm_start=False, momentum=0.9, nesterovs_momentum=True, early_stopping=False,
                 validation_fraction=0.1, beta_1=0.9, beta_2=0.999, epsilon=1e-8, n_iter_no_change=10,
                 max_fun=15000):
        super().__init__(hidden_layer_sizes=hidden_layer_sizes, activation=activation, solver=solver,
                         alpha=alpha, batch_size=batch_size, learning_rate=learning_rate,
                         learning_rate_init=learning_rate_init, power_t=power_t, max_iter=max_iter,
                         shuffle=shuffle, random_state=random_state, tol=tol, verbose=verbose,
                         warm_start=warm_start, momentum=momentum, nesterovs_momentum=nesterovs_momentum,
                         early_stopping=early_stopping, validation_fraction=validation_fraction,
                         beta_1=beta_1, beta_2=beta_2, epsilon=epsilon, n_iter_no_change=n_iter_no_change,
                         max_fun=max_fun)
        self.out_activation_ = 'relu'
        self.feature_names_out_ = None
        self.coef_ = None
        
    def fit(self, X, y):
        y_non_negative = relu(y)
        super().fit(X, y_non_negative)
        self.feature_names_out_ = ["output_feature_{}".format(i) for i in range(self.n_outputs_)]
        self.coef_ = self.coefs_
        
    def predict(self, X):
        y_pred = super().predict(X)
        return relu(y_pred)
    
    def get_feature_importances(self, X):
        # Get the weights of the input layer
        weights = self.coefs_[0]

        # Compute the absolute values of the weights
        abs_weights = np.abs(weights)

        # Compute the importance scores for each feature
        feature_importances = np.sum(abs_weights, axis=0)

        # Normalize the importance scores so that they sum to 1
        feature_importances /= np.sum(feature_importances)

        return feature_importances
X_train_scale = preprocessing. Scale(X_train)

Here's the code for the RFE

from sklearn.feature_selection import RFE


train_nn = NonNegativeRegressor(random_state=0, max_iter=1500)
train_nn.fit(X_train_scale, Y_train)
# Create an instance of RFE
rfe = RFE(NonNegativeRegressor(), n_features_to_select = 10) 

X_train_scale_np =np.array(X_train_scale)
Y_train_np = np.array(Y_train)

# Fit RFE to your scaled training data and target values
rfe.fit(X_train_scale,Y_train)

# Get the list of selected features
# Get the indices of selected features
selected_feature = np.array(rfe.support_)

# Print the selected feature indices
print(selected_feature)

Here whenever I select n_features_to_select less than the columns of X_train_scale i.e. X_train_scale,shape[1] I get the error

AttributeError: 'list' object has no attribute 'ndim'.

I tried converting the Y_train to array but nah it doesn't work

I tried first using MLP regressor too, just to check if I haven't defined NonNegativeRegressor correctly but I get the error

ValueError: when `importance_getter=='auto'`, the underlying estimator MLPRegressor should have `coef_` or `feature_importances_` attribute. Either pass a fitted estimator to feature selector or call fit before calling transform.

I tried fitting before calling it, setting the importance_getter='coefs_' but nope.

I wanted to get the top 10 features to select

DataJanitor
  • 1,276
  • 1
  • 8
  • 19

0 Answers0