Accessing required number of indices in an array

Question:

I have an array like:

a=np.array([20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
 68 69 70 71 72 73 74 75 76 77 78 79])

requirement:
I would to like to access 10 indices in an array
the above array length is 60,60/10=6. So, i need every 6th indices in an array a.

required output:[0,6,12,18,24,30,36,42,48,64,60]

Asked By: Bhar Jay

||

Answers:

Numpy is powerful i would recommend to read the Documentation about indexing in numpy

everySixthEntry=a[np.arange(0,a.shape[0],6)]

Answered By: jack

I think you meant to say:
The required index values are [0,6,12,18,24,30,36,42,48,64,60]
Corresponding output values are [20, 26, 32, 38, 44, 50, 56, 62, 68, 74]

The code below should give you the values for every 6th index.

a=np.array([20,21,22,23,24,25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43,
 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67,
 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,])

Out=[]
for i in range(10):
    Out.append(a[6*i])
print(Out)

Output is :

[20, 26, 32, 38, 44, 50, 56, 62, 68, 74]

If the Index values are required: Do the following

Out1=[]
for i in range(0,11):  #for only 10 indices (going from 0 to 10)
    print(6*i)
    Out1.append(6*i)
print("The required index values is : {}".format(Out1))

This gives an output :

0
6
12
18
24
30
36
42
48
54
60
The required index values is : [0, 6, 12, 18, 24, 30, 36, 42, 48, 54, 60]
Answered By: Geg

You can generate the indexes for any array a with np.arange(len(a)). To access every 6th index use the a slice a[start:stop:step]. Jack posted one way, here a bit more detailed.

import numpy as np

# define data
a = np.arange(60) + 20

# generate indexes for the array, index start at 0 till len(a)-1
indexes = np.arange(len(a))[::6]  # [start:stop:step]
# the same result a bit different
indexes = np.arange(0, len(a), 6)  # (start,stop,step)
indexes
# -> array([ 0,  6, 12, 18, 24, 30, 36, 42, 48, 54])

# access the values of your original array
a[indexes]
# -> array([20, 26, 32, 38, 44, 50, 56, 62, 68, 74])

Basics of slicing

a[start:stop:step] is equivalent to a[slice(start, stop, step)]. If you don’t want to specify any of start, stop, step set it to None. start and stop takes values from 0 to len(a)-1 and negative represents the position from the end of the array.

Some Slice Examples:

step = 20
a[slice(None, None, step)], a[slice(0, -1, step)], a[0: -1: step], a[::step]
# all -> array([20, 40, 60])

# the first 4 elements
step = 1
start = 0  # or None
end = 5
a[slice(start, end, step)], a[slice(start, end)] , a[start: end: step] , a[start:end]
# all -> array([20, 21, 22, 23])

# the last 4 elements
step = 1
start = -4
end = None  # -1 will cute the last entry
a[slice(start, end, step)], a[slice(start, end)] , a[start: end: step] , a[start:end]
# all -> array([76, 77, 78, 79]
Answered By: Andrew
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.