
72
2
章
NumPy
の基礎
演算子 等価なufunc
== np.equal
!= np.not_equal
< np.less
<= np.less_equal
> np.greater
>= np.greater_equal
算術演算の場合と同様に、これらは任意のサイズおよび形状を持つ配列で動作します。
2
次元の
例を示します。
In[12]: rng = np.random.RandomState(0)
x = rng.randint(10, size=(3, 4))
x
Out[12]: array([[5, 0, 3, 3],
[7, 9, 3, 5],
[2, 4, 7, 6]])
In[13]: x < 6
Out[13]: array([[ True, True, True, True],
[False, False, True,
True],
[ True, True, False, False]], dtype=bool)
いずれの場合も、結果はブール値配列であり、
NumPy
はこれらのブール値の結果を扱うための
簡単なパターンをいくつか提供します。
2.6.3
ブール値配列の操作
ブール値配列を与えると実行できる便利な操作が多数あります。先に作成した
2
次元配列
x
を
使って例を示します。
In[14]: print(x)
[[5 0 3 3]
[7 9 3 5]
[2 4 7 6]]
2.6.3.1
要素のカウント
ブール値配列の
True
エントリの数を数えるには、
np.count_nonzero
を使います。 ...