Note that there are some explanatory texts on larger screens.

plurals
  1. POOptimizing clustering in Python
    primarykey
    data
    text
    <p>I wrote my own clustering algorithm (bad, I know) for my problem. It works well, but could work faster.</p> <p>Algorithm takes list of values (1D) as in input, and works like this:</p> <ol> <li>For each cluster, calculate distance to closest neighbor cluster</li> <li>Select the cluster A which has smallest distance to neighbor B</li> <li>If distance between A and B is less then threshold, <strong>return</strong></li> <li>Combine A and B</li> <li>Goto 1.</li> </ol> <p>I probably reinvented a wheel here..</p> <p>This is my brute foce code, how to make it faster? I've Scipy and Numpy installed, if there's something ready made</p> <pre><code>#cluster center as simple average value def cluster_center(cluster): return sum(cluster) / len(cluster) #Distance between clusters def cluster_distance(a, b): return abs(cluster_center(a) - cluster_center(b)) while True: cluster_distances = [] #If nothing to cluster, ready if len(clusters) &lt; 2: break #Go thru all clusters, calculate shortest distance to neighbor for cluster in clusters: cluster_distances.append((cluster, sorted([(cluster_distance(cluster, c), c) for c in clusters if c != cluster])[0])) #Find out closest pair cluster_distances.sort(cmp=lambda a,b:cmp(a[1], b[1])) #Check if distance is under threshold 15 if cluster_distances[0][1][0] &lt; 15: a = cluster_distances[0][0] b = cluster_distances[0][1][1] #Combine clusters (combine lists) a.extend(b) #Form a new cluster list clusters = [c[0] for c in cluster_distances if c[0] != b] else: break </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload