My script to fit a curve to my data started throwing error (numpy.linalg.LinAlgError: SVD did not converge in Linear Least Squares) after working fine for months. After chasing down what could be causing this it seems that plotting another pandas data frame and setting an xlim will cause it to throw an error, whereas when I do not set the xlim it works fine. The curve plotting function has nothing to do with with the rest of the code as far as I can see but still throws an error depending on the xlim setting preceding it.
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()
I anybody can spot the problem and let me know I would very much appreciate it. Thanks!
Here's the traceback:
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