使用 altair 交互图编码错误?

Encoding error using altair interactive plot?

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

这是我试图用来制作互动情节的代码:

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

我已经尝试 google 周围并尝试了一些关于它的信息,但实际上并没有太多。有没有人解决这个问题或帮助我找到解决方法?

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