![]() |
Plotting 3D surface plot for non-linear multivariate regression with 5 variables - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Plotting 3D surface plot for non-linear multivariate regression with 5 variables (/thread-28032.html) |
Plotting 3D surface plot for non-linear multivariate regression with 5 variables - khwajaosama - Jul-02-2020 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 for command "Z = clf.predict((X1,X2,X3,X,Y))"
|