簡體   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