本文整理汇总了Java中weka.core.SerializationHelper.write方法的典型用法代码示例。如果您正苦于以下问题:Java SerializationHelper.write方法的具体用法?Java SerializationHelper.write怎么用?Java SerializationHelper.write使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类weka.core.SerializationHelper
的用法示例。
在下文中一共展示了SerializationHelper.write方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: train
import weka.core.SerializationHelper; //导入方法依赖的package包/类
public void train(String datasetFilename, boolean serialise)
{
String[] lines = Utils.readLines(datasetFilename);
int i = 1; // skip legend
try
{
for(i = 1; i < lines.length; i++) // skip legend
{
dataset.add(createFeatureVector(lines[i].split(","), true));
}
model = (Classifier) new LinearRegression();
((LinearRegression)model).setRidge(1.0e-10);
model.buildClassifier(dataset);
if(serialise)
{
SerializationHelper.write(new FileOutputStream(modelFilename), model);
}
}
catch(Exception e)
{
System.err.println("Error in line " + i + ": " + lines[i]);
e.printStackTrace();
}
}
示例2: train
import weka.core.SerializationHelper; //导入方法依赖的package包/类
@Override
public void train(List<MLExample> pTrainExamples) throws Exception {
ConfigurationUtil.TrainingMode = true;
setPaths();
//This part added since the session was so slow
List<Integer> train_example_ids = new ArrayList<Integer>();
for(MLExample example : pTrainExamples)
{
train_example_ids.add(example.getExampleId());
}
WekaFormatConvertor.writeToFile(train_example_ids, trainFile,taskName, new String[]{"1", "2"});
DataSource source = new DataSource(trainFile);
Instances data = source.getDataSet();
// setting class attribute if the data format does not provide this information
if (data.classIndex() == -1)
data.setClassIndex(data.numAttributes() - 1);
if(options!=null)
wekaAlgorithm.setOptions(options); // set the options
wekaAlgorithm.buildClassifier(data); // build classifier
// serialize model
SerializationHelper.write(modelFile, wekaAlgorithm);
}
示例3: saveModel
import weka.core.SerializationHelper; //导入方法依赖的package包/类
public void saveModel(Classifier model, String modelpath) {
try {
SerializationHelper.write(modelpath, model);
} catch (Exception ex) {
Logger.getLogger(ModelGenerator.class.getName()).log(Level.SEVERE, null, ex);
}
}
示例4: trainAndSerializeClassifier
import weka.core.SerializationHelper; //导入方法依赖的package包/类
/**
* creates a classifier, trains and serializes it
*
* @param data the data to use (J48 with nominal class, M5P with
* numeric class)
* @return the results for the data
*/
protected double[] trainAndSerializeClassifier(Instances data) {
Classifier classifier;
double[] result;
int i;
try {
// build
if (data.classAttribute().isNominal())
classifier = new weka.classifiers.trees.J48();
else
classifier = new weka.classifiers.trees.M5P();
classifier.buildClassifier(data);
// record predictions
result = new double[data.numInstances()];
for (i = 0; i < result.length; i++)
result[i] = classifier.classifyInstance(data.instance(i));
// save
SerializationHelper.write(MODEL_FILENAME, classifier);
}
catch (Exception e) {
fail("Training base classifier failed: " + e);
return null;
}
return result;
}
示例5: trainAndSerializeClassifier
import weka.core.SerializationHelper; //导入方法依赖的package包/类
/**
* creates a classifier, trains and serializes it
*
* @param data the data to use (J48 with nominal class, M5P with numeric
* class)
* @return the results for the data
*/
protected double[] trainAndSerializeClassifier(Instances data) {
Classifier classifier;
double[] result;
int i;
try {
// build
if (data.classAttribute().isNominal()) {
classifier = new weka.classifiers.trees.J48();
} else {
classifier = new weka.classifiers.trees.M5P();
}
classifier.buildClassifier(data);
// record predictions
result = new double[data.numInstances()];
for (i = 0; i < result.length; i++) {
result[i] = classifier.classifyInstance(data.instance(i));
}
// save
SerializationHelper.write(MODEL_FILENAME, classifier);
} catch (Exception e) {
fail("Training base classifier failed: " + e);
return null;
}
return result;
}
示例6: forwardlinkTraining
import weka.core.SerializationHelper; //导入方法依赖的package包/类
public synchronized LinkClassifier forwardlinkTraining(Set<String> relUrls, int levels,
String className) throws Exception {
List<Sampler<LinkNeighborhood>> instances = loadTrainingInstances(relUrls, levels);
String wekaInputAsString = createWekaInput(instances, false);
logger.info("Training new link classifier...");
Classifier classifier = trainWekaClassifier(wekaInputAsString);
String modelFile = linkClassifierFolder.resolve("link_classifier.model").toString();
String featuresFile = linkClassifierFolder.resolve("link_classifier.features").toString();
logger.info("Link Clasifier model file: "+modelFile);
logger.info("Link Clasifier features file: "+featuresFile);
SerializationHelper.write(modelFile, classifier);
writeFeaturesFile(featuresFile, features);
String[] classValues = null;
if (levels == 0) {
classValues = new String[] {"POS", "NEG"};
} else {
classValues = new String[] {"0", "1", "2"};
}
return createLinkClassifierImpl(features, classValues, classifier, className, levels);
}
示例7: saveSC
import weka.core.SerializationHelper; //导入方法依赖的package包/类
public Classifiers saveSC(String filename1, String filename2, String filename3, String filename4, String filename5) throws Exception
{
SerializationHelper.write(filename1, SCA);
SerializationHelper.write(filename2, SCB);
SerializationHelper.write(filename3, SCC1);
SerializationHelper.write(filename4, SCC2);
SerializationHelper.write(filename5, SCC3);
return this;
}
示例8: writeClassifier
import weka.core.SerializationHelper; //导入方法依赖的package包/类
/**
*
* @param classifier
* @param file
*/
public void writeClassifier(final String file, final String lang) {
final String name = getName(lang);
LOG.info("writeClassifier: " + name);
final String path = FilenameUtils.getPath(name);
try {
FileUtils.forceMkdir(new File(path));
SerializationHelper.write(name, classifier);
} catch (final Exception e) {
LOG.error(e.getLocalizedMessage(), e);
}
}
示例9: main
import weka.core.SerializationHelper; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
String modelName = "models/wiki_scoring_noun10conjarg2_sim5log-gopt_SVMRankC10_RepDisc";
String modelFile = modelName + ".model";
FilteredClassifier clf = (FilteredClassifier) SerializationHelper.read(modelFile);
RankingSVM svm = new RankingSVM();
svm.setWeights(modelName + ".weights");
clf.setClassifier(svm);
System.out.println(clf);
SerializationHelper.write(modelName + ".model_new", clf);
}
示例10: convert
import weka.core.SerializationHelper; //导入方法依赖的package包/类
public void convert() throws Exception {
if( ! new File( getInputModel() ).exists() ) {
throw new FileNotFoundException( "Input model " + getInputModel() + " does not exist!");
}
if( ! new File( getScriptPath() ).exists() ) {
throw new FileNotFoundException( "Python script " + getScriptPath() + " does not exist!");
}
PyScriptClassifier cls = (PyScriptClassifier) SerializationHelper.read( getInputModel() );
System.err.println( cls.getModelString() + "\nCurrent script path: " +
cls.getPythonFile().getAbsolutePath() );
System.err.println("Changing script path to: " + getScriptPath() );
cls.setPythonFile( new File( getScriptPath() ) );
SerializationHelper.write( getOutputModel(), cls);
}
示例11: saveRC
import weka.core.SerializationHelper; //导入方法依赖的package包/类
public Classifiers saveRC(String filename) throws Exception
{
SerializationHelper.write(filename, RC);
return this;
}
示例12: saveYNC
import weka.core.SerializationHelper; //导入方法依赖的package包/类
public Classifiers saveYNC(String filename) throws Exception
{
SerializationHelper.write(filename, YNC);
return this;
}
示例13: saveModel
import weka.core.SerializationHelper; //导入方法依赖的package包/类
public static void saveModel(Classifier model, File dest) throws Exception {
SerializationHelper.write(dest.getAbsolutePath(), model);
}