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 use argmax in numpy

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

Share

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

This article will explain in detail how to use argmax in numpy. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

1. Basic introduction

Argmax in numpy is simply an index that returns the maximum value. When np.argmax (axis) is used, the direction axis is often not understood.

In short: here axis allows us to look at a high-dimensional array in terms of rows, columns, or depth.

Code experiment 1. One-dimensional array

In a simple one-dimensional case, np.argmax () directly returns the index of the maximum value. Not specifying axis can be regarded as finding the index of the maximum value after flattening the array.

1.1 、 axis=0

When we specify axis=0, we are actually comparing in the column to find the index of the largest row

Of course, it has no effect on this one-dimensional situation.

1.2 、 axis=1

We reported an error when we specified axis=1. This is because our an is an one-dimensional array and there is no axis=1 axis. It can be seen that when we use np.argmax (), the axis cannot be specified more than the array we need to sort.

2. Two-dimensional array

Not specifying axis is equivalent to flattening a two-dimensional array and directly selecting the index of the maximum value.

2.1 、 axis=0

Specifying axis=0 is the comparison column that returns the maximum value in the row index.

We rewrite an element in b, and the result we expect is [2pm 2pm 1pm 2].

The actual results are consistent with our expectations.

2.2 、 axis=1

The specified axis=0 is the comparison row that returns the maximum value in the column index.

3. Three-dimensional array

A three-dimensional array can be regarded as a picture, and its three dimensions (high, width, channels) represent the height, width and number of channels (depth) of the image. Common color images have three channels, so we take common RGB images as an example to build an array.

Using np.argmax () directly is to flatten the three-dimensional array to find the index of the maximum value.

3.1 、 axis=0

View the data of the three channels of c separately, as shown in the figure

Taking axis=0 for three channels means comparing the maximum index of the row returned by the column respectively.

The return value we expect should be [1jin1,], [1jin1], [1jin1], and the actual result is consistent with our expectation.

3.2 、 axis=1

Taking axis=1 for three channels means comparing the maximum index of the row regression column respectively.

Our expected results are [[2, 2, 2], [2, 2, 2], [2, 2, 2], [2, 2, 2]], and the actual results are consistent with our expectations.

3.3 、 axis=2

Taking axis=2 means that we compare the depth direction (channel direction) of the image, we can think that the three arrays are stacked on top of each other, corresponding to channel0,channel1,channel2, and the index we take the maximum value is to return the channel index where the corresponding pixel pixels are located.

All the pixel values of the channel2 of c are larger than the other two channel. All the returned values should be [2, 2,], [2, 2,], [2, 2,], [2, 2,],], and the actual results are consistent with my expectations.

3.4 、 axis=-1

Axis=-1 looks at the axis the other way around, and axis=-1 and axis=2 are the same for three-dimensional cases.

Other:

For two-dimensional cases, axis=-1 and anxis=1 are the same.

For one-dimensional cases, axis=0 and anxis=-1 are the same.

IV. Reference

Https://blog.csdn.net/weixin_39190382/article/details/105854567

Https://www.cnblogs.com/zhouyang209117/p/6512302.html

PS: supplement

1. For an one-dimensional vector

Import numpy as npa = np.array ([3, 1, 2, 4, 6, 1]) b=np.argmax (a) # take out the index corresponding to the maximum value of the element in a, where the maximum value is 6, the corresponding position index value is 4, (the index value starts from 0 by default) print (b) # 4

two。 For two-dimensional vectors (matrices in the usual sense) a [] []

Import numpy as npa = np.array ([[1, 5, 5, 2], [9, 6, 2, 8], [3, 7, 9, 1]]) b=np.argmax (a, axis=0) # for a two-dimensional matrix, there are two index directions, the first direction is a [0], the first direction is a [0]. By default, the first column search for the maximum value # an is 1, the maximum is 9. Print (b) # [1221] c=np.argmax (a, axis=1) # now searches for the maximum value in the direction of a [0] [1], that is, the maximum value in the row direction, and the first behavior of # a 2, the maximum value is 5 (although there are 2 5s, but take the position of the first 5), the index value is 1, the second behavior of print (c) is 1, the maximum value is 9, the index value is 0, and the index value is 0. Because a has three rows, the resulting c has three values, that is, one row and three columns.

