Reputation: 33
Assume I have a dataset below (This is a cut down version of the daily Treasury yields sourced from the Treasury website). I had artificially created some zeroes in there to illustrate the question I have. Each row corresponds to a given date.
Question - Assume I wanted to do log or quadratic extrapolation/interpolation within each row. Is there a quick way to do it or would one have to iterate through each row to fill it in?
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]])
Upvotes: 0
Views: 148
Reputation: 901
Here's a basic approach using the mean to interpolate the value. Of course you can apply a more formal calculation, but this is a start
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)
Output below:
[[ 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 ]]
Upvotes: 1