繁体   English   中英

使用 altair 交互式 plot 编码错误?

[英]Encoding error using altair interactive plot?

我正在尝试将以下数据df_roc用于 plot 使用 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

这是我试图用来制作交互式 plot 的代码:

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()

这是我得到的错误:

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(...)

我试着用谷歌搜索了一下,并尝试了一些关于它的信息,但实际上并没有太多。 有没有人遇到过解决这个问题或帮助我找到解决方法?

与其他绘图包相比,我真的更希望能够为此使用 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()

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM