基本上,我正在编写一个峰值发现函数,该函数必须能够在基准测试中击败scipy.argrelextrema。这是我正在使用的数据和代码的链接:

https://drive.google.com/open?id=1U-_xQRWPoyUXhQUhFgnM3ByGw-1VImKB

如果此链接过期,则可以在dukascopy银行的在线历史数据下载器中找到数据。

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

data = pd.read_csv('EUR_USD.csv')
data.columns = ['Date', 'open', 'high', 'low', 'close','volume']

data.Date = pd.to_datetime(data.Date, format='%d.%m.%Y %H:%M:%S.%f')

data = data.set_index(data.Date)

data = data[['open', 'high', 'low', 'close']]

data = data.drop_duplicates(keep=False)

price = data.close.values

def fft_detect(price, p=0.4):

    trans = np.fft.rfft(price)
    trans[round(p*len(trans)):] = 0
    inv = np.fft.irfft(trans)
    dy = np.gradient(inv)
    peaks_idx = np.where(np.diff(np.sign(dy)) == -2)[0] + 1
    valleys_idx = np.where(np.diff(np.sign(dy)) == 2)[0] + 1

    patt_idx = list(peaks_idx) + list(valleys_idx)
    patt_idx.sort()

    label = [x for x in np.diff(np.sign(dy)) if x != 0]

    # Look for Better Peaks

    l = 2

    new_inds = []

    for i in range(0,len(patt_idx[:-1])):

        search = np.arange(patt_idx[i]-(l+1),patt_idx[i]+(l+1))

        if label[i] == -2:
            idx = price[search].argmax()
        elif label[i] == 2:
            idx = price[search].argmin()

        new_max = search[idx]
        new_inds.append(new_max)

    plt.plot(price)
    plt.plot(inv)
    plt.scatter(patt_idx,price[patt_idx])
    plt.scatter(new_inds,price[new_inds],c='g')
    plt.show()

    return peaks_idx, price[peaks_idx]

它基本上使用快速傅立叶变换(FFT)平滑数据,然后使用导数查找平滑数据的最小和最大索引,然后在未平滑数据上找到相应的峰值。有时,由于某些平滑效果,它发现的峰不清晰,因此我运行此for循环来搜索l指定的边界之间的每个索引的较高或较低的点。我需要矢量化此for循环的帮助!我不知道该怎么做。没有for循环,我的代码比scipy.argrelextrema快约50%,但是for循环使它变慢。因此,如果我能找到一种向量化的方法,它将是scipy.argrelextrema的非常快速,非常有效的替代方法。这两个图像分别表示不使用for循环和使用ojit_code循环的数据。

python - 如何在Python中将此峰发现进行矢量化处理?-LMLPHP
python - 如何在Python中将此峰发现进行矢量化处理?-LMLPHP

最佳答案

这是一个替代方案...它使用列表理解,通常比for循环要快

l = 2

# Define the bounds beforehand, its marginally faster than doing it in the loop
upper = np.array(patt_idx) + l + 1
lower = np.array(patt_idx) - l - 1

# List comprehension...
new_inds = [price[low:hi].argmax() + low if lab == -2 else
            price[low:hi].argmin() + low
            for low, hi, lab in zip(lower, upper, label)]

# Find maximum within each interval
new_max = price[new_inds]
new_global_max = np.max(new_max)

关于python - 如何在Python中将此峰发现进行矢量化处理?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52029864/

10-12 18:42