TypeError: len() of unsized object

TypeError: len() of unsized object

我正在尝试 sklearn 的随机森林分类器,当我想打印分类器报告时,它给我一个错误。

这是代码:

randomforestmodel = RandomForestClassifier()
randomforestmodel.fit(train_vectors, data_train['label'])
predict_rfmodel = randomforestmodel.predict(test_vectors)

print("classification with randomforest")
print(metrics.classification_report(test_vectors, predict_rfmodel))

错误是这样的:

    classification with randomforest
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-34-f976cec884e4> in <module>()
      1 print("classification with randomforest")
----> 2 print(metrics.classification_report(test_vectors, predict_rfmodel))

2 frames
/usr/local/lib/python3.7/dist-packages/sklearn/metrics/_classification.py in classification_report(y_true, y_pred, labels, target_names, sample_weight, digits, output_dict, zero_division)
   2108     """
   2109 
-> 2110     y_type, y_true, y_pred = _check_targets(y_true, y_pred)
   2111 
   2112     if labels is None:

/usr/local/lib/python3.7/dist-packages/sklearn/metrics/_classification.py in _check_targets(y_true, y_pred)
     83     """
     84     check_consistent_length(y_true, y_pred)
---> 85     type_true = type_of_target(y_true)
     86     type_pred = type_of_target(y_pred)
     87 

/usr/local/lib/python3.7/dist-packages/sklearn/utils/multiclass.py in type_of_target(y)
    308 
    309     # Invalid inputs
--> 310     if y.ndim > 2 or (y.dtype == object and len(y) and not isinstance(y.flat[0], str)):
    311         return "unknown"  # [[[1, 2]]] or [obj_1] and not ["label_1"]
    312 

TypeError: len() of unsized object

您提供的测试实例 features (test_vectors) 而不是真正的测试实例 labels classification_report.

作为per the documentation,第一个参数应该是:

y_true: 1d array-like, or label indicator array / sparse matrix.

Ground truth (correct) target values.