Python numpy, I have a question

Asked 2 years ago, Updated 2 years ago, 49 views

>>> import numpy asp
>>> g = np.arange(1,10).reshape(3,3)
>>> g
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])
>>> g[1, 2]
6
>>> g[2]
array([7, 8, 9])
>>> g[[0,0],[2,0]]
array([3, 1])

Here, g[1, 2] and g[2] understood why those results came out

I don't understand why the answer to g[[0,0],[2,0]] is array([3, 1]) ㅠ<

I thought it was an array ([1,7]), but I would appreciate it if you could also tell me the reason why this idea was wrong...

numpy

2022-09-21 19:01

2 Answers

I'm not familiar with it, but I think you can think of it as similar to the matrix calculation method.

// First left row (0), first right column (2)
// Second row left (0), second row right (0)
g[[0,0], [2,0]] = g[[0x2], [0x0]] 

If you think about it like this, the first element is row 0, row 2, row 2 and the second element is row 0, row 0...

That is, array (3, 1)..


2022-09-21 19:01

Maybe you wanted to get g[0,0] and g[2,0]. I'm not sure about nupmy, so I don't think I can help you with how to get the value, but I'll explain how the value above came out based on what I tested.

You'll know if you test it yourself, g[0] and g[[0] are different values

>>> g[0]
array([1,2,3])
>>> g[[0]]
array([[1,2,3]])

In the first case, the elements of the array are 1, 2, and 3 The second case is the first element in the array, which has the array itself [1,2,3].

Therefore, the execution results of g[0,0] and g[0,0] are also shown below.


>>> g[0,0]
1
>>> g[[0,0]]
array([[1,2,3],
       [1,2,3]])

The first case is to return the element in the (0,0) position The second case returns an array with elements ([1,2,3]) in the (0) position as their respective elements.

In addition, running g[0,2] and g[[0],[2]] results are as follows.

>>> g[0,2]
3
>>> g[[0],[2]]
array([3])

Both the first and second cases return 3, but one is the number itself and the other is returning an array. If you think about it a little bit, you can see that the second case is returning the third value of 3 as an array for [1,2,3], which is the return value of g[0].

The following is a summary of the above.

In g[[0,0],[2,0] the [0,0] part means an array with g[0] and g[0] as elements. [2,0] means the third element of the first array and the first element of the second array in the above array.

So if you do this, here's what you's like:

>>> g[[0,0],[2,0]]
array([3,1])

I don't know if it's actually used in this way, but if you put an array in the first element based on a numpy two-dimensional array, the row corresponding to the element in the array itself is returned as the element in the array, and if the array is in the second element, it is used as the element in the same position as the element in the array.

I'm not good at talking, so I explained it in a complicated way, but I don't know if it'll help you understand.

By the way, you can also do the following to get the value you want.

>>> g[[0,2],[0,0]]
array([1,7])

However, the part I explained is about why such a value comes out, and there may be other good ways to use it in this way, so I recommend you to study more about numpy.


2022-09-21 19:01

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.