title: Using K-means with cosine similarity - Python
body: <p><br>
  I am trying to implement <code>Kmeans</code> algorithm in python which will use <code>cosine distance</code> instead of euclidean distance as distance metric.<br>
  I understand that using different distance function can be fatal and should done carefully. Using cosine distance as metric forces me to change the average function (the average in accordance to cosine distance must be an element by element average of the normalized vectors).<br><br>
  I have seen <a href="https://gist.github.com/jaganadhg/b3f6af86ad99bf6e9bb7be21e5baa1b5" rel="noreferrer">this</a> elegant solution of manually overriding the distance function of sklearn, and I want to use the same technique to override the averaging section of the code but I couldn't find it.<br>
  <br>Does anyone knows How can it be done ?<br>
  How critical is it that the distance metric doesn't satisfy the triangular inequality?<br>
  If anyone knows a different efficient implementation of kmeans where I use cosine metric or satisfy an distance and averaging functions it would also be realy helpful.
  <br>
  Thank you very much! 
  <br><br>
  <strong>Edit:</strong><br>
  After using the angular distance instead of cosine distance, The code looks as something like that:</p>
  
  <pre><code>def KMeans_cosine_fit(sparse_data, nclust = 10, njobs=-1, randomstate=None):
      # Manually override euclidean
      def euc_dist(X, Y = None, Y_norm_squared = None, squared = False):
          #return pairwise_distances(X, Y, metric = 'cosine', n_jobs = 10)
          return np.arccos(cosine_similarity(X, Y))/np.pi
      k_means_.euclidean_distances = euc_dist
      kmeans = k_means_.KMeans(n_clusters = nclust, n_jobs = njobs, random_state = randomstate)
      _ = kmeans.fit(sparse_data)
      return kmeans
  </code></pre>
  
  <p>I noticed (with mathematics calculations) that if the vectors are normalized the standard average works well for the angular metric. As far as I understand, I have to change <code>_mini_batch_step()</code> in <a href="https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/cluster/k_means_.py" rel="noreferrer">k_means_.py</a>. But the function is pretty complicated and I couldn't understand how to do it. 
  <br>
  Does anyone knows about alternative solution?<br>
  Or maybe, Does anyone knows how can I edit this function with a one that always forces the centroids to be normalized?</p>