我想创建一个3d图,表示具有两个x变量的多元回归。我希望有实际y变量数据的散点图,以及表示由我的两个x变量生成的回归线的平面。更复杂的因素是所有变量都由矩阵表示。
# Variables
a # 14x1 matrix representing an x variable
b # 14x1 matrix representing an x variable
c # 14x1 matrix representing the y variable
BETAS # 2x1 matrix holding the beta coefficients of the x variables
z = (BETAS[0]*a + BETAS[1]*b) # the expected value of c based on our regression
new_array # variable representing the regression line - used previously to plot the regression line in 2d space`
# Plot in 3d
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = plt.axes(projection='3d')
plt.hold(True)
plt.plot(new_array[0], new_array[1], 'red')
ax.plot_surface(a, b, z)
plt.show()
非常感谢!