gpt4 book ai didi

python - 属性错误: 'dict' object has no attribute 'sort_values'

转载 作者:行者123 更新时间:2023-12-01 06:39:46 30 4
gpt4 key购买 nike

代码如下

我使用的是Python 3.5版本

# train a random forest classifier
rf = RandomForestClassifier(n_estimators = 100, random_state = 42)
rf.fit(X_train, y_train)

# show feature importance
feature_importances_df = pd.DataFrame({"feature": features, "importance": rf.feature_importances_}).sort_values("importance", ascending = False)
feature_importances_df.head(20)

尝试过

feature_importances = {"feature": features, "importance": rf.feature_importances_}.sort_values("importance", ascending = False)
feature_importances_df = pd.DataFrame.from_dict(feature_importances, orient='index')

我正在尝试学习情感分析。遇到错误

AttributeError:“dict”对象没有属性“sort_values”

最佳答案

我正在使用 Python 3.6.8 和 pandas 0.25.1

试试这个

dictionary = {"a":[2,3,1,4],"b":["a","f","g","z"]}
dataFrame = pd.DataFrame.from_dict(dictionary)
dataFrame.sort_values('a',ascending=False,inplace = True)
dataFrame.reset_index(drop=True)

这将打印

   a  b
0 4 z
1 3 f
2 2 a
3 1 g

希望对你有帮助!

关于python - 属性错误: 'dict' object has no attribute 'sort_values' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59507742/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com