[英]How to visualize a nonlinear relationship in a scatter plot
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?如何在 Python 中为散点图添加一个lowess平滑?
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 ):我尝试了以下操作,但无法正常工作(借鉴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. Lowess 平滑器(红线)很奇怪。
EDIT:编辑:
The following matrix also includes lowess smoothers (taken from this question on CV):以下矩阵还包括 Lowess 平滑器(取自 CV 上的这个问题):
Does someone have the code for such a graph?有人有这种图的代码吗?
From the lowess
documentation:从
lowess
文档:
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
:它不仅返回
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你应该看到类似的东西
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.