We're working on a code that clusters with a pie line.
First, the initial data is
a=[1,2,13,14,16,25,54,68,88,94,101,110,114,122,132,165,170]
Assuming that it looks like this, I wrote the clustering code.
The clustering code receives the maximum distance from the data matrix as a parameter.
Assuming a distance of 10 maximum permissible distance,
If the distance between the two elements is less than or equal to 10, coding is done to belong to the same array.
The results in the example above are represented by the following two-dimensional array:
[[1,2],[13,14,16,25],[54],[68],[88,94,101,110,114,122,132],[165,170]]
What I'm trying to do here is
Remove internal data of length 1 such as [54], [68] and
You would like to get the following results
[[1,2],[13,14,16,25],[88,94,101,110,114,122,132],[165,170]]
In order to do this, I think I should use a.pop roughly.;
The desired result will not come out.
th = 10
g=[[a[0]]]
for i in a[1:] :
if abs(i - g[-1][-1]) <= th:
g[-1].append(i)
else:
g.append([i])
Make a new list that only collects lists with more than one element length.
clustered = [[1,2],[13,14,16,25],[54],[68],[88,94,101,110,114,122,132],[165,170]]
removed = []
for ls in clustered:
if len(ls) > 1:
removed.append(ls)
# # removed = [[1, 2], [13, 14, 16, 25], [88, 94, 101, 110, 114, 122, 132], [165, 170]]
clustered = [[1,2],[13,14,16,25],[54],[68],[88,94,101,110,114,122,132],[165,170]]
removed = [ls for ls in clustered if len(ls) > 1]
# # removed = [[1, 2], [13, 14, 16, 25], [88, 94, 101, 110, 114, 122, 132], [165, 170]]
© 2024 OneMinuteCode. All rights reserved.