拟合 3D 点 python [英] Fitting 3D points python

查看:65
本文介绍了拟合 3D 点 python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 python 代码,它生成一个由数字 x、y 和 z 组成的 3 元组列表.我想使用 scipy curve_fit 拟合 z= f(x,y).这是一些非工作代码

I have python code that produces a list of 3-tuples of numbers x, y and z. I would like to fit z= f(x,y) using scipy curve_fit. Here is some non-working code

A = [(19,20,24), (10,40,28), (10,50,31)]

def func(x,y,a, b):
    return x*y*a + b

如何让python使这个函数适合列表A中的数据?

How can I get python to fit this function to the data in the list A?

推荐答案

  • func 的第一个参数必须是数据(x 和 y).
  • func 的其余参数代表参数.
    • The first argument to func must be the data (both x and y).
    • The rest of the arguments to func represent the parameters.
    • 所以你需要稍微修改一下你的func:

      So you need to modify your func a bit:

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

      <小时>

      • curve_fit 的第一个参数是函数.
      • 第二个参数是独立的data(xy在一种数组的形式).
      • 第三个参数是依赖数据 (z).
      • 第四个参数是对参数值的猜测 (ab 在这种情况下.)

        • The first argument to curve_fit is the function.
        • The second argument is the independent data (x and y in the form of one array).
        • The third argument is the dependent data (z).
        • The fourth argument is a guess for the value of the parameters (a and b in this case.)
        • 例如:

          params, pcov = optimize.curve_fit(func, A[:,:2], A[:,2], guess)
          

          <小时>

          import scipy.optimize as optimize
          import numpy as np
          
          A = np.array([(19,20,24), (10,40,28), (10,50,31)])
          
          def func(data, a, b):
              return data[:,0]*data[:,1]*a + b
          
          guess = (1,1)
          params, pcov = optimize.curve_fit(func, A[:,:2], A[:,2], guess)
          print(params)
          # [ 0.04919355  6.67741935]
          

          这篇关于拟合 3D 点 python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