我想直观地探索两个变量之间的关系。这种关系的函数形式在像这样的密集散点图中是不可见的:
如何在 Python 中向散点图添加低平滑度?
或者您有任何其他视觉探索非线性关系的建议吗?
我尝试了以下但它没有正常工作(借鉴 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)
较低的平滑器(红线)很奇怪。
编辑:
以下矩阵还包括低平滑器(取自 CV 上的 this question):
有人有这种图表的代码吗?
最佳答案
你也可以使用 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)
关于python - 如何在散点图中可视化非线性关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23784399/