Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get the neighboring elements in a numpy array with taking boundaries into account?

Tags:

python

numpy

I want to get the neighbors of the certain element in the numpy array. Lets consider following example

    a = numpy.array([0,1,2,3,4,5,6,7,8,9])

So I want to specify position 5 and want to get three neighbors from both sides. It can be done

   index = 5
   num_neighbor=3
   left = a[index-num_neighbor:index]
   right= a[num_neighbor+1:num_neighbor+index+1]

The above code does not take care of the boundaries... I want that i get the neighbours within the boundaries of the array. For this consider the following example if index is 1 then the left neighbor is only one element which is 0.

Thanks a lot

like image 508
Shan Avatar asked Sep 16 '11 09:09

Shan


People also ask

How do you access parts of a NumPy array?

You can access an array element by referring to its index number. The indexes in NumPy arrays start with 0, meaning that the first element has index 0, and the second has index 1 etc.

How do you split the element of a given NumPy array with spaces?

To split the elements of a given array with spaces we will use numpy. char. split(). It is a function for doing string operations in NumPy.

How do you access different rows of a multidimensional NumPy array?

In NumPy , it is very easy to access any rows of a multidimensional array. All we need to do is Slicing the array according to the given conditions. Whenever we need to perform analysis, slicing plays an important role.

How do I extract an element from a NumPy array?

Using the logical_and() method The logical_and() method from the numpy package accepts multiple conditions or expressions as a parameter. Each of the conditions or the expressions should return a boolean value. These boolean values are used to extract the required elements from the array.


2 Answers

left = a[max(0,index-num_neighbor):index]
like image 121
eph Avatar answered Sep 22 '22 06:09

eph


import numpy as np
a = np.array([0,1,2,3,4,5,6,7,8,9])
num_neighbor=3    

for index in range(len(a)):
    left = a[:index][-num_neighbor:]
    right= a[index+1:num_neighbor+index+1]
    print(index,left,right)

yields

(0, array([], dtype=int32), array([1, 2, 3]))
(1, array([0]), array([2, 3, 4]))
(2, array([0, 1]), array([3, 4, 5]))
(3, array([0, 1, 2]), array([4, 5, 6]))
(4, array([1, 2, 3]), array([5, 6, 7]))
(5, array([2, 3, 4]), array([6, 7, 8]))
(6, array([3, 4, 5]), array([7, 8, 9]))
(7, array([4, 5, 6]), array([8, 9]))
(8, array([5, 6, 7]), array([9]))
(9, array([6, 7, 8]), array([], dtype=int32))

The reason why a[index-num_neighbor:index] does not work when index<num_neighbor is because of slicing rules #3 and #4:

Given s[i:j]:

If i or j is negative, the index is relative to the end of the string: len(s) + i or len(s) + j is substituted.

The slice of s from i to j is defined as the sequence of items with index k such that i <= k < j. If i or j is greater than len(s), use len(s). If i is omitted or None, use 0. If j is omitted or None, use len(s). If i is greater than or equal to j, the slice is empty.

So when index=1, then a[index-num_neighbor:index] = a[-2:1] = a[10-2:1] = a[8:1] = [].

like image 27
unutbu Avatar answered Sep 22 '22 06:09

unutbu