2

Actually i want to remove the elements from numpy array which are closer to each other.For example i have array [1,2,10,11,18,19] then I need code that can give output like [1,10,18] because 2 is closer to 1 and so on.

3
  • 3
    What do you mean when you say 2 is closer to 1, closer than what? Commented Dec 14, 2018 at 7:45
  • 2
    Define Closer, From vague definition it looks like you want to start from start of the array then if next element is difference of one then remove else keep. But better solution would be to use k-mean, because it is stat question I think, use k-mean to make groups and then choose min value from each group. Commented Dec 14, 2018 at 7:45
  • what should the absolute difference be between digits in the array? Also, specify if the rule is applicable to only adjacent array element. Additionally, should the array retain the smaller of the two numbers being compared. How do you solve cases of equal numbers ? Commented Dec 14, 2018 at 7:46

4 Answers 4

4

In the following is provided an additional solution using numpy functionalities (more precisely np.ediff1d which makes the differences between consecutive elements of a given array. This code considers as threshold the value associated to the th variable.

a = np.array([1,2,10,11,18,19])
th = 1
b = np.delete(a, np.argwhere(np.ediff1d(a) <= th) + 1) # [1, 10, 18]
Sign up to request clarification or add additional context in comments.

Comments

2

Here is simple function to find the first values of series of consecutives values in a 1D numpy array.

import numpy as np

def find_consec(a, step=1):
    vals = []
    for i, x in enumerate(a):
        if i == 0:
            diff = a[i + 1] - x
            if diff == step:
                vals.append(x)
        elif i < a.size-1:
            diff = a[i + 1] - x
            if diff > step:
                vals.append(a[i + 1])
    return np.array(vals)

a = np.array([1,2,10,11,18,19])
find_consec(a) # [1, 10, 18]

Comments

0

Welcome to stackoverflow. below is the code that can answer you question:

def closer(arr,cozy):
    result = []
    result.append(arr[0])
    for i in range(1,len(arr)-1):
        if arr[i]-result[-1]>cozy:
            result.append(arr[i])
    print result           

Example:

a = [6,10,7,20,21,16,14,3,2]
a.sort()
closer(a,1)
output : [2, 6, 10, 14, 16, 20]
closer(a,3)
Output: [2, 6, 10, 14, 20]

1 Comment

Can you please let me know the reason for down flag. So that i can improve my answer for future
0

Sorry for being late to the party. But Agglomerative Clustering may be what you're looking for. By specifying the distance threshold and linkage criterion, the algorithm will return the desired result.

from sklearn.cluster import AgglomerativeClustering
a = np.array([1,2,10,11,18,19])
dups_filter = AgglomerativeClustering(linkage="single", distance_threshold=1.1, metric="manhattan", n_clusters=None)

cluster_labels = dups_filter.fit_predict(a.reshape(-1,1))
filtered_array = np.array([])
for c in range(dups_filter.n_clusters_):
 filtered_array = np.append(filtered_array, np.min(a[cluster_labels==c]))

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.