【发布时间】:2016-05-12 11:40:05
【问题描述】:
我无法做出正确的回归线。我的 a1 值应该是正的,但它是负的。如果我跳过掩码部分,那么我的 a1、b1、c1 值将变为 NaN。
kwargs = dict(delimiter = '\t',\
skip_header = 0,\
missing_values = 'NaN',\
converters = {0:matplotlib.dates.strpdate2num('%d-%m-%Y %H:%M')},\
dtype = float,\
names = True,\
)
ratingcats = np.genfromtxt('C:\Users\ker\Documents\Discharge_and_stageheight_Catsop.txt',**kwargs)
dis_rat = ratingcats['discharge'] #change names of collumns
stage_rat = ratingcats['stage']
#create regression line and mask NaN
dis_ratM = np.ma.masked_array(dis_rat,mask=np.isnan(dis_rat)).compressed()
stage_ratM = np.ma.masked_array(stage_rat,mask=np.isnan(dis_rat)).compressed()
a1,b1,c1 = polyfit(dis_ratM, stage_ratM, 2)
discharge_pred = polyval([a1,b1,c1],stage_ratM)
print (a1,b1,c1)
#create scatterplot
matplotlib.pyplot.scatter(stage_rat,dis_rat,color='red',label='Rating curve')
matplotlib.pyplot.plot(stage_ratM,discharge_pred,'r-',label='regression line')
matplotlib.pyplot.show()
【问题讨论】:
-
你能提供你的输入数据进行测试吗?
-
到底是什么问题? polyfit 曲线是否不合适? NaN 最有可能发生,因为您将 missing_values 视为 NaN。
-
@HarryPotfleur 是的,我可以,但是很大,我如何为您提供数据?
-
@ToineKerckhoffs 我认为情节的图片在这种情况下会很有帮助。
-
@RobinSpiess i.stack.imgur.com/SPL7C.png
标签: python numpy matplotlib regression