title: cosine similarity optimized implementation
body: <p>I am trying to understand this optimized code to find cosine similarity between users matrix.</p>
  
  <pre><code>def fast_similarity(ratings,epsilon=1e-9):
      # epsilon -&gt; small number for handling dived-by-zero errors
      sim = ratings.T.dot(ratings) + epsilon
      norms = np.array([np.sqrt(np.diagonal(sim))])
      return (sim / norms / norms.T)
  </code></pre>
  
  <p>If ratings =</p>
  
  <pre><code>           items           
       u  [
       s    [1,2,3]
       e    [4,5,6]
       r    [7,8,9] 
       s  ]
  </code></pre>
  
  <p>nomrs will be equal to = [1^2 + 5^2 + 9^2]</p>
  
  <p>but why we are writing sim/norms/norms.T to calculate cosine similarity?
  Any help is appreciated.</p>