1

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!

3
  • What was wrong with [-2]? Commented Apr 27, 2019 at 18:01
  • Seems like you're trying to draw a bounding box. could you please provide a simple and clear example? This one is not.. Commented Apr 27, 2019 at 18:13
  • @mkrieger1 [-2] indexes the last row(255.35194 281.08353) not the second-to-last row(255.44836 280.92575) Commented Apr 28, 2019 at 18:48

2 Answers 2

9

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Well for other arrays [-1] and [-2] would work so you have the right idea, but for mine, these don't index the right thing. As shown in the above-given examples.
Can you give us an example of 'arr' and what exactly you want to index
Well, giving an example is a little tricky because it is always getting updated so if I take a snapshot of one section people think that, that's the whole array but it's just the last appended row so(each --arr indicates new row): ----arr [[273.5383 203.26279] [339.77667 209.72774]]-third last ----arr [[273.3976 203.5339 ] [339.6704 209.93922]]-second last ----arr [[273.3121 203.85924] [339.57697 210.15872]] - last appended row
I believe it is because my array has size 2 but axis 0. So if I want to index 2 it fetches IndexError: index 2 is out of bounds for axis 0 with size 2.
1

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.

4 Comments

Well, there's only one array. Which I append new rows of values. So the arr1[0]-arr2[0] doesn't work. Now arr[0] does index the last row but then I also need to be able to index the second last row. The arr is an array which is constantly being updated(appended) with new values all I want to know is how to index the second to last row of appended values.
I have updated the answer. Hope that solves your issue. If not, let me know. Also, please provide a good simple example. Your current example is so confusing...
I did provide a simplified version, that is as much as I could simplify it. Furthermore, the arr[0]-array[2] fetches IndexError: index 2 is out of bounds for axis 0 with size 2. Likewise for arr[-4]-arr[-2] IndexError: index -4 is out of bounds for axis 0 with size 2. As for [0]-[-2] it fetches [0. 0.]. What I found was that the first [] indexes either the wm[0] or hm[1] element, then the second [] indexes one of the two columns of values inside each element. So I either get another indexing value ([0][1][-2]) which fetches error.
Or I remove the hm, which with my recent discovery is actually a column(I added a visual representation) so it isn't ideal but even with the array only having wm column, [0][-2] fetches IndexError: list index out of range. So now I have exhausted all options I can think of, thus I am asking the community. It is hard for me to explain all this but hopefully, you understood it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.