def _scale_normalize(X):
 X = make_nonnegative(X)
 row_diag = np.asarray((1.0 / np.sqrt(X.sum(axis=1)))).squeeze()
 col_diag = np.asarray((1.0 / np.sqrt(X.sum(axis=0)))).squeeze()
 row_diag = np.where(np.isnan(row_diag), 0, row_diag)
 col_diag = np.where(np.isnan(col_diag), 0, col_diag)
 if issparse(X):
  (n_rows, n_cols) = X.shape
  r = dia_matrix((row_diag, [0]), shape=(n_rows, n_rows))
  c = dia_matrix((col_diag, [0]), shape=(n_cols, n_cols))
  an = ((r * X) * c)
 else:
  an = ((row_diag[:, np.newaxis] * X) * col_diag)
 return (an, row_diag, col_diag)

def _scale_normalize ( X ) : X = make_nonnegative ( X ) row_diag = np . asarray ( ( 1.0 / np . sqrt ( X . sum ( axis = 1 ) ) ) ) . squeeze ( ) col_diag = np . asarray ( ( 1.0 / np . sqrt ( X . sum ( axis = 0 ) ) ) ) . squeeze ( ) row_diag = np . where ( np . isnan ( row_diag ) , 0 , row_diag ) col_diag = np . where ( np . isnan ( col_diag ) , 0 , col_diag ) if issparse ( X ) : ( n_rows , n_cols ) = X . shape r = dia_matrix ( ( row_diag , [ 0 ] ) , shape = ( n_rows , n_rows ) ) c = dia_matrix ( ( col_diag , [ 0 ] ) , shape = ( n_cols , n_cols ) ) an = ( ( r * X ) * c ) else : an = ( ( row_diag [ : , np . newaxis ] * X ) * col_diag ) return ( an , row_diag , col_diag )
normalize x by scaling rows and columns independently .