Least square regression

发布于 2022-07-07  491 次阅读


Please refresh the page if equations are not rendered correctly.
---------------------------------------------------------------

Polynomial least square regression by numpy

# the dataset for regression
dataset = np.array([[0, 0], [1, 2], [2, 1], [3, 2], [4, 0.5]])
# Polynomial least square regression by numpy
# Polynomial degree
degree = 4
# Generate the polynomial basis
x = dataset[:,0]
X = np.vander(x, degree + 1)  # np.vstack([x, np.ones(len(x))]).T
# Generate the polynomial coefficients
# y = a0 + a1*x + a2*x^2 + a3*x^3 + ... + an*x^n
coef = np.linalg.lstsq(X, dataset[:,1])[0]
print('the coefficients of the polynomial are:', coef)

# Generate the polynomial
xGenerate = np.linspace(0, 4, 100)
XGenerate = np.vander(xGenerate, degree + 1)
yGenerate = np.dot(XGenerate, coef)
# Plot the polynomial
plt.scatter(x, dataset[:,1], label='Original data')
plt.plot(xGenerate, yGenerate, label='Polynomial')
plt.legend()
plt.grid(True)
plt.show()

Generate and plot plane in 3D

# the dataset for regression
# --------------------------------------------------
# normal of plane: n = (A, B, C)
# point on plane: p = (x0, y0, z0)
# equation of plane: A(x-x0) + B(y-y0) + C(z-z0) = 0
# namely: z = z0 - A/C*(x-x0) - B/C*(y-y0)
# --------------------------------------------------
def threeDPlot(x, y, z):
    """
    3d plot
    :param x, y, z: array-like, coordinates of points
    :return ax: 3d plot
    """
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    ax.plot_surface(x, y, z, alpha=0.4)
    ax.contour(x, y, z)
    plt.show()
    return ax


def generate_plane(n, p, x, y):
    """
    generate a plane
    :param n: array-like, normal of plane
    :param p: array-like, point on plane
    :return x, y, z: array-like, coordinates of points
    """
    A, B, C = n
    x0, y0, z0 = p
    x, y = np.meshgrid(x, y)
    z = z0 - A / C * (x - x0) - B / C * (y - y0)
    threeDPlot(x, y, z)
    return x, y, z


x = np.linspace(-3, 3, 50)
y = np.linspace(-3, 3, 50)

n = [1, 1, 1]
p = [0, 0, 0]

z = generate_plane(n, p, x, y)
plt.show()
Everything not saved will be lost.
最后更新于 2022-07-12