Python surface fitting of variables of different dimensionto get unknown parameters?2019 Community Moderator ElectionDifference between append vs. extend list methods in PythonHow to get the current time in PythonGetting the last element of a list in PythonUse different Python version with virtualenvHow to get the number of elements in a list in Python?Fitting a sum to data in PythonCurve fitting in Python using scipyWhat exactly is the variance on the parameters of SciPy curve fit? (Python)I want to smoothen my python matplotlib plot, code not workingFit in python with curve_fit

Dot in front of file

How to simplify this time periods definition interface?

Converting Functions to Arrow functions

Replacing Windows 7 security updates with anti-virus?

Latest web browser compatible with Windows 98

How to deal with a cynical class?

At what level can a dragon innately cast its spells?

Is it possible to upcast ritual spells?

When do we add an hyphen (-) to a complex adjective word?

Why does Deadpool say "You're welcome, Canada," after shooting Ryan Reynolds in the end credits?

An Accountant Seeks the Help of a Mathematician

It's a yearly task, alright

Humanity loses the vast majority of its technology, information, and population in the year 2122. How long does it take to rebuild itself?

How can I change step-down my variable input voltage? [Microcontroller]

Good allowance savings plan?

Why are the outputs of printf and std::cout different

Sword in the Stone story where the sword was held in place by electromagnets

How to deal with taxi scam when on vacation?

How is the Swiss post e-voting system supposed to work, and how was it wrong?

Does this AnyDice function accurately calculate the number of ogres you make unconcious with three 4th-level castings of Sleep?

Instead of Universal Basic Income, why not Universal Basic NEEDS?

Why would a flight no longer considered airworthy be redirected like this?

Why are there 40 737 Max planes in flight when they have been grounded as not airworthy?

Be in awe of my brilliance!



Python surface fitting of variables of different dimensionto get unknown parameters?



2019 Community Moderator ElectionDifference between append vs. extend list methods in PythonHow to get the current time in PythonGetting the last element of a list in PythonUse different Python version with virtualenvHow to get the number of elements in a list in Python?Fitting a sum to data in PythonCurve fitting in Python using scipyWhat exactly is the variance on the parameters of SciPy curve fit? (Python)I want to smoothen my python matplotlib plot, code not workingFit in python with curve_fit










2















I have a function that includes x and y as independent variables and I want to fit the parameters to the data and function and plot a surface figure. I saw that if the variables have two different dimensions, I can use np.meshgrid(x,y), but then how do I find the parameters a,b,c? My code looks like this:



import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
import numpy as np

x = np.array([1,0.5,0.33,0.25,0.2])
y = np.array([1e-9,1e-8,1e-7,1e-6,1e-5,1e-4,1e-3,1e-2,1e-1,1e0,1e1,1e2,1e3,1e4,1e5])

def func(x,y,a,b,c):
return (1-(a/(a+y)^b))*(1-np.exp(-c*x))

x,y = np.meshgrid(x,y)


Can I still use curve_fit for this type of function? If so, how can I use it to find a,b,c and also plot the 3d function?










share|improve this question
























  • curve_fit is a scipy function; you should use the relevant tags.

    – ImportanceOfBeingErnest
    Mar 6 at 19:25















2















I have a function that includes x and y as independent variables and I want to fit the parameters to the data and function and plot a surface figure. I saw that if the variables have two different dimensions, I can use np.meshgrid(x,y), but then how do I find the parameters a,b,c? My code looks like this:



import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
import numpy as np

x = np.array([1,0.5,0.33,0.25,0.2])
y = np.array([1e-9,1e-8,1e-7,1e-6,1e-5,1e-4,1e-3,1e-2,1e-1,1e0,1e1,1e2,1e3,1e4,1e5])

def func(x,y,a,b,c):
return (1-(a/(a+y)^b))*(1-np.exp(-c*x))

x,y = np.meshgrid(x,y)


Can I still use curve_fit for this type of function? If so, how can I use it to find a,b,c and also plot the 3d function?










share|improve this question
























  • curve_fit is a scipy function; you should use the relevant tags.

    – ImportanceOfBeingErnest
    Mar 6 at 19:25













2












2








2








I have a function that includes x and y as independent variables and I want to fit the parameters to the data and function and plot a surface figure. I saw that if the variables have two different dimensions, I can use np.meshgrid(x,y), but then how do I find the parameters a,b,c? My code looks like this:



import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
import numpy as np

