I have a numpy list which I initiate by (my_array=[] and has a shape of (0,)) then I append the wm and hm elements to it like so(r is a cascade with the format of-[[300 240 22 22]]):
my_array=[]
for (x, y, w, h) in r:
wm=int(x+ (w/2.))
hm=int(y+ (h/2.))
my_array.append([numpy.float32(wm), numpy.float32(hm)])
return numpy.array(my_array)
That code produces:
wm element the hm element
[[270.01 303.43] [310.17 306.37]] # second to last row
[[269.82 303.38] [310.99 306.86]] # the last row
the shape of the returned array is (2,2) and is dtype:float32
...
Now the problem is that when I tried to append the 303.43 it theoratically would be [-2][1] but it indexes 303.38. which is fine but I also need to index 303.43 aswell.
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
-for example [0][-1] indexes the wm element[0] and last row [-1] I want to index the second last row aswell and tried [0][-2] but it didnt work as intended(it indexed the 269.82).
So I tried [0][1][-2] but it didnt work due to IndexError: invalid index to scalar variable.
All I want to do is to find the difference between the last and second to last row for the 2 columns in the wm element(so in the example above it would be 269.82-270.1=-0.19 and 303.38-303.43=-0.05). The indexing doesn't work. So is there a way around this problem?
Making 3 'r' values:
In [798]: r=np.array([[300, 240, 22, 22]])+np.array([[0],[2],[3]])
In [799]: r
Out[799]:
array([[300, 240, 22, 22],
[302, 242, 24, 24],
[303, 243, 25, 25]])
your loop produces:
In [800]: my_array=[]
...: for (x, y, w, h) in r:
...: wm=int(x+ (w/2.))
...: hm=int(y+ (h/2.))
...: my_array.append([numpy.float32(wm), numpy.float32(hm)])
...:
In [801]: my_array
Out[801]: [[311.0, 251.0], [314.0, 254.0], [315.0, 255.0]]
As an array:
In [802]: arr = np.array(my_array)
In [803]: arr
Out[803]:
array([[311., 251.],
[314., 254.],
[315., 255.]], dtype=float32)
2nd to the last row
In [804]: arr[-2,:]
Out[804]: array([314., 254.], dtype=float32)
last row:
In [805]: arr[-1,:]
Out[805]: array([315., 255.], dtype=float32)
Their difference:
In [806]: arr[-2,:]-arr[-1,:]
Out[806]: array([-1., -1.], dtype=float32)
first column:
In [807]: arr[:,0]
Out[807]: array([311., 314., 315.], dtype=float32)
2nd column
In [808]: arr[:,1]
Out[808]: array([251., 254., 255.], dtype=float32)
See your previous question for a similar answer:
Indexing appended elements from a numpy array python
I couldn't reproduce your array but given your (very convoluted) question I believe that the following will do what you ask in the end:
This should give you 269.82-270.1=-0.19
my_array[0][-2]-my_array[0][0]
and this should give you 303.38-303.43=-0.05
my_array[0][-1]-my_array[0][1]
In general I believe that you index your array my_array
as follows:
wm element the hm element
[[my_array[0][0] my_array[0][1]] [my_array[1][0] my_array[1][1]]] # second to last row
[[my_array[0][-2] my_array[0][-1]] [my_array[1][-2] my_array[1][-1]]] # the last row
In your particular case the second to last element is also the third so you could index them as my_array[0][2]
instead of my_array[0][-2]
and my_array[1][2]
instead of my_array[1][-2]