<s> __author__ = "<STR_LIT>" <EOL> __version__ = '<STR_LIT>' <EOL> import numpy as np <EOL> from numpy . random import multivariate_normal , rand <EOL> from scipy import diag <EOL> from pylab import show , hold , plot <EOL> from pybrain . datasets import ClassificationDataSet , SequenceClassificationDataSet <EOL> def generateClassificationData ( size , nClasses = <NUM_LIT:3> ) : <EOL> """<STR_LIT>""" <EOL> if nClasses == <NUM_LIT:3> : <EOL> means = [ ( - <NUM_LIT:1> , <NUM_LIT:0> ) , ( <NUM_LIT:2> , <NUM_LIT:4> ) , ( <NUM_LIT:3> , <NUM_LIT:1> ) ] <EOL> else : <EOL> means = [ ( - <NUM_LIT:2> , <NUM_LIT:0> ) , ( <NUM_LIT:2> , <NUM_LIT:1> ) , ( <NUM_LIT:6> , <NUM_LIT:0> ) ] <EOL> cov = [ diag ( [ <NUM_LIT:1> , <NUM_LIT:1> ] ) , diag ( [ <NUM_LIT:0.5> , <NUM_LIT> ] ) , diag ( [ <NUM_LIT> , <NUM_LIT> ] ) ] <EOL> dataset = ClassificationDataSet ( <NUM_LIT:2> , <NUM_LIT:1> , nb_classes = nClasses ) <EOL> for _ in range ( size ) : <EOL> for c in range ( <NUM_LIT:3> ) : <EOL> input = multivariate_normal ( means [ c ] , cov [ c ] ) <EOL> dataset . addSample ( input , [ c % nClasses ] ) <EOL> dataset . assignClasses ( ) <EOL> return dataset <EOL> def generateGridData ( x , y , return_ticks = False ) : <EOL> """<STR_LIT>""" <EOL> x = np . arange ( x [ <NUM_LIT:0> ] , x [ <NUM_LIT:1> ] , x [ <NUM_LIT:2> ] ) <EOL> y = np . arange ( y [ <NUM_LIT:0> ] , y [ <NUM_LIT:1> ] , y [ <NUM_LIT:2> ] ) <EOL> X , Y = np . meshgrid ( x , y ) <EOL> shape = X . shape <EOL> ds = ClassificationDataSet ( <NUM_LIT:2> , <NUM_LIT:1> ) <EOL> ds . setField ( '<STR_LIT:input>' , np . concatenate ( ( X . reshape ( X . size , <NUM_LIT:1> ) , Y . reshape ( X . size , <NUM_LIT:1> ) ) , <NUM_LIT:1> ) ) <EOL> ds . setField ( '<STR_LIT:target>' , np . zeros ( [ X . size , <NUM_LIT:1> ] ) ) <EOL> ds . _convertToOneOfMany ( ) <EOL> if return_ticks : <EOL> return ( ds , x , y ) <EOL> else : <EOL> return ( ds , X , Y ) <EOL> def generateNoisySines ( npoints , nseq , noise = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> x = np . arange ( npoints ) / float ( npoints ) * <NUM_LIT> <EOL> y1 = np . sin ( x + rand ( <NUM_LIT:1> ) * <NUM_LIT> ) <EOL> y2 = np . sin ( x / <NUM_LIT> + rand ( <NUM_LIT:1> ) * <NUM_LIT> ) <EOL> DS = SequenceClassificationDataSet ( <NUM_LIT:1> , <NUM_LIT:1> , nb_classes = <NUM_LIT:2> ) <EOL> for _ in range ( nseq ) : <EOL> DS . newSequence ( ) <EOL> buf = rand ( npoints ) * noise + y1 + ( rand ( <NUM_LIT:1> ) - <NUM_LIT:0.5> ) * noise <EOL> for i in range ( npoints ) : <EOL> DS . addSample ( [ buf [ i ] ] , [ <NUM_LIT:0> ] ) <EOL> DS . newSequence ( ) <EOL> buf = rand ( npoints ) * noise + y2 + ( rand ( <NUM_LIT:1> ) - <NUM_LIT:0.5> ) * noise <EOL> for i in range ( npoints ) : <EOL> DS . addSample ( [ buf [ i ] ] , [ <NUM_LIT:1> ] ) <EOL> return DS <EOL> def plotData ( ds ) : <EOL> hold ( True ) <EOL> for c in range ( ds . nClasses ) : <EOL> here , _ = np . where ( ds [ '<STR_LIT:class>' ] == c ) <EOL> plot ( ds [ '<STR_LIT:input>' ] [ here , <NUM_LIT:0> ] , ds [ '<STR_LIT:input>' ] [ here , <NUM_LIT:1> ] , '<STR_LIT:o>' ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> plotData ( generateClassificationData ( <NUM_LIT> ) ) <EOL> show ( ) </s>
