简体   繁体   中英

Encoding error using altair interactive plot?

I'm trying to use the following data df_roc to plot an ROC curve using altair:

    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

This is the code I'm attempting to use in order to make the interactive 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()

And this is the error I'm getting:

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

I've tried to google around a bit and try some information on it, but there wasn't really much. Has anyone come across a solve for this or help me to find a way around it?

I would really prefer to be able to use altair for this as opposed to other plotting packages.

Can anyone help me?

Column names in Altair (and in pandas in general) are case-sensitive. It appears that your data has columns named "TPR" and "FPR' , but your chart specifies columns named "tpr" and "fpr" .

Change the case and your chart should work:

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

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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