Python Forum
Plotting 3D surface plot for non-linear multivariate regression with 5 variables
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Plotting 3D surface plot for non-linear multivariate regression with 5 variables
#1
I am new to Python. I am trying to plot surface plots to show my model fit. I have developed a multivariate polynomial model using sklearn library. When I try to plot surface plot of the model prediction, the dimensions of meshgrid do not match with the expected dimension of the model.
import pandas as pd
df = pd.read_excel("Data.xlsx") # The file contains 6 columns and 46 rows
factors = ['a', 'b', 'c', 'd', 'e'] # These are the names of independent variables
target = df['f'] # Independent variable
var = df[factors]

import numpy as np
from sklearn.preprocessing import PolynomialFeatures
from sklearn import linear_model

poly = PolynomialFeatures(degree = 2)
Var = poly.fit_transform(var)
clf = linear_model.LinearRegression()
clf.fit(Var,target)
modelPredictions = clf.predict(Var)

# testing accuracy
absError = modelPredictions - target
SE = np.square(absError) # squared errors
MSE = np.mean(SE) # mean squared errors
RMSE = np.sqrt(MSE) # Root Mean Squared Error, RMSE
Rsquared = 1.0 - (np.var(absError) / np.var(target))
print('RMSE:', RMSE)
print('R-squared:', Rsquared)

# plotting surface plots
from mpl_toolkits import mplot3d
import matplotlib.pyplot as plt
X4 = np.linspace(0,6,100)
X5 = np.linspace(50,70,100)
X1 = np.ones((100,100))*1200
X2 = np.ones((100,100))*10
X3 = np.ones((100,100))*75
X,Y = np.meshgrid(X4,X5)

Z = clf.predict((X1,X2,X3,X,Y))
fig = plt.figure(figsize=(6,6))
ax = fig.add_subplot(111, projection='3d')

ax.plot_trisurf(X, Y, Z)
It gives me an error
Error:
Found array with dim 3. Estimator expected <= 2.
for command "Z = clf.predict((X1,X2,X3,X,Y))"
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Plot 3 variables Menthix 3 2,109 Nov-13-2021, 02:47 PM
Last Post: Menthix
  Multiple Plotting in Same PLot quest 0 1,797 Apr-18-2021, 10:29 AM
Last Post: quest
  Calculating surface area - - OOP or functional? Derek Banas Udemy course Drone4four 5 3,590 Mar-13-2021, 06:22 AM
Last Post: buran
  How to plot intraday data of several days in one plot mistermister 3 2,897 Dec-15-2020, 07:43 PM
Last Post: deanhystad
  3d Surface where Z is not a function of X and Y richterjan 2 1,719 Nov-11-2020, 04:22 PM
Last Post: michael1789
  Solve a system of linear equations with binary variables lopeslimagabriel 3 2,484 Sep-24-2020, 07:09 AM
Last Post: scidam
  matplotlib recursion error when repeatedly displaying a surface AdeIsHere 0 1,925 Sep-19-2019, 04:36 PM
Last Post: AdeIsHere
  How to plot vertically stacked plot with same x-axis and SriMekala 0 1,917 Jun-12-2019, 03:31 PM
Last Post: SriMekala
  How to plot implicit functions (with two variables) in scipy python using matplotlib? Jay_Nerella 1 7,926 May-11-2019, 01:17 AM
Last Post: scidam
  Multivariate mixed effects models primeprover 1 2,581 Aug-19-2018, 01:14 PM
Last Post: ketanm

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020