x = np.array([1,0.5,0.33,0.25,0.2])
y = np.array([1e-9,1e-8,1e-7,1e-6,1e-5,1e-4,1e-3,1e-2,1e-1,1e0,1e1,1e2,1e3,1e4,1e5])

def func(x,y,a,b,c):
return (1-(a/(a+y)^b))*(1-np.exp(-c*x))

x,y = np.meshgrid(x,y)


Can I still use curve_fit for this type of function? If so, how can I use it to find a,b,c and also plot the 3d function?










share|improve this question
















I have a function that includes x and y as independent variables and I want to fit the parameters to the data and function and plot a surface figure. I saw that if the variables have two different dimensions, I can use np.meshgrid(x,y), but then how do I find the parameters a,b,c? My code looks like this:



import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
import numpy as np

x = np.array([1,0.5,0.33,0.25,0.2])
y = np.array([1e-9,1e-8,1e-7,1e-6,1e-5,1e-4,1e-3,1e-2,1e-1,1e0,1e1,1e2,1e3,1e4,1e5])

def func(x,y,a,b,c):
return (1-(a/(a+y)^b))*(1-np.exp(-c*x))

x,y = np.meshgrid(x,y)


Can I still use curve_fit for this type of function? If so, how can I use it to find a,b,c and also plot the 3d function?







python matplotlib scipy function-fitting






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 6 at 19:38







Mary Jacketti

















asked Mar 6 at 18:59









Mary JackettiMary Jacketti

196




196












  • curve_fit is a scipy function; you should use the relevant tags.

    – ImportanceOfBeingErnest
    Mar 6 at 19:25

















  • curve_fit is a scipy function; you should use the relevant tags.

    – ImportanceOfBeingErnest
    Mar 6 at 19:25
















curve_fit is a scipy function; you should use the relevant tags.

– ImportanceOfBeingErnest
Mar 6 at 19:25





curve_fit is a scipy function; you should use the relevant tags.

– ImportanceOfBeingErnest
Mar 6 at 19:25












1 Answer
1






active

oldest

votes


















2














Here is an example with 3D scatterplot, 3D surface plot, and a contour plot.



import numpy, scipy, scipy.optimize
import matplotlib
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm # to colormap 3D surfaces from blue to red
import matplotlib.pyplot as plt

graphWidth = 800 # units are pixels
graphHeight = 600 # units are pixels

# 3D contour plot lines
numberOfContourLines = 16


def SurfacePlot(func, data, fittedParameters):
f = plt.figure(figsize=(graphWidth/100.0, graphHeight/100.0), dpi=100)

matplotlib.pyplot.grid(True)
axes = Axes3D(f)

x_data = data[0]
y_data = data[1]
z_data = data[2]

xModel = numpy.linspace(min(x_data), max(x_data), 20)
yModel = numpy.linspace(min(y_data), max(y_data), 20)
X, Y = numpy.meshgrid(xModel, yModel)

Z = func(numpy.array([X, Y]), *fittedParameters)

axes.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm, linewidth=1, antialiased=True)

axes.scatter(x_data, y_data, z_data) # show data along with plotted surface

axes.set_title('Surface Plot (click-drag with mouse)') # add a title for surface plot
axes.set_xlabel('X Data') # X axis data label
axes.set_ylabel('Y Data') # Y axis data label
axes.set_zlabel('Z Data') # Z axis data label

plt.show()
plt.close('all') # clean up after using pyplot or else thaere can be memory and process problems


def ContourPlot(func, data, fittedParameters):
f = plt.figure(figsize=(graphWidth/100.0, graphHeight/100.0), dpi=100)
axes = f.add_subplot(111)

x_data = data[0]
y_data = data[1]
z_data = data[2]

xModel = numpy.linspace(min(x_data), max(x_data), 20)
yModel = numpy.linspace(min(y_data), max(y_data), 20)
X, Y = numpy.meshgrid(xModel, yModel)

Z = func(numpy.array([X, Y]), *fittedParameters)

axes.plot(x_data, y_data, 'o')

axes.set_title('Contour Plot') # add a title for contour plot
axes.set_xlabel('X Data') # X axis data label
axes.set_ylabel('Y Data') # Y axis data label

CS = matplotlib.pyplot.contour(X, Y, Z, numberOfContourLines, colors='k')
matplotlib.pyplot.clabel(CS, inline=1, fontsize=10) # labels for contours

plt.show()
plt.close('all') # clean up after using pyplot or else thaere can be memory and process problems


