1. 第一种，多项式展开，在自变量x1,x2等的基础上构建新的自变量组合，比如x1的平方，x2的平方，x1*x2等选项；

2. 第二种，局部加权线性回归

>>> import numpy as np>>> def lwlr(testPoint, xArr, yArr, k=1.0):...     xMat = np.mat(xArr); yMat = np.mat(yArr).T...     m = np.shape(xMat)[0]...     weights = np.mat(np.eye((m)))...     for j in range(m): ...         diffMat = testPoint - xMat[j, :]...         weights[j, j] = np.exp(diffMat * diffMat.T / (-2.0 * k ** 2))...     xTx = xMat.T * (weights * xMat)...     if linalg.det(xTx) == 0.0:...         print("行列式为0，奇异矩阵，不能做逆")...         return...     ws = xTx.I * (xMat.T * (weights * yMat))...     return testPoint * ws...

>>> def lwlrTest(testArr, xArr, yArr, k=1.0): ...     m = np.shape(testArr)[0]...     yHat = np.zeros(m)...     for i in range(m):...         yHat[i] = lwlr(testArr[i], xArr, yArr, k)...     return yHat...

>>> yHat_k1 = lwlrTest(xArr, xArr, yArr, 1)>>> yHat_k2 = lwlrTest(xArr, xArr, yArr, 0.01)>>> yHat_k3 = lwlrTest(xArr, xArr, yArr, 0.003)>>> xMat = mat(xArr)>>> srtInd = xMat[:, 1].argsort(0)>>> xSort = xMat[srtInd][:, 0, :]>>> fig, axes = plt.subplots(1, 3)>>> axes[0].plot(xSort[:, 1], yHat_k1[srtInd], color='red')[<matplotlib.lines.Line2D object at 0x00D8E0E8>]>>> axes[0].scatter(xMat[:, 1].flatten().A[0], mat(yArr).T.flatten().A[0])<matplotlib.collections.PathCollection object at 0x00D8E238>>>> axes[0].set_title('k=1')Text(0.5, 1.0, 'k=1')>>> axes[1].plot(xSort[:, 1], yHat_k2[srtInd], color='red')[<matplotlib.lines.Line2D object at 0x00D8E9A0>]>>> axes[1].scatter(xMat[:, 1].flatten().A[0], mat(yArr).T.flatten().A[0])<matplotlib.collections.PathCollection object at 0x00D8E0B8>>>> axes[1].set_title('k=0.01')Text(0.5, 1.0, 'k=0.01')>>> axes[2].plot(xSort[:, 1], yHat_k3[srtInd], color='red')[<matplotlib.lines.Line2D object at 0x00EA26E8>]>>> axes[2].scatter(xMat[:, 1].flatten().A[0], mat(yArr).T.flatten().A[0])<matplotlib.collections.PathCollection object at 0x00EA2790>>>> axes[2].set_title('k=0.003')Text(0.5, 1.0, 'k=0.003')>>> plt.show()

·end·