title: Calculating cosine similarity of columns of a python matrix
body: <p>I have a numpy matrix say A as below</p>
  
  <pre><code>array([[1, 2, 3],
         [1, 2, 2]])
  </code></pre>
  
  <p>I want to find the cosine similarity matrix of this a matrix where cosine similarity is between the columns.</p>
  
  <p>Now cosine similarity of two vectors is just a dot product of two normalized by the L2 norm product of each</p>
  
  <p>But I don't want to iterate for each column in a loop and do it. </p>
  
  <p>So I first tried this: </p>
  
  <pre><code>from scipy.spatial import distance 
  cos=distance.cdist(a.T,a.T,'cosine')
  </code></pre>
  
  <p>Here I am taking transpose as else it would do cosine of rows(observations). I want for columns. </p>
  
  <p>However I am not sure this is the right answer. The doc of this function says it gives 1- cosine_similarity. So should I then do?</p>
  
  <pre><code>cos-1-distance.cdist(a.T,a.T,'cosine') 
  </code></pre>
  
  <p>Please advise. </p>
  
  <p>II)</p>
  
  <p>Also what If I try doing something like this: </p>
  
  <pre><code>cos=(np.dot(a.T,a))/(np.linalg.norm(a, axis=0, keepdims=True))*(np.linalg.norm(a, axis=0, keepdims=True))
  </code></pre>
  
  <p>It won't work as some problem in getting the right L2 norm of the right column. Any idea how we can implement this without function?</p>