def ScatterPlot(data):
f = plt.figure(figsize=(graphWidth/100.0, graphHeight/100.0), dpi=100)

matplotlib.pyplot.grid(True)
axes = Axes3D(f)
x_data = data[0]
y_data = data[1]
z_data = data[2]

axes.scatter(x_data, y_data, z_data)

axes.set_title('Scatter Plot (click-drag with mouse)')
axes.set_xlabel('X Data')
axes.set_ylabel('Y Data')
axes.set_zlabel('Z Data')

plt.show()
plt.close('all') # clean up after using pyplot or else thaere can be memory and process problems


def func(data, a, b, c):
x = data[0]
y = data[1]
return a + (x**b) * (y**c)


if __name__ == "__main__":
xData = numpy.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0])
yData = numpy.array([11.0, 12.1, 13.0, 14.1, 15.0, 16.1, 17.0, 18.1, 90.0])
zData = numpy.array([1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.0, 9.9])

data = [xData, yData, zData]

initialParameters = [1.0, 1.0, 1.0] # these are the same as scipy default values in this example

# here a non-linear surface fit is made with scipy's curve_fit()
fittedParameters, pcov = scipy.optimize.curve_fit(func, [xData, yData], zData, p0 = initialParameters)

ScatterPlot(data)
SurfacePlot(func, data, fittedParameters)
ContourPlot(func, data, fittedParameters)

print('fitted prameters', fittedParameters)

modelPredictions = func(data, *fittedParameters)

absError = modelPredictions - zData

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





share|improve this answer























  • so even though my x and y are different dimensions, if i follow this format and use meshgrid, it should still work?

    – Mary Jacketti
    Mar 6 at 20:26











  • The X, Y, and Z data must have the same number of values for curve fitting with curve_fit() as shown - if after meshgrid this is true, then it should be possible.

    – James Phillips
    Mar 6 at 21:46












  • Hi, I tried to do everything the same as your example, except I changed the xData, yData, and zData to be [0.5,0.33,0.25,0.2] and [1e-9,1e-8,1e-7,1e-6,1e-5,1e-4] and [9.9981e-10,9.9504e-10,9.7905e-10,9.492e-10],[9.9951e-9,9.9097e-9,9.6711e-9,9.2698e-9],[9.9873e-8,9.8349e-8,9.4816e-8,8.947e-8],[9.967e-7,9.6971e-7,9.1801e-7,8.478e-7],[9.9139e-6,9.4416e-6,8.6995e-6,7.7991e-6],[9.7742e-5,8.9677e-5,7.9378e-5,6.8296e-5]] , respectively, and i just numpy.meshgrid the xData and yData to be the same size. But now I am getting an error.

    – Mary Jacketti
    Mar 7 at 0:55







  • 1





    oh nevermind it works when i flatten the data points!

    – Mary Jacketti
    Mar 7 at 1:05










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55030369%2fpython-surface-fitting-of-variables-of-different-dimensionto-get-unknown-paramet%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














Here is an example with 3D scatterplot, 3D surface plot, and a contour plot.



import numpy, scipy, scipy.optimize
import matplotlib
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm # to colormap 3D surfaces from blue to red
import matplotlib.pyplot as plt

graphWidth = 800 # units are pixels
graphHeight = 600 # units are pixels

# 3D contour plot lines
numberOfContourLines = 16


def SurfacePlot(func, data, fittedParameters):
f = plt.figure(figsize=(graphWidth/100.0, graphHeight/100.0), dpi=100)

matplotlib.pyplot.grid(True)
axes = Axes3D(f)

x_data = data[0]
y_data = data[1]
z_data = data[2]

xModel = numpy.linspace(min(x_data), max(x_data), 20)
yModel = numpy.linspace(min(y_data), max(y_data), 20)
X, Y = numpy.meshgrid(xModel, yModel)

Z = func(numpy.array([X, Y]), *fittedParameters)

axes.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm, linewidth=1, antialiased=True)

axes.scatter(x_data, y_data, z_data) # show data along with plotted surface

axes.set_title('Surface Plot (click-drag with mouse)') # add a title for surface plot
axes.set_xlabel('X Data') # X axis data label
axes.set_ylabel('Y Data') # Y axis data label
axes.set_zlabel('Z Data') # Z axis data label

plt.show()
plt.close('all') # clean up after using pyplot or else thaere can be memory and process problems


def ContourPlot(func, data, fittedParameters):
f = plt.figure(figsize=(graphWidth/100.0, graphHeight/100.0), dpi=100)
axes = f.add_subplot(111)

