Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to get the first non-zero element index of numpy

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "how to get the first non-zero-element index of numpy", which is easy to understand and clear. I hope it can help you solve your doubts. Let me lead you to study and learn "how to get the first non-zero-element index of numpy".

One of the scenarios in which numpy is used is to get the first non-zero element of each row of a two-dimensional matrix. Numpy also provides a way for users to use.

Let's just look at the code ~ import numpy as nparr=np.array ([0Jing 0Jing 2 Jing 3 Jing 3 Jing 4]) print ((arronomy0) .argmax (axis=0)) # 2

Add: python gets the first non-zero element of each row of a two-dimensional matrix

Directly on the code ~ "" core function: ArrayImplement1Dexamples 0: returns a True/False sequence array.argmax (axis=0): returns the index of the largest element along the axis axis, when there are multiple equal maximum values. Return the index of the first maximum value "" import numpy as np array_1D = np.array ([0meme1je 0jry0memlj0jue]) array_2D = np.array ([[0mem1je 0meme], [0,0mai 1meme 0,1], [0meme1,-1meme0,1], [4,0memlame 0,1], [7,16,- 1flint 0] 1]]) def get_first_non_zero_1D (array_1D): first_non_zero = array_1D [(arrayweights 1Dexamples 0) .argmax (axis=0)] return first_non_zero "" Note: the following three functions are completely equivalent Personally, I prefer the last get_first_non_zeros_2D_2 because it looks more comfortable and can be easily extended to more dimensions. "" Def get_first_non_zeros_2D (array_2D): first_non_zeros = np.array ([get_first_non_zero_1D (array_ 2D [I]) for i in range (array_2D.shape [0])]) return first_non_zeros def get_first_non_zeros_2D_1 (array_2D): first_non_zeros = [] for i in range (array_2D.shape [0]): arr = array_ 2D [I :] first_non_zero = arr [(arrystagen0) .argmax (axis=0)] first_non_zeros.append (first_non_zero) return np.array (first_non_zeros) def get_first_non_zeros_2D_2 (array_2D): none_zero_index = (array2Drubly0) .argmax (axis=1) # first_non_zeros = np.array ([array_ 2D [I]) None_zero_ index [I] for i in range (array_2D.shape [0])]) first_non_zeros = array_ 2D [range (array_2D.shape [0])) None_zero_index] return first_non_zeros b = get_first_non_zeros_2D (array_2D) c = get_first_non_zeros_2D_1 (array_2D) d = get_first_non_zeros_2D_2 (array_2D) print (b) print (c) print (d) "" print result: [1-1 1 1 4 7] [1-1 1 4 7] [1-1 1 4 7] ""

Add: [Python] np.nonzero (ndarray) returns the index of elements in the array that are not zero

Syntax:

The return value of the function is of tuple tuple type, and the number of elements in tuple is the same as the ndarray dimension.

Np.nonzero (ndarray)

First of all, the index of the array starts at 0.

Example: # 1 dimensional array a = [0Power2p3] b = np.nonzero (a) print (b) # (array ([1,2], dtype=int64),) # description: the value of the element at the position of index 1 and index 2 is non-zero. # 2 dimensional array a = np.array ([[0jue 0je 3], [0je 0je 0], [0je 0je 9]]) b = np.nonzero (a) print (b) # (array ([0,2], dtype=int64), array ([2,2], dtype=int64)) # indicates that each element of # tuple determines the position of non-zero elements from one dimension. So for a two-dimensional array, tuple has two elements. The content of the element of # tuple is the position of the element whose dimension is not 0, and the order is the order in which the array is traversed. # for example, determine the location of the first non-zero element: find the first element of the first element array ([0,2], dtype=int64) in the tuple, which is 0, which means the first non-zero element is in the first row; after #, the first element of the second element array ([2,2], dtype=int64) in the tuple is 2, which means the first non-zero element is in the third column. # 3 Dimension array a = np.array ([0recorder 1], [1score0], [[0score1], [1score0]], [[0meme0], [1meme0]) print (a) # [[0 1] # [1 0]] # [[0 1] # [1 0]] # # [[0 0] # [1 0]] b = np.nonzero (a) print (b) # (array ([0Power0, 1, 1, 2], dtype=int64) Array ([0,1,0,1,1], dtype=int64), array ([1,0,1,0,0], dtype=int64) # indicates that because an is a 3-dimensional array Therefore, the index value array has three one-dimensional arrays, representing layers, rows, and columns, respectively. # the principle of lookup is the same as that of a two-dimensional array, so I won't repeat it. The above is all the contents of the article "how to get the first non-zero element index of numpy". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 233

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report