4

I have an array of integers.

data = [10,20,30,40,50,60,70,80,90,100]

I want to extract a range of integers from the array and get a smaller array.

data_extracted = [20,30,40]

I tried numpy.take.

data = [10,20,30,40,50,60,70,80,90,100]
start = 1    # index of starting data entry (20)
end = 3      # index of ending data entry (40)
data_extracted = np.take(data,[start:end])

I get a syntax error pointing to the : in numpy.take.

Is there a better way to use numpy.take to store part of an array in a separate array?

2
  • 3
    data_extracted = data[1:3]? Commented Jan 31, 2017 at 16:44
  • (where I was a bit too quick to assume you wanted the start..<end elements: for a slice over elements with indices 1 through 3 (rather than to 3), use ... = data[1:4]) Commented Jan 31, 2017 at 17:01

3 Answers 3

5

You can directly slice the list.

import numpy as np
data = [10,20,30,40,50,60,70,80,90,100]
data_extracted = np.array(data[1:4])

Also, you do not need to use numpy.array, you could just store the data in another list:

data_extracted = data[1:4]

If you want to use numpy.take, you have to pass it a list of the desired indices as second argument:

import numpy as np
data = [10,20,30,40,50,60,70,80,90,100]
data_extracted = np.take(data, [1, 2, 3])

I do not think numpy.take is needed for this application though.

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

4 Comments

So numpy.take will not accept a range of values in colon notation? In my actual scenario I select from 256 data points, so I was trying to use .take to make this more efficient.
@SpencerH. you could make use of range(...) as follows: data_extracted = np.take(data, range(1, 4)), which might be preferable over the explicit array of indices approach above (where the range(...) call yields a an integer sequence covering the half-open range over the two arguments, range(from, to) -> [from, ..., to-1]).
@Spencer H. It will accept a range as noted by dfri. You supplied a slice in your original example, which caused the syntax error, because there was nothing to slice. If you want to choose non continuous values, a range would not be preferred anymore. You should probably idxlist.append(idx) all the indices to another list and submit that second list with only the indices to np.take(data, idxlist) in that case.
@Tristan even a list of non-sequential indices might contain subranges that are better represented using range rather than explicitly listing all members of the subrange. You can still concenate the result(s) of the call to range to single non-sequential indices, e.g. data_extracted = np.take(data, range(1, 4) + [5] + range(7,10)) will result in [20, 30, 40, 60, 80, 90, 100].
0

You ought to just use a slice to get a range of indices, there is no need for numpy.take, which is intended as a shortcut for fancy indexing.

data_extracted = data[1:4]

Comments

0

As others have mentioned, you can use fancy indexing in this case. However, if you need to use np.take because e.g. the axis you're slicing over is variable, you might try:

axis=0
data.take(range(1,4), axis=axis)

Note: this might be slower than: data_extracted = data[1:4]

Comments

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.