1
votes

I am getting a value error and I don't know how to resolve.

from sklearn.ensemble import RandomForestClassifier
forest=RandomForestClassifier(n_estimators=50,
                         criterion='entropy',
                         min_samples_split=20,
                         min_samples_leaf=15,
                         max_features="sqrt",
                         max_leaf_nodes=12,
                         random_state=0)

forest.fit(X_train, y_train)
print("Acurracy on Training Set: {:.3f}".format(forest.score(X_train, 
y_train)))
print("Accuracy on Test Set: {:.3f}".format(forest.score(X_test, 
y_test)))

import numpy as np
import matplotlib.pyplot as plt
importances=forest.feature_importances_

This is the line causing the error. ---> 17 forest.fit(X_train, y_train)

Here is the entire error message: ValueError Traceback (most recent call last) in 15 random_state=0) 16 ---> 17 forest.fit(X_train, y_train) 18 print("Acurracy on Training Set: {:.3f}".format(forest.score(X_train, y_train))) 19 print("Accuracy on Test Set: {:.3f}".format(forest.score(X_test, y_test)))

~\Anaconda3\lib\site-packages\sklearn\ensemble\forest.py in fit(self, X, y, 
sample_weight)
    273         self.n_outputs_ = y.shape[1]
    274 
--> 275         y, expanded_class_weight = self._validate_y_class_weight(y)
    276 
    277         if getattr(y, "dtype", None) != DOUBLE or not 
y.flags.contiguous:

~\Anaconda3\lib\site-packages\sklearn\ensemble\forest.py in 
_validate_y_class_weight(self, y)
    476 
    477     def _validate_y_class_weight(self, y):
--> 478         check_classification_targets(y)
    479 
    480         y = np.copy(y)

~\Anaconda3\lib\site-packages\sklearn\utils\multiclass.py in 
check_classification_targets(y)
    167     if y_type not in ['binary', 'multiclass', 'multiclass- 
multioutput',
    168                       'multilabel-indicator', 'multilabel- 
sequences']:
--> 169         raise ValueError("Unknown label type: %r" % y_type)
    170 
    171 


ValueError: Unknown label type: 'continuous'
1
Make sure your y_train contains only categorical values. Check on this for more detailsthilakshiK

1 Answers

-1
votes

The issue seems to be coming from forest.fit(X_train, y_train) please check the parameters you are passing to the fit method, as they seem to be of a wrong type.