Numpy-如何创建具有行和列索引的布尔数组

Numpy- how to create a boolean array with row and column indices

我正在使用 scipy 中的 argrelextrema 来识别我的数据中的峰值。数据看起来像这样:

现在 argrelextrema 为我找到数据中的峰值并输出两个数组(行和列索引)。

argrelextrema(df.values, np.greater, axis=0,order=1)

现在,根据我的原始数据,我想创建一个布尔数组,其中峰值被标识为 True,其余的被标识为 False.

类似这样(仅插图)。

通常我会用 np.where 实现类似上面的东西,但由于我现在有行和列索引,所以我没有找到如何将其与 np.where 子句一起使用的方法.

此外,如果可能的话,我想要一个矢量化的解决方案。

正如所指出的 argrelextrema returns 相对极值的指数。

使用这些索引 index 直接进入与原始 DataFrame 形状相同的布尔数组:

import numpy as np
import pandas as pd
from scipy.signal import argrelextrema

# for reproducibility 
np.random.seed(42)

# create toy DataFrame
df = pd.DataFrame(data=np.random.random((100, 3)) * 20, columns=["AAPL", "MSFT", "TSLA"])

# extract rows and cols indices using extrema
rows, cols = argrelextrema(df.values, np.greater, axis=0,order=1)

# create boolean numpy array
values = np.zeros_like(df.values, dtype=bool)

# set the values of the extrema to True
values[rows, cols] = True

# convert to DataFrame
result = pd.DataFrame(data=values, columns=df.columns)
print(result)

输出

     AAPL   MSFT   TSLA
0   False  False  False
1   False  False   True
2   False  False  False
3   False   True   True
4   False  False  False
..    ...    ...    ...
95  False  False  False
96   True  False   True
97  False  False  False
98   True   True   True
99  False  False  False

[100 rows x 3 columns]

上面代码的重要部分是这样的:

# create boolean numpy array
values = np.zeros_like(df.values, dtype=bool)

# set the values of the extrema to True
values[rows, cols] = True

作为替代方案,您可以直接从 sparse.csr_matrix:

构建 values 数组
from scipy.sparse import csr_matrix
values = csr_matrix((np.ones(rows.shape), (rows, cols)), shape=df.shape, dtype=bool).toarray()