簡體   English   中英

如何使用 matplotlib 繪制具有 2 個特征的 3D 多重線性回歸?

[英]How to plot 3D multiple Linear Regression with 2 features using matplotlib?

我需要在 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) = (Weight, Volume, PredictedCO2)。

現在你可以用它來繪制它:

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()

這會給你一個這樣的情節:

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM