【NumPy】非ゼロ要素をカウントする関数一覧
NumPy 配列のゼロではない要素のインデクスや数え上げに関連する関数の一覧です。
numpy.nonzero()
numpy.nonzero() は配列の非ゼロ要素のインデクス配列をタプルで返します。
# NUMPY_NONZERO
# In[1]
import numpy as np
# 配列を定義
x = np.array([[5, 0, 1],
[3, 8, 0]])
# 非ゼロ要素のインデクス配列
a = np.nonzero(x)
print(a)
# (array([0, 0, 1, 1], dtype=int32),
# array([0, 2, 0, 1], dtype=int32))
タプルの 1 つめの要素は、非ゼロ要素 5, 3, 8, 1 の行方向のインデクスです。2 つめの要素は非ゼロ要素 5, 1, 3, 8 の列方向のインデクスです。この 2 種類のインデクス配列を組合わせれば、非ゼロ要素を指定することができます。すなわち、
[0, 0], [0, 2], [1, 0], [1, 1]
が非ゼロ要素の 2 次元インデクスとなっています。したがって、numpy.nonzero() で取得したインデクス配列を使えば、もとの配列の非ゼロ要素を抽出することができます。
# In[2]
# xから非ゼロ要素を抽出
print(x[a])
# [5 1 3 8]
numpy.flatnonzero()
numpy.flatnonzero() は受け取った配列をフラットにして、非ゼロ要素のインデクスを返します。
# NUMPY_FLATNONZERO
# In[1]
import numpy as np
# 配列xを定義
x = np.array([[0, 1, 2],
[3, 0, 0],
[4, 0, 5]])
# xをフラットにして非ゼロ要素のインデクス配列を生成
fnz = np.flatnonzero(x)
print(fnz)
# [1 2 3 6 8]
numpy.count_nonzero()
numpy.count_nonzero() は非ゼロ要素を数えます。
# NUMPY_COUNT_NONZERO
# In[1]
import numpy as np
# 配列を定義
x = np.array([[7, 0, 3],
[5, 8, 0],
[0, 0, 2]])
# 非ゼロ要素を数える
c1 = np.count_nonzero(x)
print(c1)
# 5
axis=0 を指定すると列方向に非ゼロ要素をカウントします。
# In[2]
# 列方向に非ゼロ要素を数える
c2 = np.count_nonzero(x, axis=0)
print(c2)
# [2 1 2]
axis=1 を指定すると行方向に非ゼロ要素をカウントします。
# In[3]
# 行方向に非ゼロ要素を数える
c3 = np.count_nonzero(x, axis=1)
print(c3)
# [2 2 1]
下記は誤植と思われますので、ご確認ください。
In[2] プログラムの上の文で、axis=0 を指定すると行方向 → axis=0 を指定すると列方向
In[2] プログラムのコメントで、# 行方向に非ゼロ → # 列方向に非ゼロ
In[3] プログラムの上の文で、axis=1 を指定すると列方向 → axis=1 を指定すると行方向
直しておきました。
ありがとうございます。m(_ _)m
numpy.nonzero: The nonzero function in NumPy is used to obtain the indices of non-zero elements within an array. It returns the indices as a tuple.The nonzero function is commonly used in the following situations:
1. Selecting elements based on conditions: When there is a need to extract elements from an array that satisfy certain conditions, nonzero can be used to obtain the indices of the elements that meet the conditions.
2. Sparse matrix representation: In the case of representing sparse matrices (matrices with many zero elements), nonzero can be used to obtain the indices of non-zero elements. This allows for efficient memory usage when storing the data.
Here is an example of using nonzero:
◆◆◆◆◆◆◆◆◆◆◆◆
import numpy as np
# Array
arr = np.array([0, 5, 0, 3, 0, 0, 7])
# Get indices of non-zero elements
nonzero_indices = np.nonzero(arr)
print(nonzero_indices)
# Output: (array([1, 3, 6]),)
# Get values of non-zero elements
nonzero_values = arr[nonzero_indices]
print(nonzero_values)
# Output: [5 3 7]
◆◆◆◆◆◆◆◆◆◆◆◆
In the above example, the indices of the non-zero elements in the arr array are returned as (array([1, 3, 6]),). Consequently, the values of the non-zero elements [5, 3, 7] are also obtained.By using the nonzero function, it becomes easy to access the positions and values of elements that are not zero within an array.
numpy.flatnonzero: The flatnonzero function in NumPy is used to obtain the indices of non-zero elements within an array as a flattened 1-dimensional array.
The flatnonzero function is commonly used in the following situations:
1. Extraction of non-zero indices: When there is a need to obtain the indices of non-zero elements within an array, flatnonzero can be used to retrieve the indices as a flattened 1-dimensional array.
2. Processing based on non-zero values: If there is a requirement to perform specific operations based on the values of non-zero elements in an array, flatnonzero can be used to obtain the indices of non-zero elements. These indices can then be used to carry out the desired operations.
Here is an example of using flatnonzero:
◆◆◆◆◆◆◆◆◆◆◆◆
import numpy as np
# Array
arr = np.array([0, 5, 0, 3, 0, 0, 7])
# Get indices of non-zero elements
nonzero_indices = np.flatnonzero(arr)
print(nonzero_indices)
# Output: [1 3 6]
# Processing based on non-zero values
nonzero_values = arr[nonzero_indices]
squared_values = nonzero_values ** 2
print(squared_values)
# Output: [25 9 49]
◆◆◆◆◆◆◆◆◆◆◆◆
In the above example, the indices of non-zero elements within the arr array are returned as [1, 3, 6], represented as a flattened 1-dimensional array. Subsequently, the values of non-zero elements are retrieved, and the squared values of those elements are computed. By using the flatnonzero function, it becomes convenient to handle non-zero indices and perform various operations based on them.
numpy.count_nonzero: The count_nonzero function in NumPy is used to count the number of non-zero elements within an array. Specifically, it returns the count of non-zero elements in the array. The count_nonzero function is commonly used in the following situations:
1. Calculation of the number of non-zero elements: When there is a need to determine the count of non-zero elements within an array, count_nonzero can be used to obtain the number of non-zero elements.
2. Counting elements based on conditions: If there is a requirement to count the number of elements that satisfy certain conditions within an array, count_nonzero can be used to retrieve the count of elements that meet the conditions.
Here is an example of using count_nonzero:
◆◆◆◆◆◆◆◆◆◆◆◆
import numpy as np
# Array
arr = np.array([0, 5, 0, 3, 0, 0, 7])
# Count the number of non-zero elements
nonzero_count = np.count_nonzero(arr)
print(nonzero_count)
# Output: 3
◆◆◆◆◆◆◆◆◆◆◆◆
In the above example, the count of non-zero elements within the arr array is returned as 3. By using the count_nonzero function, it becomes easy to obtain the count of non-zero elements within an array. This can be useful in various applications, such as statistical analysis of data or counting elements based on specific conditions.