python - 如何使用 matplotlib 绘制具有 2 个特征的 3D 多重线性回归?

标签 python matplotlib mplot3d

我需要在 matplotlib 中使用多个线性回归和 2 个特征来绘制 3D 图。我怎样才能做到这一点?

这是我的代码:

import pandas
from sklearn import linear_model

df = pandas.read_csv("cars.csv")

X = df[['Weight', 'Volume']]
y = df['CO2']

regr = linear_model.LinearRegression()

predictedCO2 = regr.predict([scaled[0]])
print(predictedCO2)

最佳答案

因此,您想要绘制回归模型结果的 3D 图。在 3d 绘图中,对于每个点,(x, y, z) = (重量、体积、预测二氧化碳)。

现在您可以使用以下命令绘制它:

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

# dummy variables for demonstration
x = [random.random()*100 for _ in range(100)]
y = [random.random()*100 for _ in range(100)]
z = [random.random()*100 or _ in range(100)]

# build the figure instance
fig = plt.figure(figsize=(10,8))
ax = fig.add_subplot(111, projection='3d')
ax.scatter(x, y, z, c='blue', marker='o')

# set your labels
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')

plt.show()

这会给你一个像这样的情节:

enter image description here

关于python - 如何使用 matplotlib 绘制具有 2 个特征的 3D 多重线性回归?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60252480/

相关文章:

python - 为所有 Flask 路由添加前缀

带有元组的 Python for 循环

matplotlib - 如何在 plotly 上绘制带注释的热图?

python - 如何自定义 sphinx.ext.autosummary 第一个模板?

python - 我如何知道生成器是否从一开始就为空?

python - 使用 matplotlib 绘制缩放和旋转的二元分布

python-3.x - 使用 Python 在图像上应用过滤器

python - 以透视方式绘制以 3D 投影的一系列 2D 图

python-3.x - matplotlib 0.99 Mplot3d 属性错误 : 'list' object has no attribute 'ndim'

python - mplot3D fill_between 超出轴限制