ndarray の配列データを全件チェックする2通りの方法 any と all の使用法です。
any
any を使えば条件に合致するデータが 「少なくとも 1 件ある」 ことを確認できます。
1 2 3 4 5 6 7 8 9 10 11 12 |
import numpy as np scores = np.array([48, 60, 70, 75, 80]) (scores < 50).any() #-------------------- # True #-------------------- (scores < 40).any() #-------------------- # False #-------------------- |
all
all を使えば 「全部、条件に合致する」 ことを確認できます。
1 2 3 4 5 6 7 8 9 |
(scores < 50).all() #-------------------- # False #-------------------- (scores < 90).all() #-------------------- # True #-------------------- |
匿名 says:
(0 < scores < 50)のような判定時はどのようにすれば良いですか?
このままではValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()とエラーが出ます。