用从多项式回归获得的计算值替换列表中的NP.NAN
我有两个 y 值列表:
y_list1 = [45,np.nan,np.nan,np.nan, 40,50,6,2,7,np.nan, np.nan,np.nan, np.nan, np.nan]
y_list2 = [4,23,np.nan, np.nan, np.nan, np.nan, np.nan,5, np.nan, np.nan, np.nan, np.nan, np.nan]
这两个值都是在一组时间点获得的:
x = np.array([0,3,4,5,6,7,8,9,10,11,12,13,14,15])
目标:通过将多项式回归拟合到那里的数据,返回 y_list1 和 y_list2,并将 np.nans 替换为值,然后计算缺失点。
我能够拟合多项式:
import sys
import numpy as np
x = np.array([0,3,4,5,6,7,8,9,10,11,12,13,14,15])
id_list = ['1','2']
list_y = np.array([[45,np.nan,np.nan,np.nan, 40,50,6,2,7,np.nan, np.nan,np.nan, np.nan, np.nan],[4,23,np.nan, np.nan, np.nan, np.nan, np.nan,5, np.nan, np.nan, np.nan, np.nan, np.nan]]
for each_id,y in zip(id_list,list_y):
#treat the missing data
idx = np.isfinite(x) & np.isfinite(y)
#fit
ab = np.polyfit(x[idx], y[idx], len(list_y[0]))
所以我想用这个拟合来替换 y 中缺失的值,所以我发现 this,并实现:
replace_nan = np.polyval(x,y)
print(replace_nan)
输出是:
[2.13161598e+20 nan nan nan
5.20634185e+19 7.52453405e+20 8.35884417e+09 3.27510000e+04
5.11358666e+10 nan nan nan
nan nan]
test_polyreg.py:16: RankWarning: Polyfit may be poorly conditioned
ab = np.polyfit(x[idx], y[idx], len(list_y[0])) #understand how many degrees
[7.45653990e+07 6.97736286e+16 nan nan
nan nan nan 9.91821285e+08
nan nan nan nan
nan nan]
我不关心条件较差的警告,因为这只是测试数据,试图了解它应该如何工作,但是输出中仍然有 nan (并且没有使用我之前生成的拟合),有人应该如何用多项式回归估计的点替换 y 值中的 nan?
I have two lists of y values:
y_list1 = [45,np.nan,np.nan,np.nan, 40,50,6,2,7,np.nan, np.nan,np.nan, np.nan, np.nan]
y_list2 = [4,23,np.nan, np.nan, np.nan, np.nan, np.nan,5, np.nan, np.nan, np.nan, np.nan, np.nan]
and both of these values were obtained at a set of time points:
x = np.array([0,3,4,5,6,7,8,9,10,11,12,13,14,15])
The aim: Return y_list1 and y_list2 with the np.nans replaced with values, by fitting a polynomial regression to the data that is there, and then calculating the missing points.
I am able to fit the polynomial:
import sys
import numpy as np
x = np.array([0,3,4,5,6,7,8,9,10,11,12,13,14,15])
id_list = ['1','2']
list_y = np.array([[45,np.nan,np.nan,np.nan, 40,50,6,2,7,np.nan, np.nan,np.nan, np.nan, np.nan],[4,23,np.nan, np.nan, np.nan, np.nan, np.nan,5, np.nan, np.nan, np.nan, np.nan, np.nan]]
for each_id,y in zip(id_list,list_y):
#treat the missing data
idx = np.isfinite(x) & np.isfinite(y)
#fit
ab = np.polyfit(x[idx], y[idx], len(list_y[0]))
So then I wanted to use this fit to replace the missing values in y, so I found this, and implemented:
replace_nan = np.polyval(x,y)
print(replace_nan)
The output is:
[2.13161598e+20 nan nan nan
5.20634185e+19 7.52453405e+20 8.35884417e+09 3.27510000e+04
5.11358666e+10 nan nan nan
nan nan]
test_polyreg.py:16: RankWarning: Polyfit may be poorly conditioned
ab = np.polyfit(x[idx], y[idx], len(list_y[0])) #understand how many degrees
[7.45653990e+07 6.97736286e+16 nan nan
nan nan nan 9.91821285e+08
nan nan nan nan
nan nan]
I'm not concerned about the poor conditioning warning because this is just test data to try understand how it should work, but the output still has nans in it (and didn't use the fit I'd previously generated), could someone should be how to replace the nans in the y values with points estimated from a polynomial regression?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您应该将
ab
定义修改为:ab
是您的多项式系数,因此您必须将它们传递给np.polyval
作为:out:
first you should modify the
ab
definition as:ab
are your polynomial coefficients, so you have to pass them tonp.polyval
as:out: