python - matplotlibplot_surface用于二维多元线性回归

标签 python matplotlib linear-regression mplot3d

我有许多具有三个维度的数据点:x1、x2 和 y。我能够计算这些点的多元线性回归,并且能够在 3D 散点图上显示这些点,但我不知道如何绘制为这些点计算的多元线性回归:相同您可以在一维线性回归中绘制一条最佳拟合线,我对为二维线性回归绘制“最佳拟合平面”感兴趣。

我的代码如下:

import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt

# collect data into numpy arrays
X = []
Y = []
for line in open('data_2d.csv'):
    x1, x2, y = line.split(',')
    X.append([1, float(x1), float(x2)]) # here X[i][0] represents x0 = 1
    Y.append(float(y))
X = np.array(X)
Y = np.array(Y)

# calculate weights
w = np.linalg.solve(np.dot(X.T,X), np.dot(X.T, Y))
Yhat = np.dot(X, w) # results of linear regression for data points

# plot results
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(X[:,1], X[:,2], Y)
ax.plot_surface(X[:,1], X[:,2], Yhat) # doesn't seem to work
plt.show()

最佳答案

结果是 plot_surface 要求它的每个输入都是坐标矩阵,而不是像我之前使用的那样的值列表。我的解决方案如下:

import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt

# collect data into numpy arrays
X = []
Y = []
for line in open('data_2d.csv'): # contains 3 columns: x1, x2, and y
    x1, x2, y = line.split(',')
    X.append([1, float(x1), float(x2)]) # here X[i][0] represents x0 = 1
    Y.append(float(y))
X = np.array(X)
Y = np.array(Y)

# calculate weights for computing solutions
w = np.linalg.solve(X.T.dot(X), X.T.dot(Y))

# calculate r-squared error given weights
Yhat = X.dot(w)
d1 = Y - Yhat
d2 = Y - Y.mean()
r2 = 1 - d1.dot(d1) / d2.dot(d2)
print("r-squared value of", r2)

# calculate plane of best fit
divs = 2 # number of divisions in surface: generates divs^2 points.
         # The surface is a plane, so just 2^2 = 4 points can define it.
# plane spans all values of x1 and x2 from data
x1_range = np.linspace(min(X[:,1]),max(X[:,1]),divs)
x2_range = np.linspace(min(X[:,2]),max(X[:,2]),divs)
X_plane = []
for i in range(divs):
    for j in range(divs):
        X_plane.append([1, x1_range[i], x2_range[j]])
X_plane = np.array(X_plane)
# values of y are equal to the linear regression of points on the plane
Yhat2 = X_plane.dot(w)

# rearrange Yhat2 into a coordinate matrix for display as a surface
Yhat2_surface = []
for i in range(divs):
    Yhat2_surface.append(Yhat2[ divs*i : divs*i+divs ])
Yhat2_surface = np.array(Yhat2_surface)
Yhat2 = Yhat2_surface

# generate coordinate matrices for x1 and x2 values
X2, X1 = np.meshgrid(x1_range, x2_range) # intentional ordering: X2, *then* X1

# plot results
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(X[:,1], X[:,2], Y) # supplied data
ax.plot_surface(X1, X2, Yhat2, color='y', alpha=0.1) # plane of best fit
plt.show()

The output is shown here.点代表输入数据,黄色矩形代表其最佳拟合平面,用 plot_surface 绘制。

关于python - matplotlibplot_surface用于二维多元线性回归,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45537055/

相关文章:

python - 在 matplotlib 中创建发散堆积条形图

python - 将嵌套列表转换为python中的字典

python - 将 CSV 中的列编码为 Base64

c# - Python 原始字符串 "r"标志在 C# 中等效

python - 从最佳拟合线找出斜率趋势

python - 如何 reshape 测试数据框,以便维度与训练和预测工作中使用的维度相同?

python - tensorflow 中的 L-0 矩阵范数

python - python 中内置聚合函数

python - 在 pandas 的 seaborn clustermap 中设置 col_colors

python - matplotlib pyplot.show : Invalid RGBA