1

I have an array data which has shape (922, 6) and trgt which is a long 1d time series. data[0] has the start indexes and data[1] the end indexes of subsets that I need to slice from trgt.

I try

trgt[data[:,0:2][0]]
>>> *** IndexError: arrays used as indices must be of integer (or boolean) type

where

data[:,0:2][0]
>>> array([0., 100.])

so I try

trgt[data[:,0:2][0].astype(int)]
>>> array([9909., 9989.])

these are the VALUES at the indexes but not the subset. I try

trgt[tuple(data[:,0:2][0].astype(int))]
>>> *** IndexError: invalid index

how can I get the subset?

1 Answer 1

4

This looks odd:

b = data[:, 0:2][0]

because with NumPy arrays it is the same as:

b = data[0, :2]

i.e. the two first columns of the first row of the array. If data is a 2d array, you will get an array with dimensions (2,). It can be used directly to index a 1d vector:

trgt[b]

No need to convert it into a slice or a tuple.

Update: With the updated description of the problem it seems that the first column contains start indices and the second column has the end indices. Then the natural thing to do is to create a list of arrays. 2d-arrays are not a good output format, as then the number of columns should be the same in each row.

In that case a one-liner will do.

lst = [ trgt[row[0]:row[1]] for row in data ]
Sign up to request clarification or add additional context in comments.

6 Comments

data[0, :2] actually has length 992. I am trying to get all 992 subsets. The indexes of the subsets are in data[0] and data[1] respectively
Are you sure? What does data[0,:2].shape say? Or data.shape?
sorry I messed up my indexes there, I meant data[:,0:2] -- data[:,0:2].shape = (992,2) and data.shape(992,6)
If you have a (992,2) array and do data[:,0:2][0], you will get only the first two elements of the first row. If you want to have the first column (a vector of 992 eleemnts), then data[:,0] is what you want. Still, you can index your trgt directly with it.
I've edited above for better explanation, I am missing something here
|

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.