在scipy优化curve_fit中可能存在元组解包不平衡的情况。

huangapple go评论65阅读模式
英文:

Possible unbalanced tuple unpacking with sequence in scipy optimization curve_fit

问题

我从我的代码中得到了pylint错误。 我不知道如何修复它。 你能帮我吗?

下面是代码:

import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit

# 定义两个点
x1, y1 = 4, 0
x2, y2 = 15, 1

# 定义平方根函数
def sqrt_func(x, a, b):
    return a * np.sqrt(x - x1) + y1

# 对这两个点进行曲线拟合
popt, pcov = curve_fit(sqrt_func, [x1, x2], [y1, y2])

# 生成4到15之间的中间x值
x_values = np.linspace(4, 15, num=100)

# 使用拟合的曲线计算y值
y_values = sqrt_func(x_values, *popt)

# 绘制曲线和这两个点
plt.plot(x_values, y_values)
plt.scatter([x1, x2], [y1, y2])
plt.show()

在下面这行代码中,我遇到了错误:** 在scipy.optimize._minpack_py的第885行定义的序列可能出现不平衡的元组解包:左侧有2个标签,右侧有5个值 **

popt, pcov = curve_fit(sqrt_func, [x1, x2], [y1, y2])
英文:

I got error from pylint from my code. I don't know how to fix that. can you please help me?

The code is here:

import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit

# Define the two points
x1, y1 = 4, 0
x2, y2 = 15, 1

# Define the square root function
def sqrt_func(x, a, b):
    return a * np.sqrt(x - x1) + y1

# Fit the curve to the two points
popt, pcov = curve_fit(sqrt_func, [x1, x2], [y1, y2])

# Generate intermediate x values between 4 and 15
x_values = np.linspace(4, 15, num=100)

# Use the fitted curve to calculate y values
y_values = sqrt_func(x_values, *popt)

# Plot the curve and the two points
plt.plot(x_values, y_values)
plt.scatter([x1, x2], [y1, y2])
plt.show()

in this bellow line I have this error: ** Possible unbalanced tuple unpacking with sequence defined at line 885 of scipy.optimize._minpack_py: left side has 2 labels, right side has 5 values **

popt, pcov = curve_fit(sqrt_func, [x1, x2], [y1, y2])

答案1

得分: 2

以下是要翻译的部分:

"Here's the relevant source code part:


def curve_fit(f, xdata, ydata, p0=None, sigma=None, absolute_sigma=False,
              check_finite=None, bounds=(-np.inf, np.inf), method=None,
              jac=None, *, full_output=False, nan_policy=None,
              **kwargs):
    ... # a lot of code here
    
    if full_output:
        return popt, pcov, infodict, errmsg, ier
    else:
        return popt, pcov

pylint analyses the body and understands that curve_fit can return a 2-tuple or 5-tuple, but fails to infer the relationship with full_output input parameter. We're more capable then pylint and can read the definition to find out that the return type is always 2-tuple in your case, and so pylint gave a false positive. You can add a comment to explain the ignorance reason and to suppress the error message, like this:

# with full_output = False, always returns a 2-tuple
# pylint: disable-next=unbalanced-tuple-unpacking
popt, pcov = curve_fit(sqrt_func, [x1, x2], [y1, y2])  

Note that I'm using pylint: disable-next, see this question for reasons to prefer it to pylint: ignore in many cases."

英文:

Here's the relevant source code part:


def curve_fit(f, xdata, ydata, p0=None, sigma=None, absolute_sigma=False,
              check_finite=None, bounds=(-np.inf, np.inf), method=None,
              jac=None, *, full_output=False, nan_policy=None,
              **kwargs):
    ... # a lot of code here
    
    if full_output:
        return popt, pcov, infodict, errmsg, ier
    else:
        return popt, pcov

pylint analyses the body and understands that curve_fit can return a 2-tuple or 5-tuple, but fails to infer the relationship with full_output input parameter. We're more capable then pylint and can read the definition to find out that the return type is always 2-tuple in your case, and so pylint gave a false positive. You can add a comment to explain the ignorance reason and to suppress the error message, like this:

# with full_output = False, always returns a 2-tuple
# pylint: disable-next=unbalanced-tuple-unpacking
popt, pcov = curve_fit(sqrt_func, [x1, x2], [y1, y2])  

Note that I'm using pylint: disable-next, see this question for reasons to prefer it to pylint: ignore in many cases.

答案2

得分: 0

因为curve_fit函数返回一个包含两个元素的元组,但您试图将其解包为两个变量poptpcov

您可以修改这行代码,只解包元组的第一个元素,即popt,并使用下划线 _ 作为占位符变量来忽略第二个元素pcov

popt, _ = curve_fit(sqrt_func, [x1, x2], [y1, y2])

或者

# pylint: disable=unpacking-non-sequence
popt, pcov = curve_fit(sqrt_func, [x1, x2], [y1, y2])

这告诉pylint忽略“Possible unbalanced tuple unpacking”错误。

英文:

because the curve_fit function returns a tuple of two elements, but you are trying to unpack it into two variables popt and pcov.

You can modify the line to just unpack the first element of the tuple, which is popt, and ignore the second element pcov by using the underscore _ as a placeholder variable:

popt, _ = curve_fit(sqrt_func, [x1, x2], [y1, y2])

Or

# pylint: disable=unpacking-non-sequence
popt, pcov = curve_fit(sqrt_func, [x1, x2], [y1, y2])

This tells pylint to ignore the "Possible unbalanced tuple unpacking" error

huangapple
  • 本文由 发表于 2023年3月4日 07:35:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75632706.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定