2017-11-17 2 views
2

私は1日分のデータを見ています。私は、データセットから12時から13時までの時間を削除しようとしていて、残りのデータが与えられている時間を補間しようとしています。どのように配列からデータを削除し、そのギャップを介して補間するPython?

私は2つの問題に実行しています:私はデータを削除しています方法が間違っていると私はそれを修正するかどうかはわからない

  1. 。データを削除しますが、データをもう一度ずらしてギャップがないようにします。これはデータをグラフ化すると明らかですが、ギャップはありません。

  2. interpolate.interp1dまたはinterpolate.splevを使用する必要があるかどうかわかりません。

これは、私が使用している正確なデータはないが、私が行う必要があると私は何をしようとしているかを示すのに良い例です。

import numpy as np 
import matplotlib.pyplot as plt 
from scipy import interpolate 
from matplotlib import pylab 

day_sec=np.arange(0,86400,1) #24 hours in seconds 
ydata=np.exp(-day_sec/30) #random ydata for example 

remove_seconds=np.arange(43200,46800,1) #12-13:00 in seconds 

last=len(remove_seconds)-1 

for i in range(len(day_sec)): #what I have tried to remove the data, but failed 
    if (day_sec[i] >= remove_seconds[0]) and (day_sec[i] <= remove_seconds[last]): 
    new_day_sec=np.delete(day_sec,[i],None) 
    new_ydata=np.delete(ydata,[i],None) 

f=interpolate.interp1d(new_day_sec,new_ydata) #not sure this is correct 

助けてください。ありがとう!

答えて

0

ループを使用せずにこれを実行できます。ここに、「意味のある」yデータの例があります。 (現在ydataの平均値が0である、との期待もゼロである。)

import numpy as np 
from scipy import interpolate 

# Step one - build your data and combine it into one 2d array 
day_sec = np.arange(0, 86400) 
ydata = np.random.randn(day_sec.shape[0]) + np.log(day_sec + 1) 
data = np.column_stack((day_sec, ydata)) 

# Step two - get rid of the slice from 12:13 with a mask 
# Remember your array is 0-indexed 
remove_seconds = np.arange(43200, 46800) 
mask = np.ones(data.shape[0], dtype=bool) 
# Mask is now False within `remove_seconds` and True everywhere else. 
# This allows us to filter (boolean mask) `data`. 
mask[remove_seconds] = False 
newdata = data[mask, :] 

# Step three - interpolate 
# `f` here is just a function, not an array. You need to input 
#  your "dropped" range of seconds to produce predicted values. 
f = interpolate.interp1d(newdata[:, 0], newdata[:, 1]) 
predicted = f(remove_seconds) 

# If you want to insert the interpolated data 
data[~mask, 1] = predicted 

plt.plot(data[:, 0], data[:, 1]) 

enter image description here

+0

これは非常に便利です!ですから、以前のデータに対して補間されたデータをプロットしたいのであれば、ギャップが存在する 'データ'配列に '予測された'値を追加しますか? – Willow

+0

私の答えを更新しました。これを行うにはいくつかの方法がありますが、元のデータをそのまま維持し、補間する新しい配列を作成し、補間された値を元のデータに戻します。 –

+0

ここでの予測は素晴らしいものではありません。なぜなら、かなりノイズの多いデータに対して1d補間を使用しているからです。 (これは関数に従いますが、ドリフト項を持ちます)。 –

関連する問題