title: Clustering algorithms with custom distance function in Python
body: <p>I've got a clustering problem that I believe requires an intuitive distance function. Each instance has an x, y coordinate but also has a set of attributes that describe it (varying in number per instance). Ideally it would be possible to pass it pythonobjects (instances of a class) and compare them arbitrarily based on their content.</p>
  
  <p>I want to represent the distance as a weighted sum of the euclidean distance between the x, y values and something like a jaccard index to measure the set overlap of the other attributes. Something like:</p>
  
  <pre><code>dist = (euclidean(x1, y1, x2, y2) * 0.6) + (1-jaccard(attrs1, attrs2) * 0.4)
  </code></pre>
  
  <p>Most of the clustering algorithms and implementations I've found convert instance features into numbers. For example with dbscan in sklearn, to do my distance function I would need to convert the numbers back into the original representation somehow.</p>
  
  <p>It would be great if it were possible to do clustering using a distance function that can compare instances in any arbitrary way. For example imagine a euclidean distance function that would evaluate objects as closer if they matched on another non-spatial feature.</p>
  
  <pre><code>def dist(ins1, ins2):
       euc = euclidean(ins1.x, ins1.y, ins2.x, ins2.y)
       if ins1.feature1 == ins2.feature1:
            euc = euc * 0.9
       return euc         
  </code></pre>
  
  <p>Is there a method that would suit this? It would also be nice if the number of clusters didn't have to be set upfront (but this is not critical for me).</p>