python中三维曲面的最小二乘拟合

python中三维曲面的最小二乘拟合,python,least-squares,data-fitting,Python,Least Squares,Data Fitting,我想用一些数据拟合我的曲面方程。我已经尝试了scipy.optimize.leastsq,但由于我无法指定边界,它会给我一个无法使用的结果。我还尝试了scipy.optimize.least_squares,但它给了我一个错误: ValueError: too many values to unpack 我的方程式是: f(x,y,z)=(x-A+y-B)/2+sqrt(((x-A-y+B)/2)^2+C*z^2) 应找到参数A、B、C,以便当以下点用于x、y、z时,上述方程尽可能接近于零:

我想用一些数据拟合我的曲面方程。我已经尝试了scipy.optimize.leastsq,但由于我无法指定边界,它会给我一个无法使用的结果。我还尝试了scipy.optimize.least_squares,但它给了我一个错误:

ValueError: too many values to unpack
我的方程式是:

f(x,y,z)=(x-A+y-B)/2+sqrt(((x-A-y+B)/2)^2+C*z^2)
应找到参数A、B、C,以便当以下点用于x、y、z时,上述方程尽可能接近于零:

    [
   [-0.071, -0.85, 0.401],
   [-0.138, -1.111, 0.494],
   [-0.317, -0.317, -0.317],
   [-0.351, -2.048, 0.848]
   ]
边界将是A>0,B>0,C>1


我怎样才能得到这样的健康呢?python中最好的工具是什么。我搜索了有关如何拟合3d曲面的示例,但大多数涉及函数拟合的示例都是关于直线或平面拟合的。

我编辑了此答案,以提供一个更一般的示例,说明如何使用scipy的general optimize.minimize方法以及scipy的optimize.least_squares方法解决此问题


首先让我们设置问题:

import numpy as np
import scipy.optimize

# ===============================================
# SETUP: define common compoments of the problem


def our_function(coeff, data):
    """
    The function we care to optimize.

    Args:
        coeff (np.ndarray): are the parameters that we care to optimize.
        data (np.ndarray): the input data
    """
    A, B, C = coeff
    x, y, z = data.T
    return (x - A + y - B) / 2 + np.sqrt(((x - A - y + B) / 2) ** 2 + C * z ** 2)


# Define some training data
data = np.array([
    [-0.071, -0.85, 0.401],
    [-0.138, -1.111, 0.494],
    [-0.317, -0.317, -0.317],
    [-0.351, -2.048, 0.848]
])
# Define training target
# This is what we want the target function to be equal to
target = 0

# Make an initial guess as to the parameters
# either a constant or random guess is typically fine
num_coeff = 3
coeff_0 = np.ones(num_coeff)
# coeff_0 = np.random.rand(num_coeff)

这不是严格意义上的最小二乘法,但是像这样的东西怎么样? 这个解决方案就像是向问题扔一把大锤。使用SVD解算器可能有一种使用最小二乘法更有效地获得解决方案的方法,但如果您只是在寻找答案,scipy.optimize.minimize将为您找到一个答案

# ===============================================
# FORMULATION #1: a general minimization problem

# Here the bounds and error are all specified within the general objective function
def general_objective(coeff, data, target):
    """
    General function that simply returns a value to be minimized.
    The coeff will be modified to minimize whatever the output of this function
    may be.
    """
    # Constraints to keep coeff above 0
    if np.any(coeff < 0):
        # If any constraint is violated return infinity
        return np.inf
    # The function we care about
    prediction = our_function(coeff, data)
    # (optional) L2 regularization to keep coeff small
    # (optional) reg_amount = 0.0
    # (optional) reg = reg_amount * np.sqrt((coeff ** 2).sum())
    losses = (prediction - target) ** 2
    # (optional) losses += reg
    # Return the average squared error
    loss = losses.sum()
    return loss


general_result = scipy.optimize.minimize(general_objective, coeff_0,
                                         method='Nelder-Mead',
                                         args=(data, target))
