#NEWLINE# def sample( self , y ) : #NEWLINE# #INDENT# ( m , n ) = y . shape #NEWLINE# num = np . asarray ( y . sum ( 1 ) ) [ : , 0 ] #NEWLINE# num . astype ( np . int32 ) #NEWLINE# num *= self . ratio #NEWLINE# nonzero = y . nonzero ( ) #NEWLINE# total = np . sum ( num ) #NEWLINE# cols = np . random . random ( total ) #NEWLINE# cols *= n #NEWLINE# cols = cols . astype ( np . int32 ) . tolist ( ) #NEWLINE# cols += nonzero [ 1 ] . tolist ( ) #NEWLINE# rows = np . zeros ( total ) #NEWLINE# pre = 0 #NEWLINE# for i in xrange ( m ) : #NEWLINE# #INDENT# after = ( pre + num [ i ] ) #NEWLINE# rows [ pre : after ] = i #NEWLINE# pre = after #NEWLINE# #UNINDENT# rows = rows . astype ( np . int32 ) . tolist ( ) #NEWLINE# rows += nonzero [ 0 ] . tolist ( ) #NEWLINE# vals = np . ones ( len ( rows ) ) . tolist ( ) #NEWLINE# sample = sp . csr_matrix ( ( vals , ( rows , cols ) ) , ( m , m ) ) #NEWLINE# return sample