【问题标题】:Testing the number of consecutive values in Numpy Array [duplicate]测试 Numpy Array 中连续值的数量
【发布时间】:2021-07-14 02:24:09
【问题描述】:

我如何编写代码来计算在a 数组中连续出现了正数、负数和零值的次数。我将如何计算这个?

import numpy as np
a = np.array([  0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.
   0.    0.    0.    0.    0.    0.    0.    0.    0.   -8.    0.    0.
 304.2 -27.8 -15.4   0.    0.  -14.8   0.    6.4  14.4   0.  -10.6  55.8
  23.1   0.   27.9  34.7  62.   23.   41.6  30.7  30.5  34.9  40.9  21.7
  31.3  19.9  32.8  26.2  14.8  18.9  15.2  23.8  21.9 112.7  38.4  34.4])

预期结果

Consecutive Positive results: 22
Consecutive Zero results: 21
Consecutive Negative results: 2

【问题讨论】:

  • 如果可能,我不会尝试使用 for 循环

标签: python arrays numpy sorting


【解决方案1】:
import numpy as np
a = np.array([0., -5., 4., -3., 0., -2.])
x = (a > 0).sum()
y = (a == 0).sum()
z = (a < 0).sum()
print("Consecutive Positive results: "+ str(x))
print("Consecutive Zero results: "+ str(y))
print("Consecutive Negative results: "+ str(z))

【讨论】:

  • 有没有更优化的方法而不使用 for 循环,这就是为什么我开始使用 numpy 的原因。还是谢谢
  • 这里我更新了答案,不再循环
  • 这不会产生发布的预期结果。该问题要求连续结果,而不是全部结果。
【解决方案2】:

您可以为此使用np.diff

a = np.array([0., -5., 4., -3., 0., -2.])
diff = np.diff(a)

print("Consecutive Positive results: ", np.count_nonzero(diff > 0))
print("Consecutive Zero results: ", np.count_nonzero(diff == 0))
print("Consecutive Negative results: ", np.count_nonzero(diff < 0))

输出:

Consecutive Positive results:  2
Consecutive Zero results:  0
Consecutive Negative results:  3

编辑

没有正确阅读问题。这是我的新尝试:

a = np.array([  0. ,   0. ,   0. ,   0. ,   0. ,   0. ,   0. ,   0. ,   0. ,
                0. ,   0. ,   0. ,   0. ,   0. ,   0. ,   0. ,   0. ,   0. ,
                0. ,   0. ,   0. ,  -8. ,   0. ,   0. , 304.2, -27.8, -15.4,
                0. ,   0. , -14.8,   0. ,   6.4,  14.4,   0. , -10.6,  55.8,
               23.1,   0. ,  27.9,  34.7,  62. ,  23. ,  41.6,  30.7,  30.5,
               34.9,  40.9,  21.7,  31.3,  19.9,  32.8,  26.2,  14.8,  18.9,
               15.2,  23.8,  21.9, 112.7,  38.4,  34.4])

sign = np.sign(a) # we only care about the sign

# find runs, cred to https://stackoverflow.com/a/54597990/14923227
def count_consecutive(arr, n):
    # pad a with False at both sides for edge cases when array starts or ends with n
    d = np.diff(np.concatenate(([False], arr == n, [False])).astype(int))
    # subtract indices when value changes from False to True from indices where value changes from True to False
    return np.flatnonzero(d == -1) - np.flatnonzero(d == 1)

print("Consecutive Positive results: ", np.max(count_consecutive(sign, 1)))
print("Consecutive Zero results: ", np.max(count_consecutive(sign, 0)))
print("Consecutive Negative results: ", np.max(count_consecutive(sign, -1)))

输出:

Consecutive Positive results:  22
Consecutive Zero results:  21
Consecutive Negative results:  2

【讨论】:

  • 这不会产生发布的预期结果。该问题要求连续结果,而不是全部。
  • np.diff 将获取连续元素之间的差异。您能否在我的代码 sn-p 中给出数组 a 的预期输出?
  • 预期结果发布在问题中。自己运行(提示,你的代码产生 19、22、18)。
  • 问题中发布的数组未格式化:(
【解决方案3】:

似乎numpy doesn't have a build-in groupby function 会使这更简单一些,但我们可以使用itertools.groupby 代替。这个想法是使用groupby 组合满足某些条件(正、负或零)的连续元素,然后找到这些组中最大的。

>>> a = np.array([
    0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,
    0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,   -8.,    0.,    0.,
  304.2, -27.8, -15.4,   0.,    0.,  -14.8,   0.,    6.4,  14.4,   0.,  -10.6,  55.8,
   23.1,   0.,   27.9,  34.7,  62.,   23.,   41.6,  30.7,  30.5,  34.9,  40.9,  21.7,
   31.3,  19.9,  32.8,  26.2,  14.8,  18.9,  15.2,  23.8,  21.9, 112.7,  38.4,  34.4
])
>>> max(map(len, (list(g) for k, g in itertools.groupby(a>0) if k)))
22
>>> max(map(len, (list(g) for k, g in itertools.groupby(a==0) if k)))
21
>>> max(map(len, (list(g) for k, g in itertools.groupby(a<0) if k)))
2

【讨论】:

    猜你喜欢
    • 2021-09-04
    • 2021-08-12
    • 1970-01-01
    • 2011-05-28
    • 1970-01-01
    • 2015-06-06
    • 1970-01-01
    • 1970-01-01
    • 2020-03-07
    相关资源
    最近更新 更多