- 2025/05/23
- Category :
[PR]
×
[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。
Python, Python, Python! In the spirit of "import antigravity"
[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。
def interp_nan(x): ''' Replace nan by interporation http://stackoverflow.com/questions/6518811/interpolate-nan-values-in-a-numpy-array ''' ok = -np.isnan(x) xp = ok.nonzero()[0] fp = x[ok] _x = np.isnan(x).nonzero()[0] x[-ok] = np.interp(_x, xp, fp) return x
Site-packagesに入れていつでもインポートできるようにしておくと便利なスニペット。もともとはStackoverflowで見つけたものほぼそのまま。ravelが何で入れてあるのかコメントで突っ込まれていて、確かになくとも動くのでとっておいた。
いつもと違うPCなんかでコーディングするときようの覚書。Pygmentsにもあげておいた。
>>> toy = np.array([np.nan, 1, np.nan, 3, np.nan, 9, np.nan, 2, np.nan]) >>> interp_nan(toy) 0: array([ 1. , 1. , 2. , 3. , 6. , 9. , 5.5, 2. , 2. ])