用从多项式回归获得的计算值替换列表中的NP.NAN

发布于 2025-01-19 21:26:57 字数 1883 浏览 0 评论 0原文

我有两个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无悔心 2025-01-26 21:26:57

首先,您应该将 ab 定义修改为:

ab = np.polyfit(x[idx], np.array(y)[idx], idx.sum())

ab 是您的多项式系数,因此您必须将它们传递给 np.polyval 作为:

replace_nan = np.polyval(ab,x)
print(replace_nan)

out:

[   4.           23.           26.54413638   28.01419869   27.00250156
   23.10135965   15.90308758    5.          -10.01558845  -29.55136312
  -54.01500938  -83.81421259 -119.3566581  -161.05003127]

first you should modify the ab definition as:

ab = np.polyfit(x[idx], np.array(y)[idx], idx.sum())

ab are your polynomial coefficients, so you have to pass them to np.polyval as:

replace_nan = np.polyval(ab,x)
print(replace_nan)

out:

[   4.           23.           26.54413638   28.01419869   27.00250156
   23.10135965   15.90308758    5.          -10.01558845  -29.55136312
  -54.01500938  -83.81421259 -119.3566581  -161.05003127]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文