首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >错误:"AttributeError:'DataFrame‘对象没有属性'source'“或"KeyError:'source'”

错误:"AttributeError:'DataFrame‘对象没有属性'source'“或"KeyError:'source'”
EN

Stack Overflow用户
提问于 2020-03-01 20:45:33
回答 1查看 1.4K关注 0票数 0

目标:绘制功能的重要性

错误1: AttributeError:'DataFrame‘对象没有属性’DataFrame‘

错误2: KeyError:“source”

Where?:名称= [data.sourcei for i in indices] names =[data‘’source‘== i in indices]

我不是蟒蛇和熊猫方面的专家,你能帮我纠正一下这段代码吗?另外,如果一个建议的语法,以避免类似的错误在未来?

代码:

代码语言:javascript
复制
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

data = pd.read_csv('data_with_anomalies.csv')
source = pd.DataFrame(data)
target = data['Anomaly']
source = source.drop(columns = ['Anomaly_Tag'])

model = ExtraTreesClassifier()
model.fit(source, target)
print(model.feature_importances_)

importances = model.feature_importances_

# Below chunk is referred from another question on stackoverflow
# Sort feature importances in descending order
indices = np.argsort(importances)[::-1]

获取错误1和下面一行:

代码语言:javascript
复制
# Rearrange feature names so they match the sorted feature importances
names = [data.source[i] for i in indices]

或如果我将其更改为下面,则会得到错误2:

代码语言:javascript
复制
names = [data['source'] == i for i in indices]

plt.figure()
plt.title("Feature Importance")
plt.bar(range(source.shape[1]), importances[indices])
plt.xticks(range(source.shape[1]), names, rotation=90)
plt.show()
EN

回答 1

Stack Overflow用户

发布于 2020-03-01 21:36:05

尝试:

names = data.reindex(indices)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60479597

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档