Jai*_*oln 6 python encoding altair
我正在尝试使用以下数据df_roc来使用 Altair 绘制 ROC 曲线:
Threshold TPR FPR
0 0.1 1.000000 0.941176
1 0.2 1.000000 0.705882
2 0.3 0.923077 0.588235
3 0.4 0.846154 0.470588
4 0.5 0.692308 0.352941
5 0.6 0.615385 0.235294
6 0.7 0.461538 0.117647
7 0.8 0.307692 0.058824
8 0.9 0.076923 0.000000
Run Code Online (Sandbox Code Playgroud)
这是我尝试使用的代码来制作交互式绘图:
base = alt.Chart(df_roc,
title='ROC Curve of KNN'
).properties(width=300)
roc_curve = base.mark_line(point=True).encode(
alt.X('fpr', title='False Positive Rate (FPR)', sort=None),
alt.Y('tpr', title='True Positive Rate (TPR) (a.k.a Recall)'),
)
roc_rule = base.mark_line(color='green').encode(
x='fpr',
y='fpr',
size=alt.value(2)
)
(roc_curve + roc_rule).interactive()
Run Code Online (Sandbox Code Playgroud)
这是我收到的错误:
ValueError: fpr encoding field is specified without a type; the type cannot be inferred because it does not match any column in the data.
alt.Chart(...)
Run Code Online (Sandbox Code Playgroud)
我尝试用谷歌搜索一下并尝试一些相关信息,但实际上并没有太多。有没有人遇到过这个问题的解决方案或帮助我找到解决方法?
我真的更希望能够使用 Altair 来完成此任务,而不是其他绘图包。
谁能帮我?
Altair(以及一般的 pandas)中的列名称区分大小写。您的数据似乎包含名为"TPR"和 的列"FPR',但您的图表指定了名为"tpr"和 的列"fpr"。
更改大小写,您的图表应该可以工作:
base = alt.Chart(df_roc,
title='ROC Curve of KNN'
).properties(width=300)
roc_curve = base.mark_line(point=True).encode(
alt.X('FPR', title='False Positive Rate (FPR)', sort=None),
alt.Y('TPR', title='True Positive Rate (TPR) (a.k.a Recall)'),
)
roc_rule = base.mark_line(color='green').encode(
x='FPR',
y='TPR',
size=alt.value(2)
)
(roc_curve + roc_rule).interactive()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8510 次 |
| 最近记录: |