title: Mean of non-diagonal elements of each row numpy
body: <p>I essentially have a confusion matrix of size <code>n x n</code> with all my diagonal elements being <code>1</code>. </p>
  
  <p>For every row, I wish to calculate its mean, excluding the <code>1</code>, i.e. excluding the diagonal value. Is there a simple way to do it in <code>numpy</code>?</p>
  
  <p>This is my current solution:</p>
  
  <pre><code>mask = np.zeros(cs.shape, dtype=bool)
  np.fill_diagonal(mask, 1)
  print(np.ma.masked_array(cs, mask).mean(axis=1))
  </code></pre>
  
  <p>where <code>cs</code> is my <code>n x n</code> matrix</p>
  
  <p>The code seems convoluted, and I certainly feel that there's a much more elegant solution.</p>