Returning an index from SArray

User 4716 | 4/13/2016, 8:43:15 PM

Can I return the index of a particular value contained in an SArray? For instance, sa = SArray(data=[1,2,3,4,5], dtype=int) Can I get the index of the value 2?

Thanks

Comments

User 4 | 4/13/2016, 11:39:39 PM

Hi @Suresh, that is possible with the following code: `

create an SFrame with sa as the "value" column

sf = gl.SFrame() sf['value'] = sa

add row numbers (creates the "id" column)

sf = sf.addrownumber()

get the indices of all the rows where the value is 2

this does a logical filter of the SFrame to values of 2,

then selects the "id" column.

indicesof2 = sf[sf['value'] == 2]['id'] `


User 4716 | 4/16/2016, 6:47:25 AM

Thanks Zach. Do you know why SArray does not have a method to return an index easily just as it returns indices for max and min values in the SArray? I have a Matlab background and would find this facility useful.


User 4 | 4/18/2016, 1:24:38 AM

Hi @Suresh, thanks, that is an interesting idea. How does SFrame currently return an index for max/min values? (I can think of a few ways to do that, but I want to make sure I have the same way in mind that you do before we go implement something different.)

Since SFrame is an open source project, if you have a specific API proposal you'd like to see as a feature request, you are welcome to open a feature request issue (tagged "enhancement") in the SFrame issues or even submit a pull request if you have a change you'd like to propose. Thanks for your help!