首页 > 解决方案 > 为什么 scipy curvefit 在之前在不相关的图上设置 xlim 时会引发错误?

问题描述

在正常工作几个月后,我的用于拟合数据曲线的脚本开始抛出错误(numpy.linalg.LinAlgError:SVD 没有在线性最小二乘法中收敛)。在追查可能导致这种情况的原因之后,似乎绘制另一个熊猫数据框并设置 xlim 会导致它抛出错误,而当我不设置 xlim 时它工作正常。据我所知,曲线绘图功能与其余代码无关,但仍会根据其前面的 xlim 设置引发错误。

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

# Function for curve fit
def variableSlope(x, bottom, top, EC50, hillSlope):

    y = bottom + (x**hillSlope) * (top - bottom) / (x**hillSlope + EC50**hillSlope)
    return(y)

# function that should calculate a regression curve and plot it.
# In my actual function the data for the curve would come from a data frame but for simplicity and to keep it self-
# contained in the function I inserted one example of the data within the function itself
# As far as I can see this function takes no variables from outside and should therefore work no matter what happens before
def plot_fitted():
    fig, ax = plt.subplots()
    p0VS = [1, 4, 3, 1]

    xdata = [0.0, 0.0, 0.0, 0.3, 0.3, 0.3, 1.0, 1.0, 1.0, 3.0, 3.0, 3.0,
             10.0, 10.0, 10.0, 30.0, 30.0, 30.0, 60.0,60.0, 60.0]
    ydata = [1.0, 1.0, 1.0, 0.89, 1.09, 0.92, 1.25, 1.34, 1.23, 1.67, 1.60, 1.45,
             2.28, 2.22, 2.03, 2.82, 2.84,2.69, 2.63, 2.74, 2.58]
    
    # Error is thrown here depended on xlim variable in main process
    popt, pcov = curve_fit(variableSlope, xdata, ydata, p0VS, method='dogbox', maxfev=2000)

    x = np.linspace(0, 60, 100)
    y = variableSlope(x, *popt)
    pylab.plot(x, y, label="currentTreatment", color="k")

# This is an unrelated, random data frame
df = pd.DataFrame({'A': [0, 0.3, 1, 3,10,30, 60],
                   'B': [0, 1.1, 1.4, 1.8,2,2.1,2.2]})


fig, ax = plt.subplots()
# plotting this unrelated data frame with xlim results in the plot_fitted() function throwing an error, deleting
# xlim solves that problem, however I would like to keep the same x axis dimensions for all data I throw at it so the 
# plots have the same dimensions
df.plot(x="A", y="B", ax=ax, ylim=(0,5), xlim=(0,61)) #setting xlim here, results in failure of plot_fitted() function

# I don't understand why this function fails 
plot_fitted()

我任何人都可以发现问题并让我知道我将非常感激。谢谢!

这是回溯:

Traceback (most recent call last):  
  File "<input>", line 1, in <module>  
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.1.1\plugins\python-ce\helpers\pydev\_pydev_bundle\pydev_umd.py",  line 197, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script  
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.1.1\plugins\python-ce\helpers\pydev\_pydev_imps\_pydev_execfile.py",   line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)  
  File "C:/Users/Sulley/Documents/Scripts/DataAnalysis/tester2.py",   line 40, in <module>
plot_fitted()  
  File "C:/Users/Sulley/Documents/Scripts/DataAnalysis/tester2.py",   line 26, in plot_fitted
popt, pcov = curve_fit(variableSlope, xdata, ydata, p0VS, method='dogbox', maxfev=2000)  
  File "C:\Users\Sulley\Documents\Scripts\GlobalVenv\lib\site-packages\scipy\optimize\minpack.py",   line 796, in curve_fit
**kwargs)  
  File "C:\Users\Sulley\Documents\Scripts\GlobalVenv\lib\site-packages\scipy\optimize\_lsq\least_squares.py",   line 928, in least_squares
tr_solver, tr_options, verbose)  
  File "C:\Users\Sulley\Documents\Scripts\GlobalVenv\lib\site-packages\scipy\optimize\_lsq\dogbox.py",   line 222, in dogbox
newton_step = lstsq(J_free, -f, rcond=-1)[0]  
  File "<__array_function__ internals>",   line 6, in lstsq  
  File "C:\Users\Sulley\Documents\Scripts\GlobalVenv\lib\site-packages\numpy\linalg\linalg.py",   line 2259, in lstsq
x, resids, rank, s = gufunc(a, b, rcond, signature=signature, extobj=extobj)  
  File "C:\Users\Sulley\Documents\Scripts\GlobalVenv\lib\site-packages\numpy\linalg\linalg.py",   line 109, in _raise_linalgerror_lstsq
raise LinAlgError("SVD did not converge in Linear Least Squares")
numpy.linalg.LinAlgError: SVD did not converge in Linear Least Squares

标签: pythonpandasscipycurve-fitting

解决方案


推荐阅读