code: def attribute_ac(M):
      """Compute assortativity for attribute matrix M.
  
      Parameters
      ----------
      M : numpy.ndarray
          2D ndarray representing the attribute mixing matrix.
  
      Notes
      -----
      This computes Eq. (2) in Ref. [1]_ , (trace(e)-sum(e^2))/(1-sum(e^2)),
      where e is the joint probability distribution (mixing matrix)
      of the specified attribute.
  
      References
      ----------
      .. [1] M. E. J. Newman, Mixing patterns in networks,
         Physical Review E, 67 026126, 2003
      """
      if M.sum() != 1.0:
          M = M / M.sum()
      s = (M @ M).sum()
      t = M.trace()
      r = (t - s) / (1 - s)
      return r
  
file_path: networkx/networkx/algorithms/assortativity/correlation.py
imports: import networkx
  from networkx.algorithms.assortativity.mixing import attribute_mixing_matrix, degree_mixing_matrix
  from networkx.algorithms.assortativity.pairs import node_degree_xy
label_desc: Use image node_id 5 for calling a global function with example usage: attribute_ac(M) and returns: r