Jul-02-2020, 04:50 AM
(This post was last modified: Jul-02-2020, 04:51 AM by khwajaosama.)
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.
It gives me an error
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
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) |
Error:Found array with dim 3. Estimator expected <= 2.
for command "Z = clf.predict((X1,X2,X3,X,Y))"