x_data = data[0]
y_data = data[1]
z_data = data[2]

xModel = numpy.linspace(min(x_data), max(x_data), 20)
yModel = numpy.linspace(min(y_data), max(y_data), 20)
X, Y = numpy.meshgrid(xModel, yModel)

Z = func(numpy.array([X, Y]), *fittedParameters)

axes.plot(x_data, y_data, 'o')

axes.set_title('Contour Plot') # add a title for contour plot
axes.set_xlabel('X Data') # X axis data label
axes.set_ylabel('Y Data') # Y axis data label

CS = matplotlib.pyplot.contour(X, Y, Z, numberOfContourLines, colors='k')
matplotlib.pyplot.clabel(CS, inline=1, fontsize=10) # labels for contours

plt.show()
plt.close('all') # clean up after using pyplot or else thaere can be memory and process problems


def ScatterPlot(data):
f = plt.figure(figsize=(graphWidth/100.0, graphHeight/100.0), dpi=100)

matplotlib.pyplot.grid(True)
axes = Axes3D(f)
x_data = data[0]
y_data = data[1]
z_data = data[2]

axes.scatter(x_data, y_data, z_data)

axes.set_title('Scatter Plot (click-drag with mouse)')
axes.set_xlabel('X Data')
axes.set_ylabel('Y Data')
axes.set_zlabel('Z Data')

plt.show()
plt.close('all') # clean up after using pyplot or else thaere can be memory and process problems


def func(data, a, b, c):
x = data[0]
y = data[1]
return a + (x**b) * (y**c)


if __name__ == "__main__":
xData = numpy.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0])
yData = numpy.array([11.0, 12.1, 13.0, 14.1, 15.0, 16.1, 17.0, 18.1, 90.0])
zData = numpy.array([1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.0, 9.9])

data = [xData, yData, zData]

initialParameters = [1.0, 1.0, 1.0] # these are the same as scipy default values in this example

# here a non-linear surface fit is made with scipy's curve_fit()
fittedParameters, pcov = scipy.optimize.curve_fit(func, [xData, yData], zData, p0 = initialParameters)

ScatterPlot(data)
SurfacePlot(func, data, fittedParameters)
ContourPlot(func, data, fittedParameters)

print('fitted prameters', fittedParameters)

modelPredictions = func(data, *fittedParameters)

absError = modelPredictions - zData

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





share|improve this answer























  • so even though my x and y are different dimensions, if i follow this format and use meshgrid, it should still work?

    – Mary Jacketti
    Mar 6 at 20:26











  • The X, Y, and Z data must have the same number of values for curve fitting with curve_fit() as shown - if after meshgrid this is true, then it should be possible.

    – James Phillips
    Mar 6 at 21:46












  • Hi, I tried to do everything the same as your example, except I changed the xData, yData, and zData to be [0.5,0.33,0.25,0.2] and [1e-9,1e-8,1e-7,1e-6,1e-5,1e-4] and [9.9981e-10,9.9504e-10,9.7905e-10,9.492e-10],[9.9951e-9,9.9097e-9,9.6711e-9,9.2698e-9],[9.9873e-8,9.8349e-8,9.4816e-8,8.947e-8],[9.967e-7,9.6971e-7,9.1801e-7,8.478e-7],[9.9139e-6,9.4416e-6,8.6995e-6,7.7991e-6],[9.7742e-5,8.9677e-5,7.9378e-5,6.8296e-5]] , respectively, and i just numpy.meshgrid the xData and yData to be the same size. But now I am getting an error.

    – Mary Jacketti
    Mar 7 at 0:55







  • 1





    oh nevermind it works when i flatten the data points!

    – Mary Jacketti
    Mar 7 at 1:05















2














Here is an example with 3D scatterplot, 3D surface plot, and a contour plot.



import numpy, scipy, scipy.optimize
import matplotlib
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm # to colormap 3D surfaces from blue to red
import matplotlib.pyplot as plt

graphWidth = 800 # units are pixels
graphHeight = 600 # units are pixels

# 3D contour plot lines
numberOfContourLines = 16


def SurfacePlot(func, data, fittedParameters):
f = plt.figure(figsize=(graphWidth/100.0, graphHeight/100.0), dpi=100)

matplotlib.pyplot.grid(True)
axes = Axes3D(f)

x_data = data[0]
y_data = data[1]
z_data = data[2]

