如果我尝试运行下面的脚本,则会收到错误:LinAlgError:SVD 未在线性最小二乘中收敛
。我在类似的数据集上使用了完全相同的脚本,并且它可以工作。我尝试在数据集中搜索 Python 可能解释为 NaN 的值,但我找不到任何内容。
我的数据集非常大,无法手动检查。 (但我认为我的数据集很好)。我还检查了 stageheight_masked 和 discharge_masked 的长度,但它们是相同的。有谁知道为什么我的脚本中有错误以及我该怎么办?
import numpy as np
import datetime
import matplotlib.dates
import matplotlib.pyplot as plt
from scipy import polyfit, polyval
kwargs = dict(delimiter = '\t',\
skip_header = 0,\
missing_values = 'NaN',\
converters = {0:matplotlib.dates.strpdate2num('%d-%m-%Y %H:%M')},\
dtype = float,\
names = True,\
)
rating_curve_Gillisstraat = np.genfromtxt('G:\Discharge_and_stageheight_Gillisstraat.txt',**kwargs)
discharge = rating_curve_Gillisstraat['discharge'] #change names of columns
stageheight = rating_curve_Gillisstraat['stage'] - 131.258
#mask NaN
discharge_masked = np.ma.masked_array(discharge,mask=np.isnan(discharge)).compressed()
stageheight_masked = np.ma.masked_array(stageheight,mask=np.isnan(discharge)).compressed()
#sort
sort_ind = np.argsort(stageheight_masked)
stageheight_masked = stageheight_masked[sort_ind]
discharge_masked = discharge_masked[sort_ind]
#regression
a1,b1,c1 = polyfit(stageheight_masked, discharge_masked, 2)
discharge_predicted = polyval([a1,b1,c1],stageheight_masked)
print 'regression coefficients'
print (a1,b1,c1)
#create upper and lower uncertainty
upper = discharge_predicted*1.15
lower = discharge_predicted*0.85
#create scatterplot
plt.scatter(stageheight,discharge,color='b',label='Rating curve')
plt.plot(stageheight_masked,discharge_predicted,'r-',label='regression line')
plt.plot(stageheight_masked,upper,'r--',label='15% error')
plt.plot(stageheight_masked,lower,'r--')
plt.axhline(y=1.6,xmin=0,xmax=1,color='black',label='measuring range')
plt.title('Rating curve Catsop')
plt.ylabel('discharge')
plt.ylim(0,2)
plt.xlabel('stageheight[m]')
plt.legend(loc='upper left', title='Legend')
plt.grid(True)
plt.show()
最佳答案
我没有您的数据文件,但几乎总是这样,当您收到该错误时,您的数据中有 NaN 或无穷大。查找使用 pd.notnull 或 np.isfinite 的两个
关于python - LinAlg错误: SVD did not converge in Linear Least Squares when trying polyfit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35581644/