根据数值和窗口大小从另一个数组构建两个数组

huangapple go评论125阅读模式
英文:

Build two array from another array based on the values and a window size

问题

我有一个包含一千行和列的数组。数组中有大于1和小于1的数字。我想从中构建两个数组,方式如下:

最重要的部分是小于1的值。然后基于一个窗口大小(这里是7),小于1的值之前大于1的值应该变为1,其余的值都为0。例如,如果一行是[1, 1, 1.2, 0.5, 1.9, 1, 1],我想要的第一个数组是:[0, 0, 1, 0, 0, 0, 0],而第二个数组与小于1的值之后大于1的值有关。对于这个例子,我想要 [0, 0, 0, 0, 1, 0, 0]

这里是一个简单的例子:
我有的数组:

a = np.array([[1,1,1.01, 0.5, 0.5, 1.02, 1, 1,1,1.21, 0.5, 0.5, 1.22, 1.3], [1,1.4,1.01, 0.5, 0.5, 1.02, 1, 1,1,1.51, 0.5, 0.7, 1.22, 1]])

我想要的两个数组:

array([[0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
       [0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0]])

array([[0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1],
       [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1]])

请问你需要什么帮助?谢谢!

英文:

I have an array with a thousand rows and columns. The array has the number 1, greater than 1, and less than 1. I want to build two arrays from that with this way:

The most important part is the values which are less than 1. Then based on a window size (here is 7), the value greater than 1 before the values less than 1, should change to 1, and all of the other remaining are zero. For example, if a row is [1, 1, 1.2, 0.5, 1.9, 1, 1], the first array that I want is: [0, 0, 1, 0,0,0,0]
and the second array that I want are related to the values greater than 1 after the values less than 1. For this example, I want [0, 0, 0, 0, 1, 0,0].

Here is a simple exmaple:
array I have:

a = np.array([[1,1,1.01, 0.5, 0.5, 1.02, 1, 1,1,1.21, 0.5, 0.5, 1.22, 1.3], [1,1.4,1.01, 0.5, 0.5, 1.02, 1, 1,1,1.51, 0.5, 0.7, 1.22, 1]])



 a= array([[1.  , 1.  , 1.01, 0.5 , 0.5 , 1.02, 1.  , 1.  , 1.  , 1.21, 0.5 ,0.5 , 1.22, 1.3 ],
           [1.  , 1.4 , 1.01, 0.5 , 0.5 , 1.02, 1.  , 1.  , 1.  , 1.51, 0.5 ,0.7 , 1.22, 1.  ]])

Two array I want:

array([[0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
       [0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0]])

and

array([[0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1],
       [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1]])

Could you please help me with this? Thank you

答案1

得分: 1

你可以使用scipy中的convolve1d来实现这一点:

from scipy.ndimage import convolve1d

def generate_arrays(a):
    # 创建掩模数组
    mask1 = np.zeros_like(a)
    mask2 = np.zeros_like(a)

    # 创建卷积核
    kernel = np.ones(7)

    # 用卷积核对a进行卷积
    convolved = convolve1d(a, kernel, axis=1, mode='constant')

    # 创建小于1的值的掩模
    mask1[a < 1] = convolved[a < 1] < 1
    mask2[a < 1] = 0

    # 创建大于1的值的掩模
    mask1[a > 1] = 0
    mask2[a > 1] = convolved[a > 1] > 1

    return mask2, mask1
英文:

You can achieve this by using the convolve1d from scipy:

from scipy.ndimage import convolve1d

def generate_arrays(a):
    # create mask arrays
    mask1 = np.zeros_like(a)
    mask2 = np.zeros_like(a)

    # create kernel for convolution
    kernel = np.ones(7)

    # convolve kernel with a
    convolved = convolve1d(a, kernel, axis=1, mode=&#39;constant&#39;)

    # create mask for values less than 1
    mask1[a &lt; 1] = convolved[a &lt; 1] &lt; 1
    mask2[a &lt; 1] = 0

    # create mask for values greater than 1
    mask1[a &gt; 1] = 0
    mask2[a &gt; 1] = convolved[a &gt; 1] &gt; 1

    return mask2, mask1

huangapple
  • 本文由 发表于 2023年3月7日 01:32:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75654014.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定