xModel = numpy.linspace(min(x_data), max(x_data), 20)
yModel = numpy.linspace(min(y_data), max(y_data), 20)
X, Y = numpy.meshgrid(xModel, yModel)

Z = func(numpy.array([X, Y]), *fittedParameters)

axes.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm, linewidth=1, antialiased=True)

axes.scatter(x_data, y_data, z_data) # show data along with plotted surface

axes.set_title('Surface Plot (click-drag with mouse)') # add a title for surface plot
axes.set_xlabel('X Data') # X axis data label
axes.set_ylabel('Y Data') # Y axis data label
axes.set_zlabel('Z Data') # Z axis data label

plt.show()
plt.close('all') # clean up after using pyplot or else thaere can be memory and process problems


def ContourPlot(func, data, fittedParameters):
f = plt.figure(figsize=(graphWidth/100.0, graphHeight/100.0), dpi=100)
axes = f.add_subplot(111)

x_data = data[0]
y_data = data[1]
z_data = data[2]

xModel = numpy.linspace(min(x_data), max(x_data), 20)
yModel = numpy.linspace(min(y_data), max(y_data), 20)
X, Y = numpy.meshgrid(xModel, yModel)

Z = func(numpy.array([X, Y]), *fittedParameters)

axes.plot(x_data, y_data, 'o')

axes.set_title('Contour Plot') # add a title for contour plot
axes.set_xlabel('X Data') # X axis data label
axes.set_ylabel('Y Data') # Y axis data label

CS = matplotlib.pyplot.contour(X, Y, Z, numberOfContourLines, colors='k')
matplotlib.pyplot.clabel(CS, inline=1, fontsize=10) # labels for contours

plt.show()
plt.close('all') # clean up after using pyplot or else thaere can be memory and process problems


def ScatterPlot(data):
f = plt.figure(figsize=(graphWidth/100.0, graphHeight/100.0), dpi=100)

matplotlib.pyplot.grid(True)
axes = Axes3D(f)
x_data = data[0]
y_data = data[1]
z_data = data[2]

axes.scatter(x_data, y_data, z_data)

axes.set_title('Scatter Plot (click-drag with mouse)')
axes.set_xlabel('X Data')
axes.set_ylabel('Y Data')
axes.set_zlabel('Z Data')

plt.show()
plt.close('all') # clean up after using pyplot or else thaere can be memory and process problems


def func(data, a, b, c):
x = data[0]
y = data[1]
return a + (x**b) * (y**c)


if __name__ == "__main__":
xData = numpy.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0])
yData = numpy.array([11.0, 12.1, 13.0, 14.1, 15.0, 16.1, 17.0, 18.1, 90.0])
zData = numpy.array([1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.0, 9.9])

data = [xData, yData, zData]

initialParameters = [1.0, 1.0, 1.0] # these are the same as scipy default values in this example

# here a non-linear surface fit is made with scipy's curve_fit()
fittedParameters, pcov = scipy.optimize.curve_fit(func, [xData, yData], zData, p0 = initialParameters)

ScatterPlot(data)
SurfacePlot(func, data, fittedParameters)
ContourPlot(func, data, fittedParameters)

print('fitted prameters', fittedParameters)

modelPredictions = func(data, *fittedParameters)

absError = modelPredictions - zData

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





share|improve this answer























  • so even though my x and y are different dimensions, if i follow this format and use meshgrid, it should still work?

    – Mary Jacketti
    Mar 6 at 20:26











  • The X, Y, and Z data must have the same number of values for curve fitting with curve_fit() as shown - if after meshgrid this is true, then it should be possible.

    – James Phillips
    Mar 6 at 21:46












  • Hi, I tried to do everything the same as your example, except I changed the xData, yData, and zData to be [0.5,0.33,0.25,0.2] and [1e-9,1e-8,1e-7,1e-6,1e-5,1e-4] and [9.9981e-10,9.9504e-10,9.7905e-10,9.492e-10],[9.9951e-9,9.9097e-9,9.6711e-9,9.2698e-9],[9.9873e-8,9.8349e-8,9.4816e-8,8.947e-8],[9.967e-7,9.6971e-7,9.1801e-7,8.478e-7],[9.9139e-6,9.4416e-6,8.6995e-6,7.7991e-6],[9.7742e-5,8.9677e-5,7.9378e-5,6.8296e-5]] , respectively, and i just numpy.meshgrid the xData and yData to be the same size. But now I am getting an error.

    – Mary Jacketti
    Mar 7 at 0:55







  • 1





    oh nevermind it works when i flatten the data points!

    – Mary Jacketti
    Mar 7 at 1:05