3. For the three-dimensional matrix a [0] [1] [2], the situation is the most replicated, but it is the most widely used in lstm.

Import numpy as npa = np.array ([[1, 5, 5, 2], [9,-6, 2, 8], [- 3, 7,-9, 1], [[- 1, 7,-5, 2], [9] 6, 2, 8], [3, 7, 9, 1], [[21, 6,-5, 2], [9, 36, 2, 8], [3, 7, 79, 1]]) b=np.argmax (a, axis=0) # for three-dimensional matrices A has three directions a [0] [1] [2] # when axis=0, it is to find the maximum value in the a [0] direction, that is, two matrices are compared, specifically # (1) compare the first row of the three matrices That is, take [1, 5, 5, 2], # [- 1, 7,-5, 2], # [21, 6,-5, 2], # and then compare the maximum value of each column in that matrix, we can see that the maximum value of the first column 1 is 21, and in the third matrix, the maximum value of the index is 7. In the second matrix, the index value is 1. Finally, we get the comparison result [2100] # and then take out the second row of the three matrixes. According to the above method, we get the comparison result [0200] # there are three. So the final result is a three-row and four-column matrix print (b) # [[000] # [0100] # [1010]] c=np.argmax (a, axis=1) # for a three-dimensional matrix, a has three directions a [0] [1] [2] # when axis=1, it is to find the maximum value in the a [1] direction, that is, to compare in the column direction This means to compare in the column direction within each matrix # (1) look at the first matrix # [1, 5, 5, 2], # [9 Magi-6, 2, 8], # [- 3, 7,-9, 1] # compare the maximum value of each column, you can see that the maximum value of the first column is 9. The index value is column 2, the maximum value is 7, and the index value is 7, so for the first matrix, find out the index result is [1] # and take out 2 more. According to the above method, there are three comparison results [1021] # So the final result b is three rows and four columns matrix print (c) # [[1 201] # [1 021] # [0 21]] d=np.argmax (a, axis=2) # for a three dimensional matrix, a has three directions a [0] [1] [2] # when axis=2, it is to find the maximum value in the a [2] direction, that is, to compare in the row direction This means to compare in the row direction within each matrix # (1) look at the first matrix # [1, 5, 5, 2], # [9,-6, 2, 8], # [- 3, 7,-9, 1] # to find the maximum value of the first row, we can see that the maximum value of the first row [1, 5, 5, 2] is 5. The index value is row 2 [9,-6, 2, 8], the maximum value is 9, and the index value is 9. Therefore, for the first matrix, the maximum index result is [1] # and two matrices are obtained. According to the above method, there are three comparison results [1021] # So the final result d is a three-row and three-column matrix print (d) # [[101] # [102] # [012]] # the last case Specify the matrix a [0,-1,:], the first number 0 means to take out the first matrix (it can be seen from the previous that a has three matrices) is # [1, 5, 5, 2], # [9,-6, 2, 8], # [- 3, 7,-9, 1] # the second number "- 1" represents the penultimate line For # [- 3, 7,-9, 1] # the maximum index value for this line is 1 #,-1 Represent the last line m=np.argmax (a [0,-1,:]) print (m) # 1 # h, take the second matrix # [- 1, 7,-5, 2], # [9, 6, 2, 8], # [3, 7, 9, 1] # of line 3 [3, 7, 9, 1] # has a maximum of 9 The index is 2h=np.argmax (a [1,2,:]) print (h) # 2 g=np.argmax (a [1 g=np.argmax, 2]) # g, take out the matrix a, the third column of the second matrix is-5 argmax 2, the maximum is 9, and the index is 2print (g) # 2. This is the end of the article on "how to use argmax in numpy". I hope the above content can be helpful to you, so that you can learn more knowledge. If you think the article is good, please share it for more people to see.

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: 0

*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