# Test what the squared error of the returned result is
coeff = general_result.x
general_output = our_function(coeff, data)
print('====================')
print('general_result =\n%s' % (general_result,))
print('---------------------')
print('general_output = %r' % (general_output,))
print('====================')

我在文档中发现,要使其适应实际的最小二乘法,只需指定计算残差的函数

# ===============================================
# FORMULATION #2: a special least squares problem

# Here all that is needeed is a function that computes the vector of residuals
# the optimization function takes care of the rest
def least_squares_residuals(coeff, data, target):
    """
    Function that returns the vector of residuals between the predicted values
    and the target value. Here we want each predicted value to be close to zero
    """
    A, B, C = coeff
    x, y, z = data.T
    prediction = our_function(coeff, data)
    vector_of_residuals = (prediction - target)
    return vector_of_residuals


# Here the bounds are specified in the optimization call
bound_gt = np.full(shape=num_coeff, fill_value=0, dtype=np.float)
bound_lt = np.full(shape=num_coeff, fill_value=np.inf, dtype=np.float)
bounds = (bound_gt, bound_lt)

lst_sqrs_result = scipy.optimize.least_squares(least_squares_residuals, coeff_0,
                                               args=(data, target), bounds=bounds)
# Test what the squared error of the returned result is
coeff = lst_sqrs_result.x
lst_sqrs_output = our_function(coeff, data)
print('====================')
print('lst_sqrs_result =\n%s' % (lst_sqrs_result,))
print('---------------------')
print('lst_sqrs_output = %r' % (lst_sqrs_output,))
print('====================')
这里的输出是:

====================
lst_sqrs_result =
 active_mask: array([ 0, -1,  0])
        cost: 6.197329866927735e-05
         fun: array([ 0.00518416, -0.00564099, -0.00710112,  0.00385024])
        grad: array([ -4.61826888e-09,   3.70771396e-03,   1.26659198e-09])
         jac: array([[-0.72611025, -0.27388975,  0.13653112],
       [-0.74479565, -0.25520435,  0.1644325 ],
       [-0.35777232, -0.64222767,  0.11601263],
       [-0.77338046, -0.22661953,  0.27104366]])
     message: '`gtol` termination condition is satisfied.'
        nfev: 13
        njev: 13
  optimality: 4.6182688779976278e-09
      status: 1
     success: True
           x: array([  2.46392438e-01,   5.39025298e-17,   1.71555150e+00])
---------------------
lst_sqrs_output = array([ 0.00518416, -0.00564099, -0.00710112,  0.00385024])
====================

你写的代码给了我正确的结果!然而,我真的不明白你是怎么强加边界条件的?哎呀。我忘了做那件事。¯\_(ツ)_/“”。我认为这是可行的,因为我最初的猜测是使用正值,L2正则化鼓励系数收缩,所以当得到一个好的答案时,它就停止了。尽管该方法非常通用,所以可以包括if-np.any(coeff<0):返回np.inf,这将施加约束。啊,好的,这是有意义的。谢谢!我知道了最小二乘法实现的工作原理。您指定的函数需要计算残差。在这种情况下,可以显式指定边界。
====================
lst_sqrs_result =
 active_mask: array([ 0, -1,  0])
        cost: 6.197329866927735e-05
         fun: array([ 0.00518416, -0.00564099, -0.00710112,  0.00385024])
        grad: array([ -4.61826888e-09,   3.70771396e-03,   1.26659198e-09])
         jac: array([[-0.72611025, -0.27388975,  0.13653112],
       [-0.74479565, -0.25520435,  0.1644325 ],
       [-0.35777232, -0.64222767,  0.11601263],
       [-0.77338046, -0.22661953,  0.27104366]])
     message: '`gtol` termination condition is satisfied.'
        nfev: 13
        njev: 13
  optimality: 4.6182688779976278e-09
      status: 1
     success: True
           x: array([  2.46392438e-01,   5.39025298e-17,   1.71555150e+00])
---------------------
lst_sqrs_output = array([ 0.00518416, -0.00564099, -0.00710112,  0.00385024])
====================