altair:在回归中访问 rSquared 值 [英] altair: Access rSquared-value in a regression
本文介绍了altair:在回归中访问 rSquared 值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用这个例子
此外,我想将 rSquared 值作为文本添加到图表中.如何访问该值?根据文档,它应该是这样的:
chart + chart.transform_regression('x', 'y', params=True).mark_text()
解决方案
使用 mark_text()
时,您需要指定 x 和 y 位置(或编码)以及标签您要显示的文本值:
将 altair 导入为 alt将熊猫导入为 pd将 numpy 导入为 npnp.random.seed(42)x = np.linspace(0, 10)y = x - 5 + np.random.randn(len(x))df = pd.DataFrame({'x': x, 'y': y})图表 = alt.Chart(df).mark_point().encode(x='x',y='y')line = chart.transform_regression('x', 'y').mark_line()参数 = alt.Chart(df).transform_regression('x', 'y', 参数=真).mark_text(align='left').encode(x=alt.value(20), # 左起像素y=alt.value(20), # 从顶部开始的像素text='rSquared:N')图表 + 线 + 参数
I am using this example https://altair-viz.github.io/user_guide/transform/regression.html for plotting a regression trendline in altair.
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'
)
chart + chart.transform_regression('x', 'y').mark_line()
Additionally, I want to add the rSquared-value as text to the chart. How can I access the value? According to the documentation, it should be something like:
chart + chart.transform_regression('x', 'y', params=True).mark_text()
解决方案
When using mark_text()
you'll need to specify the x and y location (or encoding) along with the label of the text value you want to show:
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'
)
chart + line + params
这篇关于altair:在回归中访问 rSquared 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文