'str' object has no attribute 'target'
时间: 2023-08-16 08:06:54 浏览: 53
对不起,我之前的回答可能引起了误解。如果`current_features`是一个字符串列表,并且你想要将每个字符串作为预测结果和目标进行比较,那么你不需要访问目标的`target`属性。
以下是修改后的代码:
```python
import matplotlib.pyplot as plt
# 提取特征名称和重要性得分
sorted_feat_importance = dict(sorted(feat_importance.items(), key=lambda x: x[1], reverse=True))
sorted_features = list(sorted_feat_importance.keys())
sorted_importance_scores = list(sorted_feat_importance.values())
# 计算精度与累计纳入特征的关系
accuracy = []
cumulative_features = []
for i in range(1, len(sorted_features) + 1):
current_features = sorted_features[:i]
# 计算准确率的逻辑
correct = 0
total = len(current_features)
for prediction in current_features:
if prediction == target:
correct += 1
current_accuracy = correct / total
accuracy.append(current_accuracy)
cumulative_features.append(i)
# 绘制折线图
plt.plot(cumulative_features, accuracy)
plt.xlabel('Cumulative Features')
plt.ylabel('Accuracy')
plt.title('Accuracy vs Cumulative Features')
plt.show()
```
这个修改后的代码假设`current_features`是一个字符串列表,每个字符串代表一个预测结果。我们直接将每个预测结果与目标进行比较,并在每次匹配时增加正确预测的数量。最后,我们计算准确率,并将其添加到`accuracy`列表中。
请确保在运行代码之前定义了`target`变量,并确保它与预测结果的格式和类型匹配。如果`current_features`和`target`不匹配,你需要相应地调整比较逻辑。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)