如何在 Python 中的数据集中 extrapolate/interpolate

How to extrapolate/interpolate within a dataset in Python

假设我有下面的数据集(这是来自财政部网站的每日国债收益率的缩减版本)。我在那里人为地创建了一些零来说明我的问题。每行对应一个给定的日期。

问题 - 假设我想在每一行中做对数或二次extrapolation/interpolation。有没有一种快速的方法可以做到这一点,还是必须遍历每一行来填写它?

    x = np.array([[ 1.31,  1.44,  0, 2.51,  0,  0],
                  [ 0,  1.45,  1.63, 2.48,  0,  2.69],
                  [ 1.31,  1.43,  1.59, 2.48,  2.55,  2.71]])

这是使用均值插值的基本方法。当然你可以应用更正式的计算,但这是一个开始

import numpy as np

# Interpolate using the mean of each row

# Your original data
x = np.array([[ 1.31,  1.44,  0, 2.51,  0,  0],
              [ 0,  1.45,  1.63, 2.48,  0,  2.69],
              [ 1.31,  1.43,  1.59, 2.48,  2.55,  2.71]])

print(x)

print()

# for each row in x, set the zero values to the mean of the non zero values
for row in x:
    row[row == 0] = np.mean(row[np.nonzero(row)])

print(x)

输出如下:

[[ 1.31  1.44  0.    2.51  0.    0.  ]
 [ 0.    1.45  1.63  2.48  0.    2.69]
 [ 1.31  1.43  1.59  2.48  2.55  2.71]]

[[ 1.31        1.44        1.75333333  2.51        1.75333333  1.75333333]
 [ 2.0625      1.45        1.63        2.48        2.0625      2.69      ]
 [ 1.31        1.43        1.59        2.48        2.55        2.71      ]]