类型错误:对象的 len() 大小不正确
我正在尝试 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您向
classification_report
提供测试实例功能 (test_vectors
),而不是真正的测试实例标签。正如根据文档,第一个参数应该是:
You're providing the test instances features (
test_vectors
) instead of the true test instances labels toclassification_report
.As per the documentation, the first parameter should be: