[英]Altair: Extract and display regression coefficients
這個問題解決了如何使用mark_text()
訪問和顯示 R 2值
我有興趣訪問和顯示系數。 如文檔中所述,將rSquared
替換為coef
會產生截距和斜率的扁平數組。
如何索引到這個數組以僅顯示其中一個值,例如斜率? 我想知道mark_text()
步驟之前是否應該進行轉換(可能是transform_filter()
,或者是否可以使用altair.Text()
。
抱歉,如果這是一個非常簡單的問題。 提前致謝。
import altair as alt
import pandas as pd
import numpy as np
np.random.seed(42)
x = np.linspace(0, 10)
y = x - 5 + np.random.randn(len(x))
df = pd.DataFrame({'x': x, 'y': y})
chart = alt.Chart(df).mark_point().encode(
x='x',
y='y'
)
line = chart.transform_regression('x', 'y').mark_line()
params = alt.Chart(df).transform_regression(
'x', 'y', params=True
).mark_text(align='left').encode(
x=alt.value(20), # pixels from left
y=alt.value(20), # pixels from top
text='rSquared:N',
# text='coef:N' # flattened array
# text='coef[0]:N' # fails
)
chart + line + params
您可以使用計算轉換來訪問它:
params = alt.Chart(df).transform_regression(
'x', 'y', params=True
).transform_calculate(
intercept='datum.coef[0]',
slope='datum.coef[1]',
).mark_text(align='left').encode(
x=alt.value(20), # pixels from left
y=alt.value(20), # pixels from top
text='intercept:N'
)
chart + line + params
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.