Visualizing Results
Now by using the parameters we have obtained from the curve_fit method plot the curve in the 3-D plane using the plot3D method.
Python3
# plotting the data points and the fitted curve fig = plt.figure() fig.set_figwidth( 40 ) fig.set_figheight( 10 ) ax = plt.axes(projection = '3d' ) ax.set_title( 'Curve fit plot' , fontsize = 15 ) ax.set_xlabel( 'x_1' , fontsize = 12 , color = 'green' ) ax.set_ylabel( 'x_2' , fontsize = 12 , color = 'green' ) ax.set_zlabel( 'y' , fontsize = 12 , color = 'green' ) ax.scatter3D(x_1, x_2, y, color = 'green' ) ax.plot3D(x_1, x_2, popt[ 0 ] * (x_1 * * 2 ) + popt[ 1 ] * (x_2 * * 2 ), color = 'black' ) plt.show() |
Output:
We can optimize our solution with the help of other parameters such as p0 and bounds. p0 is the Initial guess for the parameters (length N). If None, then the initial values will all be 1. Bounds are used to set lower and upper bounds on parameters. Defaults to no bounds.
Python – Scipy curve_fit with multiple independent variables
Curve fitting examines the relationship between one or more predictors (independent variables) and a response variable (dependent variable), with the goal of defining a “best fit” model of the relationship. It is the process of constructing a mathematical function, that has the best fit to a series of data points possibly subject to constraints.