2












2








2







Here is an example with 3D scatterplot, 3D surface plot, and a contour plot.



import numpy, scipy, scipy.optimize
import matplotlib
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm # to colormap 3D surfaces from blue to red
import matplotlib.pyplot as plt

graphWidth = 800 # units are pixels
graphHeight = 600 # units are pixels

# 3D contour plot lines
numberOfContourLines = 16


def SurfacePlot(func, data, fittedParameters):
f = plt.figure(figsize=(graphWidth/100.0, graphHeight/100.0), dpi=100)

matplotlib.pyplot.grid(True)
axes = Axes3D(f)

x_data = data[0]
y_data = data[1]
z_data = data[2]

xModel = numpy.linspace(min(x_data), max(x_data), 20)
yModel = numpy.linspace(min(y_data), max(y_data), 20)
X, Y = numpy.meshgrid(xModel, yModel)

Z = func(numpy.array([X, Y]), *fittedParameters)

axes.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm, linewidth=1, antialiased=True)

axes.scatter(x_data, y_data, z_data) # show data along with plotted surface

axes.set_title('Surface Plot (click-drag with mouse)') # add a title for surface plot
axes.set_xlabel('X Data') # X axis data label
axes.set_ylabel('Y Data') # Y axis data label
axes.set_zlabel('Z Data') # Z axis data label

plt.show()
plt.close('all') # clean up after using pyplot or else thaere can be memory and process problems


def ContourPlot(func, data, fittedParameters):
f = plt.figure(figsize=(graphWidth/100.0, graphHeight/100.0), dpi=100)
axes = f.add_subplot(111)

x_data = data[0]
y_data = data[1]
z_data = data[2]

xModel = numpy.linspace(min(x_data), max(x_data), 20)
yModel = numpy.linspace(min(y_data), max(y_data), 20)
X, Y = numpy.meshgrid(xModel, yModel)

Z = func(numpy.array([X, Y]), *fittedParameters)

axes.plot(x_data, y_data, 'o')

axes.set_title('Contour Plot') # add a title for contour plot
axes.set_xlabel('X Data') # X axis data label
axes.set_ylabel('Y Data') # Y axis data label

CS = matplotlib.pyplot.contour(X, Y, Z, numberOfContourLines, colors='k')
matplotlib.pyplot.clabel(CS, inline=1, fontsize=10) # labels for contours

plt.show()
plt.close('all') # clean up after using pyplot or else thaere can be memory and process problems


def ScatterPlot(data):
f = plt.figure(figsize=(graphWidth/100.0, graphHeight/100.0), dpi=100)

matplotlib.pyplot.grid(True)
axes = Axes3D(f)
x_data = data[0]
y_data = data[1]
z_data = data[2]

axes.scatter(x_data, y_data, z_data)

axes.set_title('Scatter Plot (click-drag with mouse)')
axes.set_xlabel('X Data')
axes.set_ylabel('Y Data')
axes.set_zlabel('Z Data')

plt.show()
plt.close('all') # clean up after using pyplot or else thaere can be memory and process problems


def func(data, a, b, c):
x = data[0]
y = data[1]
return a + (x**b) * (y**c)


if __name__ == "__main__":
xData = numpy.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0])
yData = numpy.array([11.0, 12.1, 13.0, 14.1, 15.0, 16.1, 17.0, 18.1, 90.0])
zData = numpy.array([1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.0, 9.9])

data = [xData, yData, zData]

initialParameters = [1.0, 1.0, 1.0] # these are the same as scipy default values in this example

# here a non-linear surface fit is made with scipy's curve_fit()
fittedParameters, pcov = scipy.optimize.curve_fit(func, [xData, yData], zData, p0 = initialParameters)

ScatterPlot(data)
SurfacePlot(func, data, fittedParameters)
ContourPlot(func, data, fittedParameters)

print('fitted prameters', fittedParameters)

modelPredictions = func(data, *fittedParameters)

absError = modelPredictions - zData

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





share|improve this answer













Here is an example with 3D scatterplot, 3D surface plot, and a contour plot.



import numpy, scipy, scipy.optimize
import matplotlib
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm # to colormap 3D surfaces from blue to red
import matplotlib.pyplot as plt

graphWidth = 800 # units are pixels
graphHeight = 600 # units are pixels

# 3D contour plot lines
numberOfContourLines = 16


def SurfacePlot(func, data, fittedParameters):
f = plt.figure(figsize=(graphWidth/100.0, graphHeight/100.0), dpi=100)

