我遇到价值错误,不知道如何解决。
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_
这是导致错误的行。 ---> 17 forest.fit(X_train,y_train)
这是整个错误消息: ValueError追溯(最新 呼叫 持续) 在 15 random_state = 0) 16 ---> 17 forest.fit(X_train,y_train) 18份印刷品(“培训套现”: {:.3f}“。format(forest.score(X_train, y_train))) 19 print(“ 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'
答案 0 :(得分:-1)
问题似乎来自forest.fit(X_train, y_train)
,请检查传递给fit
方法的参数,因为它们的类型似乎错误。