类型错误:对象的 len() 大小不正确

发布于 2025-01-16 13:43:36 字数 1747 浏览 4 评论 0原文

我正在尝试 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

I am trying random forest classifier from sklearn, when i want to print the classifier report, it is give me an error.

This was the code :

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))

And the error was like this :

    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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雨后咖啡店 2025-01-23 13:43:37

您向 classification_report 提供测试实例功能 (test_vectors),而不是真正的测试实例标签

正如根据文档,第一个参数应该是:

y_true:一维数组,或标签指标数组/稀疏矩阵。

地面实况(正确)目标值。

You're providing the test instances features (test_vectors) instead of the true test instances labels to classification_report.

As per the documentation, the first parameter should be:

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

Ground truth (correct) target values.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文