Reputation: 37
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 theoretically would be [-2][1] but it indexes 303.38. which is fine but I also need to index 303.43 as well.
-for example [0][-1] indexes the wm element[0] and last row [-1] I want to index the second last row as well and tried [0][-2] but it didn't work as intended(it indexed the 269.82).
So I tried [0][1][-2] but it didn't 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). All solutions presented in other questions dont work ([0][-1],[-1][0], you can try them yourself to find out) The indexing doesn't work. So is there a way around this problem? Please explain it fully because I am still kind of new to this! Thanks in advance!
Addition:
Taking the last two blocks of data
Indexing the array (in the idle) fetches(I copied the last two blocks of the array):
[[293.51373 323.4329 ]
[247.77493 316.02783]]
[[292.9887 322.23425]
[247.24142 314.2921 ]]
On my program, it shows up as (the same array)
--wm element------------------hm element
[[293.51373 323.4329 ][247.77493 316.02783]]
I consider this the second to last row
[[292.9887 322.23425][247.24142 314.2921 ]]
and I thought this was the last row
This brought forth a lot of confusion for me, but I ignored the minor difference of the way they are displayed until now. Now, the question is how to index the 323.4329 and the 293.51373 numbers, it would be better if they can be indexed separately?
Upvotes: 0
Views: 1912
Reputation: 231385
A sample r
:
In [41]: r = np.array([[0,0,8,10],[1,1,6,8],[2,2,10,12]])
In [42]: r
Out[42]:
array([[ 0, 0, 8, 10],
[ 1, 1, 6, 8],
[ 2, 2, 10, 12]])
In [43]: my_array=[]
In [45]: for (ex,ey,ew,eh) in r:
...: wm = int(ex+(ew/2))
...: hm = int(ey+(eh/2))
...: print(wm,hm)
...: my_array.append([wm,hm])
...:
4 5
4 5
7 8
The resulting array:
In [46]: arr = np.array(my_array)
In [47]: arr
Out[47]:
array([[4, 5],
[4, 5],
[7, 8]])
Sample indexing:
In [48]: arr[:,0]
Out[48]: array([4, 4, 7]) # the 3 wm values
In [49]: arr[-1,:] # the last values produced by the last `r` row
Out[49]: array([7, 8])
Or a more symbolic array:
In [52]: arr = np.array([[f'wm{i}',f'hm{i}'] for i in range(3)])
In [53]: arr
Out[53]:
array([['wm0', 'hm0'],
['wm1', 'hm1'],
['wm2', 'hm2']], dtype='<U3')
In [54]: arr[:,0]
Out[54]: array(['wm0', 'wm1', 'wm2'], dtype='<U3')
In [55]: arr[-1,:]
Out[55]: array(['wm2', 'hm2'], dtype='<U3')
===
In [108]: arr = np.array([[313.5536, 330.60587], [368.23245, 332.70932]])
In [109]: arr
Out[109]:
array([[313.5536 , 330.60587], # 2nd to the last row
[368.23245, 332.70932]]) # last row
Last row:
In [110]: arr[-1]
Out[110]: array([368.23245, 332.70932])
In [111]: arr[-1,:]
Out[111]: array([368.23245, 332.70932])
First column
In [112]: arr[:,0]
Out[112]: array([313.5536 , 368.23245])
2nd to the last row:
In [113]: arr[-2,:]
Out[113]: array([313.5536 , 330.60587])
Upvotes: 2