<s> """<STR_LIT>""" <EOL> import math , random , sys , bisect , time <EOL> import numpy , scipy . spatial . distance <EOL> from scipy . spatial import cKDTree <EOL> import cProfile , pstats , gendata <EOL> import numpy as np <EOL> class kdtknn ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , k = <NUM_LIT:3> , method = '<STR_LIT>' , leafsize = <NUM_LIT:10> ) : <EOL> """<STR_LIT>""" <EOL> self . leafsize = leafsize <EOL> self . data = None <EOL> self . kdt = None <EOL> self . rebuild_tree = True <EOL> self . k = k <EOL> self . method = method <EOL> def addEvidence ( self , dataX , dataY = None ) : <EOL> """<STR_LIT>""" <EOL> '''<STR_LIT>''' <EOL> if not dataY == None : <EOL> data = numpy . zeros ( [ dataX . shape [ <NUM_LIT:0> ] , dataX . shape [ <NUM_LIT:1> ] + <NUM_LIT:1> ] ) <EOL> data [ : , <NUM_LIT:0> : dataX . shape [ <NUM_LIT:1> ] ] = dataX <EOL> data [ : , ( dataX . shape [ <NUM_LIT:1> ] ) ] = dataY <EOL> else : <EOL> data = dataX <EOL> self . rebuild_tree = True <EOL> if self . data is None : <EOL> self . data = data <EOL> else : <EOL> self . data = numpy . append ( self . data , data , axis = <NUM_LIT:0> ) <EOL> def rebuildKDT ( self ) : <EOL> """<STR_LIT>""" <EOL> self . kdt = cKDTree ( self . data [ : , : - <NUM_LIT:1> ] , leafsize = self . leafsize ) <EOL> self . rebuild_tree = False <EOL> def query ( self , points , k = None , method = None ) : <EOL> """<STR_LIT>""" <EOL> if k is None : <EOL> k = self . k <EOL> if method is None : <EOL> method = self . method <EOL> if self . rebuild_tree is True : <EOL> if self . data is None : <EOL> return None <EOL> self . rebuildKDT ( ) <EOL> na_dist , na_neighbors = self . kdt . query ( points , k ) <EOL> if k == <NUM_LIT:1> : <EOL> na_dist = na_dist . reshape ( - <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> na_neighbors = na_neighbors . reshape ( - <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> n_clsses = map ( lambda rslt : map ( lambda p : p [ - <NUM_LIT:1> ] , self . data [ rslt ] ) , na_neighbors ) <EOL> if method == '<STR_LIT>' : <EOL> return map ( lambda x : scipy . stats . stats . mode ( x ) [ <NUM_LIT:0> ] , n_clsses ) [ <NUM_LIT:0> ] <EOL> elif method == '<STR_LIT>' : <EOL> return numpy . array ( map ( lambda x : numpy . mean ( x ) , n_clsses ) ) <EOL> elif method == '<STR_LIT>' : <EOL> return numpy . array ( map ( lambda x : numpy . median ( x ) , n_clsses ) ) <EOL> elif method == '<STR_LIT>' : <EOL> return numpy . array ( n_clsses ) <EOL> elif method == '<STR_LIT:all>' : <EOL> return numpy . array ( n_clsses ) , na_dist <EOL> def getflatcsv ( fname ) : <EOL> inf = open ( fname ) <EOL> return numpy . array ( [ map ( float , s . strip ( ) . split ( '<STR_LIT:U+002C>' ) ) for s in inf . readlines ( ) ] ) <EOL> def testgendata ( ) : <EOL> fname = '<STR_LIT>' <EOL> querys = <NUM_LIT:1000> <EOL> d = <NUM_LIT:2> <EOL> k = <NUM_LIT:3> <EOL> bnds = ( ( - <NUM_LIT:10> , <NUM_LIT:10> ) , ) * d <EOL> clsses = ( <NUM_LIT:0> , <NUM_LIT:1> ) <EOL> data = getflatcsv ( fname ) <EOL> kdt = kdtknn ( k , method = '<STR_LIT>' ) <EOL> kdt . addEvidence ( data ) <EOL> kdt . rebuildKDT ( ) <EOL> stime = time . time ( ) <EOL> for x in xrange ( querys ) : <EOL> pnt = numpy . array ( gendata . gensingle ( d , bnds , clsses ) ) <EOL> reslt = kdt . query ( numpy . array ( [ pnt [ : - <NUM_LIT:1> ] ] ) ) <EOL> print pnt , "<STR_LIT>" , reslt <EOL> etime = time . time ( ) <EOL> print etime - stime , '<STR_LIT:/>' , querys , '<STR_LIT:=>' , ( etime - stime ) / float ( querys ) , '<STR_LIT>' <EOL> def test ( ) : <EOL> testgendata ( ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> test ( ) </s>
