如何在numpy数组中找到连续的正数、负数和零?

3

我使用以下函数来查找连续的负数和正数,现在我还想添加一个条件,以获取连续的零。 我该怎么做?

def consecutive_counts(arr):
    '''
    Returns number of consecutive negative and positive numbers
    arr = np.array
    negative = consecutive_counts()[0]
    positive = consecutive_counts()[1]
    '''
    pos = arr > 0
    # is used to Compute indices that are non-zero in the flattened version of arr
    idx = np.flatnonzero(pos[1:] != pos[:-1])
    count = np.concatenate(([idx[0]+1], idx[1:] - idx[:-1], [arr.size-1-idx[-1]]))
    negative = count[1::2], count[::2]
    positive = count[::2], count[1::2]
    if arr[0] < 0:
        return negative
    else:
        return positive

这是一个Pandas序列:
In [221]: n.temp.p['50000']
Out[221]: 
name
0         0.00
1       -92.87
2       -24.01
3       -92.87
4       -92.87
5       -92.87
...       ...

我像这样使用它:

arr = n.temp.p['50000'].values #Will be a numpy array as the input

期望输出:

In [225]: consecutive_counts(a)
Out[225]: (array([30, 29, 11, ...,  2,  1,  3]), array([19,  1,  1, ...,  1,  1,  2]))


感谢您的请求 :)

你能提供一小段输入数据框和期望输出吗? - moys
@SH-SF 当然,给你。我已经做好了:) - daryush shiri
2个回答

3

既然你在这里标记了pandas,这是一种方法:

# random data
np.random.seed(1)
a = np.random.choice(range(-2,3), 1000)

# np.sign: + = 1, 0 = 0, - = -1
b = pd.Series(np.sign(a))

# b.head()
# 0    1
# 1    1
# 2   -1
# 3   -1
# 4    1
# dtype: int32

# sign blocks
blks = b.diff().ne(0).cumsum()

# blks.head()
# 0    1
# 1    1
# 2    2
# 3    2
# 4    3
# dtype: int32

# number of blocks:
blks.iloc[-1]
# 654

# block counts:
blks.value_counts()

# 1      2
# 2      2
# 3      1
# 4      3
# 5      2
# ...

它是如何工作的?我的数组中有288个零,但显示的数量比这还要多。你能解释一下吗? - daryush shiri
我不确定我理解了。每个连续的零块都被视为一个块。但是,该代码不应显示哪个块为零。 - Quang Hoang

2
这里是一种numpy方法:
# create example
arr = np.random.randint(-2,3,(10))

# split into negative, zero, positive
*nzp, = map(np.flatnonzero,(arr<0,arr==0,arr>0))
# find block boundaries
*bb, = (np.flatnonzero(np.diff(x,prepend=-2,append=-2)-1) for x in nzp)
# compute block sizes
*bs, = map(np.diff,bb)

# show all
for data in (arr,nzp,bb,bs): print(data)
# [-1  1 -1  1  0  0  2 -1 -2  1]
# [array([0, 2, 7, 8]), array([4, 5]), array([1, 3, 6, 9])]
# [array([0, 1, 2, 4]), array([0, 2]), array([0, 1, 2, 3, 4])]
# [array([1, 1, 2]), array([2]), array([1, 1, 1, 1])]

看起来是个不错的方法,但我遇到了以下错误:TypeError: diff() got an unexpected keyword argument 'prepend' - daryush shiri
1
@daryushshiri 这是API的一个相对较新的添加。您可以使用np.diff(np.concatenate([[a],x,[b]]))来解决它,而不是使用np.diff(x,prepend=a,append=b) - Paul Panzer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接