title: How to handle negative values of cosine similarities
body: <p>I computed tf-idf of my documents based of terms. Then, I applied LSA to reduce the dimensionality of the terms. 'similarity_dist' contains values which are negative (see table below). How can I compute cosine distance with the range 0-1?</p>
  
  <pre><code>tf_vectorizer = CountVectorizer(max_df=0.95, min_df=2, tokenizer=tokenize_and_stem, stop_words='english')
  %time tf = tf_vectorizer.fit_transform(descriptions)
  print(tf.shape)
  svd  = TruncatedSVD(100)
  normalizer = Normalizer(copy=False)
  lsa = make_pipeline(svd, normalizer)
  tfidf_desc = lsa.fit_transform(tfidf_matrix_desc)
  explained_variance = svd.explained_variance_ratio_.sum()
  print("Explained variance of the SVD step: {}%".format(int(explained_variance * 100)))
  
  similarity_dist = cosine_similarity(tfidf_desc)
  pd.DataFrame(similarity_dist,index=descriptions.index, columns=descriptions.index).head(10)
  
  print(tfidf_matrix_desc.min(),tfidf_matrix_desc.max())
  #0.0 0.736443429828
  
  print(tfidf_desc.min(),tfidf_desc.max())
  #-0.518015429416 0.988306783341
  
  print(similarity_dist.max(),similarity_dist.min())
  #1.0 -0.272010919022
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/1sqs7.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/1sqs7.png" alt="enter image description here"></a> </p>