Cyrine selma
Cyrine selma

Reputation: 33

RandomForest IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices

I am working with sklearn on RandomForestClassifier:

class RandomForest(RandomForestClassifier):

    def fit(self, x, y):
        self.unique_train_y,  y_classes = transform_y_vectors_in_classes(y)
        return RandomForestClassifier.fit(self, x, y_classes)

    def predict(self, x):
        y_classes = RandomForestClassifier.predict(self, x)
        predictions = transform_classes_in_y_vectors(y_classes, self.unique_train_y)
        return predictions

    def transform_classes_in_y_vectors(y_classes, unique_train_y):
        cyr = [unique_train_y[predicted_index] for predicted_index in y_classes]
        predictions = np.array(float(cyr))
        return predictions

I got this Error message:

IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices

Upvotes: 3

Views: 3296

Answers (1)

omri_saadon
omri_saadon

Reputation: 10669

It seems that y_classes holds values that are not valid indices.

When you try to get access into unique_train_y with predicted_index than you get the exception as predicted_index is not what you think it is.

Try to execute the following code:

cyr = [unique_train_y[predicted_index] for predicted_index in range(len(y_classes))] 
# assuming unique_train_y is a list and predicted_index should be integer.

Upvotes: 1

Related Questions