Python Forum

Full Version: Plotting 3D surface plot for non-linear multivariate regression with 5 variables
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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))"