Saving and loading a model: Difference between revisions

From Jstacs
Jump to navigationJump to search
No edit summary
No edit summary
 
Line 1: Line 1:
<source lang="java5">
<source lang="java5">
//create a Sample for each class from the input data, using the DNA alphabet
Sample[] data = new Sample[2];
data[0] = new DNASample( args[0] );


//create a DNA-alphabet
AlphabetContainer container = new AlphabetContainer( new DNAAlphabet() );
//the length of our input sequences
//the length of our input sequences
int length = 7;
int length = data[0].getElementLength();
 
//create a Sample for each class from the input data, using the alphabet from above
Sample[] data = new Sample[]{ new Sample( container, new StringExtractor( new File(args[0]), 100) ),
                            new Sample( container, new StringExtractor( new File(args[1]), 100 ), length ) };


data[1] = new Sample( new DNASample( args[1] ), length );
//create a new PWM
//create a new PWM
BayesianNetworkModel pwm = new BayesianNetworkModel( new BayesianNetworkModelParameterSet(
BayesianNetworkModel pwm = new BayesianNetworkModel( new BayesianNetworkModelParameterSet(
//the alphabet and the length of the model:
//the alphabet and the length of the model:
container, length,  
data[0].getAlphabetContainer(), length,  
//the equivalent sample size to compute hyper-parameters
//the equivalent sample size to compute hyper-parameters
4,  
4,  
Line 22: Line 21:
//we want to estimate the MAP-parameters
//we want to estimate the MAP-parameters
LearningType.ML_OR_MAP ) );
LearningType.ML_OR_MAP ) );
 
//create a new classifier
//create a new classifier
ModelBasedClassifier classifier = new ModelBasedClassifier( pwm, pwm );
ModelBasedClassifier classifier = new ModelBasedClassifier( pwm, pwm );
 
//train the classifier
//train the classifier
classifier.train( data );
classifier.train( data );
 
//create the XML-representation of the classifier
//create the XML-representation of the classifier
StringBuffer buf = classifier.toXML();
StringBuffer buf = classifier.toXML();
 
//write it to disk
//write it to disk
FileManager.writeFile( new File("myClassifier.xml"), buf );
FileManager.writeFile( new File(home+"myClassifier.xml"), buf );
 
//read XML-representation from disk
//read XML-representation from disk
StringBuffer buf2 = FileManager.readFile( new File("myClassifier.xml") );
StringBuffer buf2 = FileManager.readFile( new File(home+"myClassifier.xml") );
 
//create new classifier from read StringBuffer containing XML-code
//create new classifier from read StringBuffer containing XML-code
ModelBasedClassifier loaded = new ModelBasedClassifier(buf2);
ModelBasedClassifier loaded = new ModelBasedClassifier(buf2);
</source>
</source>

Latest revision as of 14:26, 2 December 2009

//create a Sample for each class from the input data, using the DNA alphabet
Sample[] data = new Sample[2];
data[0] = new DNASample( args[0] );

//the length of our input sequences
int length = data[0].getElementLength();

data[1] = new Sample( new DNASample( args[1] ), length );
 
//create a new PWM
BayesianNetworkModel pwm = new BayesianNetworkModel( new BayesianNetworkModelParameterSet(
		//the alphabet and the length of the model:
		data[0].getAlphabetContainer(), length, 
		//the equivalent sample size to compute hyper-parameters
		4, 
		//some identifier for the model
		"my PWM", 
		//we want a PWM, which is an inhomogeneous Markov model (IMM) of order 0
		ModelType.IMM, (byte) 0, 
		//we want to estimate the MAP-parameters
		LearningType.ML_OR_MAP ) );
 
//create a new classifier
ModelBasedClassifier classifier = new ModelBasedClassifier( pwm, pwm );
 
//train the classifier
classifier.train( data );
 
//create the XML-representation of the classifier
StringBuffer buf = classifier.toXML();
 
//write it to disk
FileManager.writeFile( new File(home+"myClassifier.xml"), buf );
 
//read XML-representation from disk
StringBuffer buf2 = FileManager.readFile( new File(home+"myClassifier.xml") );
 
//create new classifier from read StringBuffer containing XML-code
ModelBasedClassifier loaded = new ModelBasedClassifier(buf2);