matplotlib.pyplot.grid(True)
axes = Axes3D(f)

x_data = data[0]
y_data = data[1]
z_data = data[2]

xModel = numpy.linspace(min(x_data), max(x_data), 20)
yModel = numpy.linspace(min(y_data), max(y_data), 20)
X, Y = numpy.meshgrid(xModel, yModel)

Z = func(numpy.array([X, Y]), *fittedParameters)

axes.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm, linewidth=1, antialiased=True)

axes.scatter(x_data, y_data, z_data) # show data along with plotted surface

axes.set_title('Surface Plot (click-drag with mouse)') # add a title for surface plot
axes.set_xlabel('X Data') # X axis data label
axes.set_ylabel('Y Data') # Y axis data label
axes.set_zlabel('Z Data') # Z axis data label

plt.show()
plt.close('all') # clean up after using pyplot or else thaere can be memory and process problems


def ContourPlot(func, data, fittedParameters):
f = plt.figure(figsize=(graphWidth/100.0, graphHeight/100.0), dpi=100)
axes = f.add_subplot(111)

x_data = data[0]
y_data = data[1]
z_data = data[2]

xModel = numpy.linspace(min(x_data), max(x_data), 20)
yModel = numpy.linspace(min(y_data), max(y_data), 20)
X, Y = numpy.meshgrid(xModel, yModel)

Z = func(numpy.array([X, Y]), *fittedParameters)

axes.plot(x_data, y_data, 'o')

axes.set_title('Contour Plot') # add a title for contour plot
axes.set_xlabel('X Data') # X axis data label
axes.set_ylabel('Y Data') # Y axis data label

CS = matplotlib.pyplot.contour(X, Y, Z, numberOfContourLines, colors='k')
matplotlib.pyplot.clabel(CS, inline=1, fontsize=10) # labels for contours

plt.show()
plt.close('all') # clean up after using pyplot or else thaere can be memory and process problems


def ScatterPlot(data):
f = plt.figure(figsize=(graphWidth/100.0, graphHeight/100.0), dpi=100)

matplotlib.pyplot.grid(True)
axes = Axes3D(f)
x_data = data[0]
y_data = data[1]
z_data = data[2]

axes.scatter(x_data, y_data, z_data)

axes.set_title('Scatter Plot (click-drag with mouse)')
axes.set_xlabel('X Data')
axes.set_ylabel('Y Data')
axes.set_zlabel('Z Data')

plt.show()
plt.close('all') # clean up after using pyplot or else thaere can be memory and process problems


def func(data, a, b, c):
x = data[0]
y = data[1]
return a + (x**b) * (y**c)


if __name__ == "__main__":
xData = numpy.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0])
yData = numpy.array([11.0, 12.1, 13.0, 14.1, 15.0, 16.1, 17.0, 18.1, 90.0])
zData = numpy.array([1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.0, 9.9])

data = [xData, yData, zData]

initialParameters = [1.0, 1.0, 1.0] # these are the same as scipy default values in this example

# here a non-linear surface fit is made with scipy's curve_fit()
fittedParameters, pcov = scipy.optimize.curve_fit(func, [xData, yData], zData, p0 = initialParameters)

ScatterPlot(data)
SurfacePlot(func, data, fittedParameters)
ContourPlot(func, data, fittedParameters)

print('fitted prameters', fittedParameters)

modelPredictions = func(data, *fittedParameters)

absError = modelPredictions - zData

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






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 6 at 19:56









James PhillipsJames Phillips

1,722388




