surface fitting library for Python?

G

Grant Edwards

I'm looking for a surface fitting library for Python.

Something like the least-squares module in scientific python,
except I want to fit a surface (z = f(x,y)) instead of a curve
(y = f(x)).

I found a couple other curve fitting libraries (some are
wrappers to C/Fortran libs I don't have), and I found a
curve and surface-fitting web page (www.zuzun.com) that's
written in Python -- I found a couple postings by James R.
Phillips (zunzun's author) and I suspect he's calling a
non-Python library of some sort.

What's the recommended method for doing surface fitting in
Python?
 
D

David M. Cooke

At some point said:
I'm looking for a surface fitting library for Python.

Something like the least-squares module in scientific python,
except I want to fit a surface (z = f(x,y)) instead of a curve
(y = f(x)).

You mean Konrad Hinsen's ScientificPython?
Scientific.Functions.LeastSquares.leastSquaresFit should still work
for you. You just need to make your data a list of tuples of (x,y) and z.

Example:

from Scientific.Functions.LeastSquares import leastSquaresFit

data = [ ((x1,y1), z1), ((x2,y2), z2), ...]

parameters = [initialc0, initialc1, initialc2]
def model(params, xy):
c0, c1, c2 = params
x, y = xy
return c0*x + c1*sin(c2*y)

params, chisquared = leastSquaresFit(model, parameters, data)

If your model isn't amiable to having derivatives taken, you might
check out the wrapping by Robert Kern of ODRPACK:
http://starship.python.net/crew/kernr/Projects.html
I've used this successfully.
 
C

Carl Banks

Grant Edwards said:
I'm looking for a surface fitting library for Python.

Something like the least-squares module in scientific python,
except I want to fit a surface (z = f(x,y)) instead of a curve
(y = f(x)).


Idea: see if you can use the versatility of Python to fool SciPy into
fitting a surface when it thinks it's fitting a curve. I don't know
the exact interface of the function you speak of, but I assume you
pass in a function taking two arguments (a paramter vector and an
independent variable), a data vector of x values, and a data vector of
f(x) values. I wonder if these vectors can be Python vectors, or do
they have to be a Numeric vectors? If a Python vector suffices, then
you can perhaps write your function to accept a tuple rather than a
single independent value:

def f(paramters,(x,y)):
whatever

Then, for your independent values vector, instead of [1,2,3,4] you'd
use with a 2-D curve, use [(1,1),(1,2),(2,1)].

Even if a Scipy requires Numeric vectors, I think there's a good
chance it can still do this. Knowing what I know about Numeric, I
suspect SciPy might do the right thing if you pass in a rank 2 array
as the independent variable vector. It might directly pass the an
element of your rank 2 array, which is a rank 1 array, to your
function (and it might not).

I found a couple other curve fitting libraries (some are
wrappers to C/Fortran libs I don't have), and I found a
curve and surface-fitting web page (www.zuzun.com) that's
written in Python -- I found a couple postings by James R.
Phillips (zunzun's author) and I suspect he's calling a
non-Python library of some sort.

What's the recommended method for doing surface fitting in
Python?

If SciPy can't do it, it's not too hard to do it by hand, unless there
are bounds or unless you want to minimize a non-Euclidean distance.
The setup is the hard part; Numeric can do solve a linear least
squares problem in one call.
 
G

Grant Edwards

You mean Konrad Hinsen's ScientificPython?

That's the one.
Scientific.Functions.LeastSquares.leastSquaresFit should still
work for you. You just need to make your data a list of tuples
of (x,y) and z.

Example:

[...]

Well, that's entirely too easy. For some reason I convinced
myself that wouldn't work (though I hadn't tried it).
If your model isn't amiable to having derivatives taken,

That I don't know yet. We haven't chosen a model, since we're
still working on gather the data.
you might check out the wrapping by Robert Kern of ODRPACK:
http://starship.python.net/crew/kernr/Projects.html I've used
this successfully.

I will. Thanks!
 
G

Grant Edwards

Idea: see if you can use the versatility of Python to fool SciPy into
fitting a surface when it thinks it's fitting a curve.

Cool -- that's the plan for now...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top