代码在下面
我正在使用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' object has no attribute 'sort_values'
答案 0 :(得分:1)
我正在使用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
希望有帮助!