<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import <EOL> import random <EOL> import numpy as np <EOL> from . import mathadapt as ma <EOL> from . compat import range <EOL> def sparsify_columns ( arr , n_non_zero , keep_diagonal = False , random_state = None ) : <EOL> """<STR_LIT>""" <EOL> colsize = arr . shape [ <NUM_LIT:0> ] <EOL> arr_np = arr if isinstance ( arr , np . ndarray ) else arr . as_numpy_array ( ) <EOL> mask = np . ones_like ( arr_np ) <EOL> for i in range ( arr . shape [ <NUM_LIT:1> ] ) : <EOL> idxs = range ( colsize ) <EOL> if random_state is None : <EOL> zeros = random . sample ( idxs , colsize - n_non_zero ) <EOL> else : <EOL> zeros = random_state . choice ( idxs , colsize - n_non_zero , <EOL> replace = False ) <EOL> mask [ zeros , i ] *= <NUM_LIT:0> <EOL> if keep_diagonal and arr . shape [ <NUM_LIT:0> ] == arr . shape [ <NUM_LIT:1> ] : <EOL> mask += np . eye ( arr . shape [ <NUM_LIT:0> ] ) <EOL> arr *= mask <EOL> def bound_spectral_radius ( arr , bound = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> vals , vecs = np . linalg . eigh ( ma . assert_numpy ( arr ) ) <EOL> vals /= abs ( vals ) . max ( ) <EOL> vals *= bound <EOL> arr [ ... ] = np . dot ( vecs , np . dot ( np . diag ( vals ) , vecs . T ) ) <EOL> def randomize_normal ( arr , loc = <NUM_LIT:0> , scale = <NUM_LIT:1> , random_state = None ) : <EOL> """<STR_LIT>""" <EOL> rng = np . random if random_state is None else random_state <EOL> sample = rng . normal ( loc , scale , arr . shape ) <EOL> if isinstance ( arr , np . ndarray ) : <EOL> arr [ ... ] = sample . astype ( arr . dtype ) <EOL> else : <EOL> arr [ : ] = sample . astype ( '<STR_LIT>' ) </s>
