繁体   English   中英

为什么我的函数会移动我的输入数据?

[英]Why does my function shift my input data?

我写了一个函数,它接受作为输入数组,然后转移所述阵列(使用numpy.roll函数)超过shift为值operations迭代和替换移位值0 但是我遇到了一个问题。 当我绘制旧数组和新数组时,它们都已移动,而不仅仅是新创建的数组。

下面是这个函数的代码。

def modified_roll(inp, shift: int, operations: int):
    """
    @param inp: Input array to perform the modified roll on
    @param shift: Part of the array that gets shifted
    @param operations: Number of operations
    @return: Returns a shifted array, with zeros at the start
    """

    count = 0
    array = array_rolled = inp

    for k in range(operations):
        count += shift
        array = np.roll(array, shift, axis=0)
        array[:count] = 0
        array_rolled += array

    out = array_rolled / operations

    return out

这张图片中看到我的一个结果,我希望左侧的信号没有右侧(移位)信号确实具有的峰值。

也许是因为您引用了同一个数组,请考虑复制它,如下所示:

array = inp[:]
array_rolled = inp[:]

作为@Marcos答案建议我需要使数组的副本inp ,我做到了通过将.copy()函数用于numpy的阵列。

# This code is part of another function I use
    if operations == 0:
        shifted_signal = selected_whitenoise_signal
    else:
        shifted_signal = modified_roll(selected_whitenoise_signal.copy(), \ 
shift, operations)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM