Numpy slicing allows you to input a list of indices to an array so that you can slice to the exact values you want.
For example:
import numpy as np
a = np.random.randn(10)
a[[2,4,6,8]]
This will return the 2nd, 4th, 6th, and 8th array elements (keeping in mind that python indices start from 0). So, if you want every 2nd element starting from an index x, you can simply populate a list with those elements and then feed that list into the array to get the elements you want, e.g:
idx = list(range(2,10,2))
a[idx]
This again returns the desired elements (index 2,4,6,8).
Answer from enumaris on Stack OverflowNumpy slicing allows you to input a list of indices to an array so that you can slice to the exact values you want.
For example:
import numpy as np
a = np.random.randn(10)
a[[2,4,6,8]]
This will return the 2nd, 4th, 6th, and 8th array elements (keeping in mind that python indices start from 0). So, if you want every 2nd element starting from an index x, you can simply populate a list with those elements and then feed that list into the array to get the elements you want, e.g:
idx = list(range(2,10,2))
a[idx]
This again returns the desired elements (index 2,4,6,8).
Your x is an array of length 20
x = np.arange(0,20)
Returns
x [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]
Access every nth(even indices here) element ignoring first two and last two indices in the array x by
print(x[2:len(x)-1:2])
Returns
[2 4 6 8]
And for the rest in similar fashion,
print(x[5:len(x)-1:5])
print(x[4:len(x)-1:4])
Returns
[ 5 10 15]
[ 4 8 12 16]
Python: using range of indexes of an array in conditions - Stack Overflow
Using python range objects to index into numpy arrays - Stack Overflow
Array elements in a specific range and center element of range
numpy.take range of array elements Python - Stack Overflow
import numpy as np outcomes1000 = np.random.choice(die, size = 1000, p = probabilities) averages = [ np.mean(outcomes1000[range(0,i)]) for i in range(1,1001)]
What does outcomes1000[range(0,i)] do? Can we use range() to access elements of an array?
Just to wrap this up (thanks to @WarrenWeckesser in the comments): This behavior is actually documented. One only has to realize that range objects are python sequences in the strict sense.
So this is just a case of fancy indexing. Be warned, though, that it is very slow:
>>> a = np.arange(100000)
>>> timeit(lambda: a[range(100000)], number=1000)
12.969507368048653
>>> timeit(lambda: a[list(range(100000))], number=1000)
7.990526253008284
>>> timeit(lambda: a[np.arange(100000)], number=1000)
0.22483703796751797
Not a proper answer, but too long for comment.
In fact, it seems to work with about any indexable object:
import numpy as np
class MyIndex:
def __init__(self, n):
self.n = n
def __getitem__(self, i):
if i < 0 or i >= self.n:
raise IndexError
return i
def __len__(self):
return self.n
a = np.array([1, 2, 3])
print(a[MyIndex(2)])
# [1 2]
I think the relevant lines in NumPy's code are below this comment in core/src/multiarray/mapping.c:
/*
* Some other type of short sequence - assume we should unpack it like a
* tuple, and then decide whether that was actually necessary.
*/
But I'm not entirely sure. For some reason, this hangs if you remove the if i < 0 or i >= self.n: raise IndexError, even though there is a __len__, so at some point it seems to be iterating through the given object until IndexError is raised.
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.
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]