def density(w, **kwargs):
 if hasattr(w, 'toarray'):
  d = (float(w.nnz) / (w.shape[0] * w.shape[1]))
 else:
  d = (0 if (w is None) else (float((w != 0).sum()) / w.size))
 return d

def density ( w , ** kwargs ) : if hasattr ( w , 'toarray' ) : d = ( float ( w . nnz ) / ( w . shape [ 0 ] * w . shape [ 1 ] ) ) else : d = ( 0 if ( w is None ) else ( float ( ( w != 0 ) . sum ( ) ) / w . size ) ) return d
return the density of a graph .