<s> """<STR_LIT>""" <EOL> from __future__ import unicode_literals <EOL> import random <EOL> import numpy as np <EOL> from libact . utils import zip <EOL> class Dataset ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , X = [ ] , y = [ ] ) : <EOL> self . data = list ( zip ( X , y ) ) <EOL> self . modified = True <EOL> self . _update_callback = set ( ) <EOL> def __len__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return len ( self . data ) <EOL> def len_labeled ( self ) : <EOL> """<STR_LIT>""" <EOL> return len ( self . get_labeled_entries ( ) ) <EOL> def len_unlabeled ( self ) : <EOL> """<STR_LIT>""" <EOL> return len ( list ( filter ( lambda entry : entry [ <NUM_LIT:1> ] is None , self . data ) ) ) <EOL> def get_num_of_labels ( self ) : <EOL> """<STR_LIT>""" <EOL> return len ( { entry [ <NUM_LIT:1> ] for entry in self . data if entry [ <NUM_LIT:1> ] is not None } ) <EOL> def append ( self , feature , label = None ) : <EOL> """<STR_LIT>""" <EOL> self . data . append ( ( feature , label ) ) <EOL> self . modified = True <EOL> return len ( self . data ) - <NUM_LIT:1> <EOL> def update ( self , entry_id , new_label ) : <EOL> """<STR_LIT>""" <EOL> self . data [ entry_id ] = ( self . data [ entry_id ] [ <NUM_LIT:0> ] , new_label ) <EOL> self . modified = True <EOL> for callback in self . _update_callback : <EOL> callback ( entry_id , new_label ) <EOL> def on_update ( self , callback ) : <EOL> """<STR_LIT>""" <EOL> self . _update_callback . add ( callback ) <EOL> def format_sklearn ( self ) : <EOL> """<STR_LIT>""" <EOL> X , y = zip ( * self . get_labeled_entries ( ) ) <EOL> return np . array ( X ) , np . array ( y ) <EOL> def get_entries ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . data <EOL> def get_labeled_entries ( self ) : <EOL> """<STR_LIT>""" <EOL> return list ( filter ( lambda entry : entry [ <NUM_LIT:1> ] is not None , self . data ) ) <EOL> def get_unlabeled_entries ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ <EOL> ( idx , entry [ <NUM_LIT:0> ] ) for idx , entry in enumerate ( self . data ) <EOL> if entry [ <NUM_LIT:1> ] is None <EOL> ] <EOL> def labeled_uniform_sample ( self , sample_size , replace = True ) : <EOL> """<STR_LIT>""" <EOL> if replace : <EOL> samples = [ <EOL> random . choice ( self . get_labeled_entries ( ) ) <EOL> for _ in range ( sample_size ) <EOL> ] <EOL> else : <EOL> samples = random . sample ( self . get_labeled_entries ( ) , sample_size ) <EOL> return Dataset ( * zip ( * samples ) ) <EOL> def import_libsvm_sparse ( filename ) : <EOL> """<STR_LIT>""" <EOL> from sklearn . datasets import load_svmlight_file <EOL> X , y = load_svmlight_file ( filename ) <EOL> return Dataset ( X . toarray ( ) . tolist ( ) , y . tolist ( ) ) <EOL> def import_scipy_mat ( filename ) : <EOL> from scipy . io import loadmat <EOL> data = loadmat ( filename ) <EOL> X = data [ '<STR_LIT:X>' ] <EOL> y = data [ '<STR_LIT:y>' ] <EOL> zipper = list ( zip ( X , y ) ) <EOL> np . random . shuffle ( zipper ) <EOL> X , y = zip ( * zipper ) <EOL> X , y = np . array ( X ) , np . array ( y ) . reshape ( - <NUM_LIT:1> ) <EOL> return Dataset ( X , y ) </s>
