python - 尝试在 Python 中获取 False Negative 时获取“索引 1 超出范围”
问题描述
我正在尝试为我的预测获取 TP、TN、FP 和 FN
这是我的代码
xtrain, xval, ytrain, yval = train_test_split(df_features, df_results, test_size=0.2, random_state=9)
clf = RandomForestClassifier(n_estimators=1000, max_depth=10, random_state=0)
clf.fit(xtrain, ytrain)
y_pred = clf.predict(xval)
CM = confusion_matrix(yval, y_pred)
TN = CM[0][0]
FN = CM[1][0]
TP = CM[1][1]
FP = CM[0][1]
我得到了 TN 值,但是当我尝试获取 FN 值时,我得到了这个错误
CM[1][0]
Traceback (most recent call last):
File "<ipython-input-18-4e0323bdbf74>", line 1, in <module>
CM[1][0]
IndexError: index 1 is out of bounds for axis 0 with size 1
我该如何解决?
编辑 :
print(CM)
[[19]]
这就是 yval 的样子
yval
6 A72
46 B83
26 B88
76 A94
28 B99
38 A72
61 A72
3 B88
20 A29
45 C52
86 A75
解决方案
将它放在 try-catch 块中以避免错误:
try:
Precision = cm[0][0]/(cm[0][0] + cm[1][0])
Recall = cm[0][0]/(cm[0][0] + cm[0][1])
except:
pass
推荐阅读
- amazon-cloudformation - 获取通过 LaunchConfiguration 创建的 EC2 的属性
- c# - EWS 基本身份验证停用的影响
- javascript - 在 Officejs 加载项中提交文件时如何处理 getFileAsync 失败状态
- sql - SQL DB2 根据计数拆分分组结果
- matplotlib - 未保存数字
- python-3.x - 从对角线创建 3-D(或更大)对角线 NumPy 数组
- bash - Bash 中的双向哈希?
- python - Azure Functions/Cosmos DB 中的 MaxItemCount
- jenkins - 减少并行运行多个作业的管道脚本
- sql - 当我尝试将日期转换为 char 并且时间戳部分被删除时