TypeError:“int”和“str”的实例之间不支持“<”

数据挖掘 机器学习 Python scikit-学习 错误处理
2022-03-14 03:50:24

我有以下代码

rf = RandomForestClassifier()
rf.fit(X_train, Y_train)
print("Features sorted by their score:")
print(sorted(zip(map(lambda x: round(x, 2), rf.feature_importances_), X_train), reverse=True))

我收到以下错误:

> TypeError Traceback (most recent call last)
>
> ipython-input-109-c48c3ffd74e2> in <module>()
> 
> 2 rf.fit(X_train, Y_train)
> 
> 3 print ("Features sorted by their score:")
> 
> ----> 4 print (sorted(zip(map(lambda x: round(x, 2),
> rf.feature_importances_), X_train), reverse=True))
> 
> TypeError: '<' not supported between instances of 'int' and 'str'

我不确定我做错了什么。我的数据框中只有 int 和 float 。

1个回答

根据我们的讨论,省略reverse = True并使用 list 而不是 sorted 并打印它。我想你会看到你想要的。