Setting python 3D points

I have python code that creates a list of 3 sets of numbers x, y and z. I would like to set z = f (x, y) using scipy curve_fit. Here are some broken codes

A = [(19,20,24), (10,40,28), (10,50,31)] def func(x,y,a, b): return x*y*a + b 

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

+4
source share
1 answer
  • The first argument to func should be data (both x and y).
  • The remaining arguments to func represent the parameters.

So you need to change your func bit:

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

  • The first argument to curve_fit is the function.
  • The second argument is the independent data ( x and y in the form of a single array).
  • The third argument is the dependent data ( z ).
  • The fourth argument is the assumption for the parameter value ( a and b in this case.)

So for example:

 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] 
+8
source

Source: https://habr.com/ru/post/1469276/


All Articles