简体   繁体   English

Scipy ValueError:对象不足,无法使用optimize.leastsq进行所需的数组

[英]Scipy ValueError: object too deep for desired array with optimize.leastsq

I am trying to fit my 3D data with linear 3D function Z = a x+b y+c. 我正在尝试使用线性3D函数Z = a x + b y + c来拟合我的3D数据。 I import the data with pandas: 我使用pandas导入数据:

dataframe = pd.read_csv('3d_data.csv',names=['x','y','z'],header=0)

print(dataframe)

            x          y          z
0   52.830740   7.812507   0.000000
1   44.647931  61.031381   8.827942
2   38.725318   0.707952  52.857968
3    0.000000  31.026271  17.743218
4   57.137854  51.291656  61.546131
5   46.341341   3.394429  26.462564
6    3.440893  46.333864  70.440650

I have done some digging and found that the best way to fit 3D data it is to use optimize from scipy with the model equation and residual function: 我进行了一些挖掘,发现适合3D数据的最佳方法是使用带有模型方程式和残差函数的scipy优化:

def model_calc(parameter, x, y):
    a, b, c = parameter
    return a*x + b*y + c

def residual(parameter, data, x, y):
    res = []
    for _x in x:
        for _y in y:
            res.append(data-model_calc(parameter,x,y))
    return res

I fit the data with: 我将数据拟合为:

params0 = [0.1, -0.2,1.]
result = scipy.optimize.leastsq(residual,params0,(dataframe['z'],dataframe['x'],dataframe['y']))
fittedParams = result[0]

But the result is a ValueError: 但是结果是ValueError:

ValueError: object too deep for desired array [...]
minpack.error: Result from function call is not a proper array of floats.

I was trying to minimize the residual function to give only single value or single np.array but it didn't help. 我试图最小化残差函数以仅给出单个值或单个np.array,但这没有帮助。 I don't know where is the problem and if maybe the search space for parameters it is not too complex. 我不知道问题出在哪里,如果参数的搜索空间不太复杂。 I would be very grateful for some hints! 我将非常感谢您提供一些提示!

If you are fitting parameters to a function, you can use curve_fit . 如果要为函数拟合参数,则可以使用curve_fit Here's an implementation: 这是一个实现:

from scipy.optimize import curve_fit

def model_calc(X, a, b, c):
    x, y = X
    return a*x + b*y + c

p0 = [0.1, -0.2, 1.]
popt, pcov = curve_fit(model_calc, (dataframe.x, dataframe.y), dataframe.z, p0)  #popt is the fit, pcov is the covariance matrix (see the docs)

Note that your sintax must be if the form f(X, a, b, c), where X can be a 2D vector (See this post ). 请注意,sintax必须为f(X,a,b,c)形式,其中X可以是2D向量(请参阅此文章 )。

(Another approach) (另一种方法)

If you know your fit is going to be linear, you can use numpy.linalg.lstsq . 如果您知道拟合度将是线性的,则可以使用numpy.linalg.lstsq See here . 这里 Example solution: 解决方案示例:

import numpy as np
from numpy.linalg import lstsq
A = np.vstack((dataframe.x, dataframe.y, np.ones_like(dataframe.y))).T
B = dataframe.z
a, b, c = lstsq(A, B)[0]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ValueError:对象太深,无法在optimize.curve_fit中找到所需的数组 - ValueError: object too deep for desired array in optimize.curve_fit ValueError:'对象太深,不适合所需的数组' - ValueError: 'object too deep for desired array' ValueError:对象对于所需数组而言太深 - ValueError: object too deep for desired array Scipy:optimize.fmin 和 optimize.leastsq 之间的区别 - Scipy: difference between optimize.fmin and optimize.leastsq Scipy:使用optimize.leastsq时拟合参数的界限 - Scipy: bounds for fitting parameter(s) when using optimize.leastsq scipy.optimize.curve_fit:对于所需的数组,没有正确的浮点数错误/对象太深 - scipy.optimize.curve_fit: not a proper array of floats error/object too deep for desired array ValueError:对象太深,无法放入所需的数组(netcdf) - ValueError: object too deep for desired array (netcdf) ValueError:当我尝试使用scipy的convolve2d方法时,对象对于所需的数组而言太深 - ValueError: object too deep for desired array, when I try to use scipy's convolve2d method ValueError:使用互相关时对象对于所需数组来说太深了 - ValueError: object too deep for desired array while using cross correlation ValueError:使用卷积时对象对于所需数组来说太深了 - ValueError: object too deep for desired array while using convolution
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM