LoginSignup
6
6

More than 5 years have passed since last update.

時系列データの補間

Last updated at Posted at 2018-02-07

時系列データの補間

  • x軸が時系列データを補完したくて,考えてみた。
  • pandasを使って,日付型をジュリアンデイに変換して補間を行った。

インポート

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy import interpolate

テストデータの作成

  • xの時間は2時間毎で,1日分
  • sinカーブ,1日で2周期
x = pd.date_range('2018-02-07', '2018-02-08', freq='2H')
y = np.sin(x.to_julian_date()*2*np.pi*2)
# テストデータのプロット
plt.figure(figsize=(13,5))
plt.xlim(x[0], x[-1])
plt.scatter(x,y)

output_3_1.png

補間データ

x_in = pd.date_range('2018-02-07', '2018-02-08', freq='10min')

補間

  • xはジュリアンデイに変換する
# 線形
f1 = interpolate.interp1d(x.to_julian_date(), y)
y_f1 = f1(x_in.to_julian_date())
#2次スプライン
f2 = interpolate.interp1d(x.to_julian_date(), y, kind="quadratic")
y_f2 = f2(x_in.to_julian_date())

結果表示

plt.figure(figsize=(13,5))
plt.xlim(x[0], x[-1])
plt.scatter(x, y, label='original')
plt.plot(x_in, y_f1, label='linear')
plt.plot(x_in, y_f2, label='quadratic')
plt.legend()

output_10_1.png

6
6
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
6
6