LoginSignup
1
1

More than 3 years have passed since last update.

Numpy > 2次元データに対して1次元方向に補間をする > interp1d()にてaxis指定で実施する

Last updated at Posted at 2020-02-10
動作環境
Python 3.6.8 (default, Aug  7 2019, 17:28:10) 
IPython 7.9.0 -- An enhanced Interactive Python. Type '?' for help.
CentOS 6.8

概要

  • WRFという処理で使うOI SSTにおいて、時間補間をしようとしている
  • 720x1440の配列が時間方向に9個あり、その状況で時間方向に補間をする
  • 要素取出ししながらの補間では処理時間がかかりすぎる

方針

上記で紹介されているaxis指定の補間を実装する。

f_out = interp1d(heights, t_in, axis=1)

実装

処理対象のデータと同じ構造のarrayを用意して、それに対して補間を実装した (処理時間の確認用)。

import numpy as np
from scipy.interpolate import interp1d
from time import time

# PEP8 checked

# 1. make test array
print('===DEBUG', time())
xarray = [x * 24 for x in range(-4, 5)]
print(xarray)
target = []
for val in range(9):  # value
    olist = np.array([])
    for oi in range(720):  # oi: outer index
        ilist = []
        for ii in range(1440):  # ii: inner index
            ilist.append(val)
        olist = np.append(olist, np.array(ilist))
    target.append(np.array(olist))
target = np.array(target)

# 2. interpolation long the axis of xarray
print('===DEBUG', time())
f_out = interp1d(xarray, target, axis=0)
print(target)
print(len(target))
print(target.dtype)
new_x = np.array([-50, -40, -30])
t_out = f_out(new_x)
print('===DEBUG', time())

print(t_out)

結果

-50, -40, -30に該当する補間値が求まったようだ。
処理時間は0.09秒程度。

===DEBUG 1581303872.2242842
[-96, -72, -48, -24, 0, 24, 48, 72, 96]
===DEBUG 1581303878.1328526
[[0. 0. 0. ... 0. 0. 0.]
 [1. 1. 1. ... 1. 1. 1.]
 [2. 2. 2. ... 2. 2. 2.]
 ...
 [6. 6. 6. ... 6. 6. 6.]
 [7. 7. 7. ... 7. 7. 7.]
 [8. 8. 8. ... 8. 8. 8.]]
9
float64
===DEBUG 1581303878.2205226
[[1.91666667 1.91666667 1.91666667 ... 1.91666667 1.91666667 1.91666667]
 [2.33333333 2.33333333 2.33333333 ... 2.33333333 2.33333333 2.33333333]
 [2.75       2.75       2.75       ... 2.75       2.75       2.75      ]]

補足

下記の補間も見つけたが、今回の目的には使わない気がする。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1