Trong bài viết này, chúng tôi sẽ thảo luận về cách tìm chỉ mục của một giá trị trong một mảng numpy [cả 1D & 2D] bằng Numpy.where []. Hãy để tạo ra một mảng numpy từ một danh sách các số, tức là. Bây giờ, hãy để Lừa xem cách tìm kiếm các yếu tố trong mảng numpy này. Trong phần tử mảng numpy ở trên với giá trị 15 xảy ra ở các nơi khác nhau, hãy để tìm tất cả các chỉ số của nó, tức là.15 occurs at different
places let’s find all it’s indices i.e. Đầu ra: Kết quả là một tuple của các mảng [một cho mỗi trục] chứa các chỉ số trong đó giá trị 15 tồn tại trong mảng mảng, tức là. is a tuple of arrays [one for each axis] containing the indices where value 15 exists in array arr i.e. Vì mảng của chúng tôi là một mảng 1D phẳng, vì vậy tple được trả về sẽ chỉ chứa một mảng các chỉ số và nội dung & nbsp;arr is a flat 1D array, so returned tuple will contain only one array of indices and contents of the returned array result[0] are, Nhận chỉ mục đầu tiên của phần tử có giá trị 15,import numpy as np
# Create a numpy array from a list of numbers
arr = np.array[[11, 12, 13, 14, 15, 16, 17, 15, 11, 12, 14, 15, 16, 17]]
# Get the index of elements with value 15
result = np.where[arr == 15]
print['Tuple of arrays returned : ', result]
print["Elements with value 15 exists at following indices", result[0], sep='\n']
Tuple of arrays returned : [array[[ 4, 7, 11], dtype=int32],]
Elements with value 15 exists at following indices
[ 4 7 11]
[array[[ 4, 7, 11], dtype=int32],]
[ 4 7 11]
result[0][0]
Nó đã hoạt động như thế nào?
numpy.where [] chấp nhận một điều kiện và 2 mảng tùy chọn, tức là.
numpy.where[condition[, x, y]]
Nếu chỉ đối số điều kiện được đưa ra thì nó sẽ trả về các chỉ số của các yếu tố đúng trong mảng bool numpy được trả về theo điều kiện. & Nbsp; ví dụ như điều kiện sau, điều kiện,
boolArr = [arr == 15]
Trả về một mảng bool numpy boolarr, chứa đúng của mỗi phần tử bằng 15, đối với các phần tử khác, nó chứa sai, tức là.boolArr, containing TRUE of each element that is equal to 15, for other elements it contains False i.e.
[False False False False True False False True False False False True False False]
Bây giờ nếu bạn sẽ vượt qua mảng bool này cho numpy.where []
result = numpy.where[boolArr]
Sau đó, nó sẽ trả về một tuple của các mảng [một cho mỗi trục] chứa các chỉ số trong đó giá trị là đúng trong mảng bool numpy đã cho, tức là.
[ 4 7 11]
Quảng cáo
Nếu phần tử không tìm thấy trong mảng numpy
Nếu phần tử đã cho không tồn tại trong mảng numpy thì sẽ trả về mảng các chỉ số sẽ trống, tức là.
# Get the index of elements with value 15 result = np.where[arr == 15] print['Tuple of arrays returned : ', result] print["Elements with value 15 exists at following indices", result[0], sep='\n']1
Đầu ra:
# Get the index of elements with value 15 result = np.where[arr == 15] print['Tuple of arrays returned : ', result] print["Elements with value 15 exists at following indices", result[0], sep='\n']2
Kết quả là một tuple của các mảng [một cho mỗi trục] chứa các chỉ số trong đó giá trị 15 tồn tại trong mảng mảng, tức là.
Vì mảng của chúng tôi là một mảng 1D phẳng, vì vậy tple được trả về sẽ chỉ chứa một mảng các chỉ số và nội dung & nbsp;
# Get the index of elements with value 15 result = np.where[arr == 15] print['Tuple of arrays returned : ', result] print["Elements with value 15 exists at following indices", result[0], sep='\n']3
Nhận chỉ mục đầu tiên của phần tử có giá trị 15,
# Get the index of elements with value 15 result = np.where[arr == 15] print['Tuple of arrays returned : ', result] print["Elements with value 15 exists at following indices", result[0], sep='\n']4
Nó đã hoạt động như thế nào?value 15 in this 2D numpy array i.e.
# Get the index of elements with value 15 result = np.where[arr == 15] print['Tuple of arrays returned : ', result] print["Elements with value 15 exists at following indices", result[0], sep='\n']5
Đầu ra:
# Get the index of elements with value 15 result = np.where[arr == 15] print['Tuple of arrays returned : ', result] print["Elements with value 15 exists at following indices", result[0], sep='\n']6
Kết quả là một tuple của các mảng [một cho mỗi trục] chứa các chỉ số trong đó giá trị 15 tồn tại trong mảng mảng, tức là.
Vì mảng của chúng tôi là một mảng 1D phẳng, vì vậy tple được trả về sẽ chỉ chứa một mảng các chỉ số và nội dung & nbsp;array 1 represents the row indices where this value is found i.e.
# Get the index of elements with value 15 result = np.where[arr == 15] print['Tuple of arrays returned : ', result] print["Elements with value 15 exists at following indices", result[0], sep='\n']7
Nhận chỉ mục đầu tiên của phần tử có giá trị 15,array 2 represents the column indices where this value is found i.e.
# Get the index of elements with value 15 result = np.where[arr == 15] print['Tuple of arrays returned : ', result] print["Elements with value 15 exists at following indices", result[0], sep='\n']8
Nó đã hoạt động như thế nào?
# Get the index of elements with value 15 result = np.where[arr == 15] print['Tuple of arrays returned : ', result] print["Elements with value 15 exists at following indices", result[0], sep='\n']9
numpy.where [] chấp nhận một điều kiện và 2 mảng tùy chọn, tức là.
Tuple of arrays returned : [array[[ 4, 7, 11], dtype=int32],] Elements with value 15 exists at following indices [ 4 7 11]0
Nếu chỉ đối số điều kiện được đưa ra thì nó sẽ trả về các chỉ số của các yếu tố đúng trong mảng bool numpy được trả về theo điều kiện. & Nbsp; ví dụ như điều kiện sau, điều kiện,
Tuple of arrays returned : [array[[ 4, 7, 11], dtype=int32],] Elements with value 15 exists at following indices [ 4 7 11]1
Trả về một mảng bool numpy boolarr, chứa đúng của mỗi phần tử bằng 15, đối với các phần tử khác, nó chứa sai, tức là.
Bây giờ nếu bạn sẽ vượt qua mảng bool này cho numpy.where []
For
example, get the indices of elements with value less than 16 and greater than 12 i.e.
Tuple of arrays returned : [array[[ 4, 7, 11], dtype=int32],] Elements with value 15 exists at following indices [ 4 7 11]2
Đầu ra:
Tuple of arrays returned : [array[[ 4, 7, 11], dtype=int32],] Elements with value 15 exists at following indices [ 4 7 11]3
Kết quả là một tuple của các mảng [một cho mỗi trục] chứa các chỉ số trong đó giá trị 15 tồn tại trong mảng mảng, tức là.
Tuple of arrays returned : [array[[ 4, 7, 11], dtype=int32],] Elements with value 15 exists at following indices [ 4 7 11]4
Vì mảng của chúng tôi là một mảng 1D phẳng, vì vậy tple được trả về sẽ chỉ chứa một mảng các chỉ số và nội dung & nbsp;
Tuple of arrays returned : [array[[ 4, 7, 11], dtype=int32],] Elements with value 15 exists at following indices [ 4 7 11]5
Nhận chỉ mục đầu tiên của phần tử có giá trị 15,
Tuple of arrays returned : [array[[ 4, 7, 11], dtype=int32],] Elements with value 15 exists at following indices [ 4 7 11]6
Output:
Nó đã hoạt động như thế nào?