0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

pythonで移動平均計算するならbottleneckライブラリを使うべし

Last updated at Posted at 2021-09-09

時系列データ(株価データ)のある値(終値)の移動平均を計算するのに、どのやり方が早いのかやってみた。

  1. pandas
  2. numpy
  3. bottleneck(中身はnumpyが使われているらしい)

PCスペック ryzen3600

結論. bottleneck

import pandas as pd
import numpy as np
import bottleneck as bn

# 読み込み
path = 'データ.csv'
df = pd.read_csv(path)

# 終値
close = df['Close']

# pandas
%time ma_close = close.rolling(25).mean()

# numpy
def moving_average(a, n) :
    ret = np.cumsum(a, dtype=float)
    ret[n:] = ret[n:] - ret[:-n]
    return ret[n - 1:] / n
%time ma_close = moving_average(close.to_numpy(), 25)

# bottleneck
def rollavg_bottlneck(a,n):
    return bn.move_mean(a, window=n,min_count = None)
%time ma_close = rollavg_bottlneck(close.to_numpy(), 25) 


%time
CPU times: user 332 µs, sys: 0 ns, total: 332 µs Wall time: 280 µs
CPU times: user 130 µs, sys: 0 ns, total: 130 µs Wall time: 100 µs
CPU times: user 42 µs, sys: 0 ns, total: 42 µs Wall time: 45.5 µs

0
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?