Reputation: 37
I want to index the second to last row of a numpy array I have a loop that constantly appends new rows:
arr2= is an array formatted like so [[300 240 22 22]]
so in the example above x=300 y=240 w=22 h=22
these numbers get processed in the equation until wm has 2 numbers so [200, 300] and hm also has 2 numbers [200, 300] and are both NON-STOP appended to the end of the arr(not creating new arrays but rather adding to the single array. So it would look something like this constantly adding new rows(i don't know if it actually is adding new rows or overriding the old values):
----row [[301.77325 225.71806][366.08594 232.20958]]
----row [[301.54193 225.93848][365.76663 232.37358]]
---- [[300.9011 225.27153][365.21063 231.52242]]second to last appended
row
---- [[300.63205 223.20387][364.91928 229.46263]]last appended row
then there would be another row being appended this is just a snapshot
code:
arr=[] #shape is (0,)
for (x, y, w, h) in arr2:
wm=int(x + (w/2))
hm=int(y + (h/2))
arr.append([wm, hm])
simplified version:
arr=[]
for (x, w) in arr2:
wm=int(x + (w/2))
arr.append([wm])
The non-simplified code produces an array with shape (2,2) with a dtype:float32.
column (wm) column(hm)
| |
| |
[[293.51373 323.4329 ] # second to last row
[247.77493 316.02783]]
[[292.9887 322.23425]
[247.24142 314.2921 ]]
These are some examples of the indexing methods I've tried(along with the array)(these are the two last appended rows of the array):
arr [[255.44836 280.92575] # I want to index this line-second to last appended row
[298.6107 285.75986]]
arr[-1] [298.6107 285.75986]
arr[-2] [255.44836 280.92575]
arr[-2][0] 255.44836
arr[:,0] [255.44836 298.6107 ]
arr[-1,:] [298.6107 285.75986]
arr[-2,0] 255.44836
arr[-2::2] [255.44836 280.92575]
arr [[255.35194 281.08353] # along with this line-last appended row
[298.45673 285.88693]]
arr[-1] [298.45673 285.88693]
arr[-2] [255.35194 281.08353]
arr[-2][0] 255.35194
arr[:,0] [255.35194 298.45673]
arr[-1,:] [298.45673 285.88693]
arr[-2,0] 255.35194
arr[-2::2] [255.35194 281.08353]
Other times when I try to index the second to last row it shows me an axis error, which could be the problem(that the appending doesn't add new rows which is what I am trying to index but rather overrides the old values). I don't know. So I tried to switch the appending method with: concatenate, vstack, pandas. But it didn't help. All I want to do is to find the difference between the last and second to last appended rows from the second column in the wm element (in the example 280.92575-281.08353) and also the same with the first column of wm element(255.44836-255.35194). All the various indexing methods have not fetched the second to last row just the last row. Please let me know if there's a different way to index the second to last row in a different way or if there are other indexing methods which I haven't already tried. Sorry if it is confusing still getting the hang of it. I can try to clear up any confusion. Thanks in advance!
Upvotes: 1
Views: 34258
Reputation: 61355
If I understand your description correctly, then the following should work:
In [49]: arr
Out[49]:
array([[255.44836, 280.92575],
[298.6107 , 285.75986],
[255.35194, 281.08353],
[298.45673, 285.88693]])
In [50]: arr[0] - arr[-2]
Out[50]: array([ 0.09642, -0.15778])
# accessing elements in a `forward manner`
In [51]: arr[0] - arr[2]
Out[51]: array([ 0.09642, -0.15778])
# accessing elements in a `reverse manner` using negative indices
In [52]: arr[-4] - arr[-2]
Out[52]: array([ 0.09642, -0.15778])
# or mix both
In [50]: arr[0] - arr[-2]
Out[50]: array([ 0.09642, -0.15778])
You can use either the forward manner indexing or reverse manner indexing or mixed, whichever is convenient.
Upvotes: 1
Reputation: 411
If you want to index the last item of a list use
arr[-1]
If you want to index the second to last item use
arr[-2]
However make sure that your array is long enough. If your array only has 1 item and you want to index the second to last item, it will result in an error.
Upvotes: 9