我想直观地探索两个变量之间的关系。这种关系的函数形式在密集散点图中不可见,如下所示:
如何在Python中的散点图中添加低平滑度?
或者您还有其他建议来直观地探索非线性关系吗?
我尝试了以下方法,但它无法正常工作(借鉴来自米歇尔·德胡恩):
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)
Lowess 平滑器(红线)很奇怪。
EDIT:
The following matrix also includes lowess smoothers (taken from this question on CV):
有人有这样的图表的代码吗?
你也可以使用seaborn:
import numpy as np
import seaborn as sns
x = np.arange(0, 10, 0.01)
ytrue = np.exp(-x / 5) + 2 * np.sin(x / 3)
y = ytrue + np.random.normal(size=len(x))
sns.regplot(x, y, lowess=True)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)