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.
4 Answers
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]
Comments
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
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
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]))
2 is closer to 1, closer than what?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.