1,722388












  • so even though my x and y are different dimensions, if i follow this format and use meshgrid, it should still work?

    – Mary Jacketti
    Mar 6 at 20:26











  • The X, Y, and Z data must have the same number of values for curve fitting with curve_fit() as shown - if after meshgrid this is true, then it should be possible.

    – James Phillips
    Mar 6 at 21:46












  • Hi, I tried to do everything the same as your example, except I changed the xData, yData, and zData to be [0.5,0.33,0.25,0.2] and [1e-9,1e-8,1e-7,1e-6,1e-5,1e-4] and [9.9981e-10,9.9504e-10,9.7905e-10,9.492e-10],[9.9951e-9,9.9097e-9,9.6711e-9,9.2698e-9],[9.9873e-8,9.8349e-8,9.4816e-8,8.947e-8],[9.967e-7,9.6971e-7,9.1801e-7,8.478e-7],[9.9139e-6,9.4416e-6,8.6995e-6,7.7991e-6],[9.7742e-5,8.9677e-5,7.9378e-5,6.8296e-5]] , respectively, and i just numpy.meshgrid the xData and yData to be the same size. But now I am getting an error.

    – Mary Jacketti
    Mar 7 at 0:55







  • 1





    oh nevermind it works when i flatten the data points!

    – Mary Jacketti
    Mar 7 at 1:05

















  • so even though my x and y are different dimensions, if i follow this format and use meshgrid, it should still work?

    – Mary Jacketti
    Mar 6 at 20:26











  • The X, Y, and Z data must have the same number of values for curve fitting with curve_fit() as shown - if after meshgrid this is true, then it should be possible.

    – James Phillips
    Mar 6 at 21:46












  • Hi, I tried to do everything the same as your example, except I changed the xData, yData, and zData to be [0.5,0.33,0.25,0.2] and [1e-9,1e-8,1e-7,1e-6,1e-5,1e-4] and [9.9981e-10,9.9504e-10,9.7905e-10,9.492e-10],[9.9951e-9,9.9097e-9,9.6711e-9,9.2698e-9],[9.9873e-8,9.8349e-8,9.4816e-8,8.947e-8],[9.967e-7,9.6971e-7,9.1801e-7,8.478e-7],[9.9139e-6,9.4416e-6,8.6995e-6,7.7991e-6],[9.7742e-5,8.9677e-5,7.9378e-5,6.8296e-5]] , respectively, and i just numpy.meshgrid the xData and yData to be the same size. But now I am getting an error.

    – Mary Jacketti
    Mar 7 at 0:55







  • 1





    oh nevermind it works when i flatten the data points!

    – Mary Jacketti
    Mar 7 at 1:05
















so even though my x and y are different dimensions, if i follow this format and use meshgrid, it should still work?

– Mary Jacketti
Mar 6 at 20:26





so even though my x and y are different dimensions, if i follow this format and use meshgrid, it should still work?

– Mary Jacketti
Mar 6 at 20:26













The X, Y, and Z data must have the same number of values for curve fitting with curve_fit() as shown - if after meshgrid this is true, then it should be possible.

– James Phillips
Mar 6 at 21:46






The X, Y, and Z data must have the same number of values for curve fitting with curve_fit() as shown - if after meshgrid this is true, then it should be possible.

– James Phillips
Mar 6 at 21:46














Hi, I tried to do everything the same as your example, except I changed the xData, yData, and zData to be [0.5,0.33,0.25,0.2] and [1e-9,1e-8,1e-7,1e-6,1e-5,1e-4] and [9.9981e-10,9.9504e-10,9.7905e-10,9.492e-10],[9.9951e-9,9.9097e-9,9.6711e-9,9.2698e-9],[9.9873e-8,9.8349e-8,9.4816e-8,8.947e-8],[9.967e-7,9.6971e-7,9.1801e-7,8.478e-7],[9.9139e-6,9.4416e-6,8.6995e-6,7.7991e-6],[9.7742e-5,8.9677e-5,7.9378e-5,6.8296e-5]] , respectively, and i just numpy.meshgrid the xData and yData to be the same size. But now I am getting an error.

– Mary Jacketti
Mar 7 at 0:55






Hi, I tried to do everything the same as your example, except I changed the xData, yData, and zData to be [0.5,0.33,0.25,0.2] and [1e-9,1e-8,1e-7,1e-6,1e-5,1e-4] and [9.9981e-10,9.9504e-10,9.7905e-10,9.492e-10],[9.9951e-9,9.9097e-9,9.6711e-9,9.2698e-9],[9.9873e-8,9.8349e-8,9.4816e-8,8.947e-8],[9.967e-7,9.6971e-7,9.1801e-7,8.478e-7],[9.9139e-6,9.4416e-6,8.6995e-6,7.7991e-6],[9.7742e-5,8.9677e-5,7.9378e-5,6.8296e-5]] , respectively, and i just numpy.meshgrid the xData and yData to be the same size. But now I am getting an error.

– Mary Jacketti
Mar 7 at 0:55





1




1





oh nevermind it works when i flatten the data points!

– Mary Jacketti
Mar 7 at 1:05





oh nevermind it works when i flatten the data points!

– Mary Jacketti
Mar 7 at 1:05



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55030369%2fpython-surface-fitting-of-variables-of-different-dimensionto-get-unknown-paramet%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

1928 у кіно

Захаров Федір Захарович

Ель Греко