首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Numpy,我如何索引一个数组,以保持比它们后面的前5项和下5项更小的项?

Numpy,我如何索引一个数组,以保持比它们后面的前5项和下5项更小的项?
EN

Stack Overflow用户
提问于 2021-04-13 04:01:55
回答 2查看 43关注 0票数 3

我正在制定一个使用支撑位和阻力位的交易策略。我找到它们的方法之一是搜索maxima's/minima's (价格高于/低于前5个价格和下5个价格)。

我有一个平滑的收盘价数组,我首先尝试用for循环找到它们:

代码语言:javascript
复制
def find_max_min(smoothed_prices) # smoothed_prices = np.array([1.873,...])
    avg_delta = np.diff(smoothed_prices).mean()
    maximas = []
    minimas = []
    for index in range(len(smoothed_prices)):
        if index < 5 or index > len(smoothed_prices) - 6:
            continue

        current_value = smoothed_prices[index]
        previous_points = smoothed_prices[index - 5:index]
        next_points = smoothed_prices [index+1:index+6]

        previous_are_higher = all(x > current_value for x in previous_points)
        next_are_higher = all(x > current_value for x in next_points)

        previous_are_smaller = all(x < current_value for x in previous_points)
        next_are_smaller = all(x < current_value for x in next_points)

        previous_delta_is_enough = abs(previous[0] - current_value) > avg_delta 
        next_delta_is_enough = abs(next_points[-1] - current_value) > avg_delta
        delta_is_enough = previous_delta_is_enough and next_delta_is_enough  

        if previous_are_higher and next_are_higher and delta_is_enough:
            minimas.append(current_value)

        elif previous_are_higher and next_are_higher and delta_is_enough:
            maximas.append(current_value)

        else:
            continue

        return maximas, minimas

(这不是我使用的实际代码,因为我删除了它,这可能不起作用,但它是类似的东西)

所以这段代码可以找到最大值和最小值,但它太慢了,我需要在大型数组上每秒多次使用该函数。

我的问题是:有没有可能像这样用一个麻木面具来做这件事:

代码语言:javascript
复制
smoothed_prices = s
minimas = s[all(x > s[index] for x in s[index-5:index]) and all(x > s[index] for x in s[index+1:index+6])]
maximas = ...

或者你知道我怎样才能以另一种高效麻木的方式去做吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-04-13 06:58:25

我想到了一种方法,它应该比你所呈现的for循环更快,但它使用了更多的内存。简单地说,它创建了一个窗口的中间矩阵,然后它只得到每个窗口的最大值和最小值:

代码语言:javascript
复制
def find_max_min(arr, win_pad_size=5):
    windows = np.zeros((len(arr) - 2 * win_pad_size, 2 * win_pad_size + 1))
    for i in range(2 * win_pad_size + 1):
        windows[:, i] = arr[i:i+windows.shape[0]]
    return windows.max(axis=1), windows.min(axis=1)

编辑:我发现了一种更快的方法来从Split Python sequence into subsequences计算子序列(我称之为窗口)。它没有使用更多的内存,相反,它创建了一个数组的视图。

代码语言:javascript
复制
def subsequences(ts, window):
    shape = (ts.size - window + 1, window)
    strides = ts.strides * 2
    return np.lib.stride_tricks.as_strided(ts, shape=shape, strides=strides)

def find_max_min(arr, win_pad_size=5):
    windows = subsequences(arr, 2 * win_pad_size + 1)
    return windows.max(axis=1), windows.min(axis=1)
票数 1
EN

Stack Overflow用户

发布于 2021-04-13 11:04:42

您可以通过以下方式轻松完成此操作:

代码语言:javascript
复制
from skimage.util import view_as_windows
a = smoothed_prices[4:-5]
a[a == view_as_windows(smoothed_prices, (10)).min(-1)]

请注意,由于您查看的是索引的+/- 5内的最小值,因此它们可以位于数组的索引[4:-5]中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67064957

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档