python - Numpy where 条件语句沿轴 0

标签 python numpy vector conditional-statements

我有一个 1D 向量 Zc ,其中包含 n 个二维数组元素。我想找到每个等于 np.ones(Zc[i].shape) 的 2D 数组的索引。

a = np.zeros((5,5))
b = np.ones((5,5))*4
c = np.ones((5,5))
d = np.ones((5,5))*2

Zc = np.stack((a,b,c,d))

for i in range(len(Zc)):
    a = np.ones(Zc[i].shape)
    b = Zc[i]
    if np.array_equal(a,b):
        print(i)
    else:
        pass 

返回2。上面的代码有效并返回正确的答案,但我想知道是否有矢量化的方法来实现相同的结果?

最佳答案

脱离 hpaulj 的评论:

>>> allones = (Zc == np.array(np.ones(Zc[i].shape))).all(axis=(1,2))
>>> np.where(allones)[0][0]
2

关于python - Numpy where 条件语句沿轴 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52302458/

相关文章:

python - 在 Python Pandas 中从其他两个系列创建一个元素最小系列

python - 如何获得列表中每个列表的平均值以避免某些值?

java - 在 Vector 中插入元素时出现错误 java.lang.NullPointerException

python - 使用 PIL 从 macbook isight 捕获帧

python - 如何删除Python 3中的空白

python - 无法理解 numpy argpartition 输出

python - 如何在 Python 中用间隔索引数组的多个项目

c++ - 使用 std::vector 作为 std::map 的键在未找到和使用 reserve() 时不返回 end()

c++ - 简单的 C++ vector/指针失误

python - Python 3.6.5 中的 Base58 和 utf-8 编码问题