Reputation: 2625
I need to plot a 3D plot with multiple Linear Regression with 2 features in matplotlib. How can I do that?
this is my code:
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)
Upvotes: 5
Views: 3112
Reputation: 1731
So you want to plot a 3d plot of your regression model's outcome. In your 3d plot, for each point you have (x, y, z) = (Weight, Volume, PredictedCO2).
Now you can plot it with:
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()
This will give you a plot like this:
Upvotes: 3