如何可视化散点图中的非线性关系 [英] How to visualize a nonlinear relationship in a scatter plot
问题描述
我想直观地探索两个变量之间的关系。这种关系的功能形式在像这样的密集散点图中不可见:
如何在Python中为散点图添加最低平滑度?
或者您还有其他建议以可视方式探索非线性关系吗?
我尝试了请遵循以下步骤,但不能正常工作(请参考(简历):
是
来自 lowess
文档:
定义:lowess(endog,exog,frac = 0.6666666666666666,it = 3,delta = 0.0, is_sorted = False,缺少='drop',重新turn_sorted = True)
[...]
参数
----------
endog:1-D numpy数组
观测点的y值
exog:1维numpy数组
观测点的x值
它以其他顺序接受参数。它还不仅返回 y
:
> >最低(y,x)
数组([[0.00000000e + 00,1.13752478e + 00],
[1.00000000e-02,1.14087128e + 00],
[2.00000000e-02 ,1.14421582e + 00],
...,
[9.97000000e + 00,-5.17702654e-04],
[9.98000000e + 00,-5.94304755e-03],
[9.99000000e + 00,-1.13692896e-02]])
但如果您致电
ys = lowess(y,x)[:, 1]
您应该看到类似
I want to visually explore the relationship between two variables. The functional form of the relationship is not visible in dense scatter plots like this:
How can I add a lowess smooth to the scatter plot in Python?
Or do you have any other suggestions to visually explore non-linear relationships?
I tried the following but it didn't work properly (drawing on an example from Michiel de Hoon):
import numpy as np
from statsmodels.nonparametric.smoothers_lowess import lowess
x = np.arange(0,10,0.01)
ytrue = np.exp(-x/5.0) + 2*np.sin(x/3.0)
# add random errors with a normal distribution
y = ytrue + np.random.normal(size=len(x))
plt.scatter(x,y,color='cyan')
# calculate a smooth curve through the scatter plot
ys = lowess(x, y)
_ = plt.plot(x,ys,'red',linewidth=1)
# draw the true values for comparison
plt.plot(x,ytrue,'green',linewidth=3)
The lowess smoother (red lines) is strange.
EDIT:
The following matrix also includes lowess smoothers (taken from this question on CV):
Does someone have the code for such a graph?
From the lowess
documentation:
Definition: lowess(endog, exog, frac=0.6666666666666666, it=3, delta=0.0, is_sorted=False, missing='drop', return_sorted=True)
[...]
Parameters
----------
endog: 1-D numpy array
The y-values of the observed points
exog: 1-D numpy array
The x-values of the observed points
It accepts arguments in the other order. It also doesn't only return y
:
>>> lowess(y, x)
array([[ 0.00000000e+00, 1.13752478e+00],
[ 1.00000000e-02, 1.14087128e+00],
[ 2.00000000e-02, 1.14421582e+00],
...,
[ 9.97000000e+00, -5.17702654e-04],
[ 9.98000000e+00, -5.94304755e-03],
[ 9.99000000e+00, -1.13692896e-02]])
But if you call
ys = lowess(y, x)[:,1]
you should see something like
这篇关于如何可视化散点图中的非线性关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!