<p>您可以使用<a href="https://en.wikipedia.org/wiki/Local_regression" rel="nofollow noreferrer">LOESS</a>或<a href="https://en.wikipedia.org/wiki/Kernel_regression#Nadaraya%E2%80%93Watson_kernel_regression" rel="nofollow noreferrer">Nadarya-Watson estimator</a>(和变体)从离散数据获得平滑曲线。这些方法执行“移动平均”技术,因此它们通常不会产生原始数据范围以外的点(对于原始数据域内的<code>x</code>值)</p>
<p>有一个<a href="https://github.com/csbrown/pylomo" rel="nofollow noreferrer">convenient python library</a>(完整的免责声明,我是该库的作者)使此类模型易于构建。注意,结果通常不会通过给定的数据点</p>
<p>当带宽参数变为0时,曲线将成为线性插值。当它变为无穷大时,曲线将变成一条普通的最小二乘回归线。这两个值之间的带宽在这两个极端之间产生连续的曲线序列</p>
<pre><code>import random
import numpy as np
import matplotlib.pyplot as plt
import sklearn.linear_model
import local_models.local_models as pylomo
np.random.seed(1)
x = [1, 2, 3, 4]
y = [random.randint(0,10) for _ in range(4)]
x_test = np.linspace(min(x), max(x), 100)
kernel = pylomo.GaussianKernel(bandwidth=0.5)
LOESS = pylomo.LocalModels(sklearn.linear_model.LinearRegression(), kernel=kernel)
LOESS.fit(np.array(x).reshape(-1,1), np.array(y))
y_pred = LOESS.predict(x_test.reshape(-1,1))
plt.plot(x,y)
plt.plot(x_test, y_pred, c='r')
plt.show()
</code></pre>
<p><a href="https://i.stack.imgur.com/VUn4i.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VUn4i.png" alt="enter image description here"/></a></p>