/** * serializes the given object to the specified file. * * @param filename the file to write the object to * @param o the object to serialize * @throws Exception if serialization fails */ public static void write(String filename, Object o) throws Exception { write(new FileOutputStream(filename), o); }
/** * serializes the given object to the specified file. * * @param filename the file to write the object to * @param o the object to serialize * @throws Exception if serialization fails */ public static void write(String filename, Object o) throws Exception { write(new FileOutputStream(filename), o); }
public Classifier train(Instances data, File model, List<String> parameters) throws Exception { String algoName = parameters.get(0); List<String> algoParameters = parameters.subList(1, parameters.size()); // build classifier Classifier cl = AbstractClassifier.forName(algoName, algoParameters.toArray(new String[0])); cl.buildClassifier(data); weka.core.SerializationHelper.write(model.getAbsolutePath(), cl); return cl; }
public Classifier train(Instances data, File model, List<String> parameters) throws Exception { String algoName = parameters.get(0); List<String> algoParameters = parameters.subList(1, parameters.size()); // build classifier Classifier cl = AbstractClassifier.forName(algoName, algoParameters.toArray(new String[0])); cl.buildClassifier(data); weka.core.SerializationHelper.write(model.getAbsolutePath(), cl); return cl; }
/** * Writes and experiment to disk. * * @param exp the experiment to save * @param file the file to save to * @return null if successful, otherwise error message */ @Override public String write(Experiment exp, File file) { String result; result = null; try { SerializationHelper.write(file.getAbsolutePath(), exp); } catch (Exception e) { result = handleException("Failed to write experiment to: " + file, e); } return result; } }
/** * Writes and experiment to disk. * * @param exp the experiment to save * @param file the file to save to * @return null if successful, otherwise error message */ @Override public String write(Experiment exp, File file) { String result; result = null; try { SerializationHelper.write(file.getAbsolutePath(), exp); } catch (Exception e) { result = handleException("Failed to write experiment to: " + file, e); } return result; } }
/** * Write classifier into a file * * @param cls classifier * @param filename name (with complete path) of the destination file * @return false if error */ public static boolean writeClassifier(AbstractClassifier cls, String filename) { try { SerializationHelper.write(filename, cls); } catch (Exception e) { IJ.log("Error while writing classifier into a file"); e.printStackTrace(); return false; } return true; }
/** * Write classifier into a file * * @param cls classifier * @param filename name (with complete path) of the destination file * @return false if error */ public static boolean writeClassifier(AbstractClassifier cls, String filename) { try { SerializationHelper.write(filename, cls); } catch (Exception e) { IJ.log("Error while writing classifier into a file"); e.printStackTrace(); return false; } return true; }
/** * writes the serialized object to the speicified file * * @param filename the file to serialize the object to * @param o the object to serialize * @return true if writing was successful */ public static boolean saveToFile(String filename, Object o) { boolean result; if (SerializationHelper.isSerializable(o.getClass())) { try { SerializationHelper.write(filename, o); result = true; } catch (Exception e) { result = false; } } else { result = false; } return result; }
public Classifier train(Instances data, File model, List<String> parameters) throws Exception { List<String> mlArgs = parameters.subList(1, parameters.size()); MultiLabelClassifier cl = (MultiLabelClassifier) AbstractClassifier .forName((String) parameters.get(0), new String[] {}); if (!mlArgs.isEmpty()) { cl.setOptions(mlArgs.toArray(new String[0])); } cl.buildClassifier(data); if (serializeModel) { weka.core.SerializationHelper.write(model.getAbsolutePath(), cl); } return cl; }
/** * writes the serialized object to the speicified file * * @param filename the file to serialize the object to * @param o the object to serialize * @return true if writing was successful */ public static boolean saveToFile(String filename, Object o) { boolean result; if (SerializationHelper.isSerializable(o.getClass())) { try { SerializationHelper.write(filename, o); result = true; } catch (Exception e) { result = false; } } else { result = false; } return result; }
private void createWekaEvaluationObject(Classifier classifier, File evalOutput, Instances trainData, Instances testData) throws Exception { Evaluation eval = new Evaluation(trainData); eval.evaluateModel(classifier, testData); weka.core.SerializationHelper.write(evalOutput.getAbsolutePath(), eval); }
public Classifier train(Instances data, File model, List<String> parameters) throws Exception { List<String> mlArgs = parameters.subList(1, parameters.size()); MultiLabelClassifier cl = (MultiLabelClassifier) AbstractClassifier .forName((String) parameters.get(0), new String[] {}); if (!mlArgs.isEmpty()) { cl.setOptions(mlArgs.toArray(new String[0])); } cl.buildClassifier(data); if (serializeModel) { weka.core.SerializationHelper.write(model.getAbsolutePath(), cl); } return cl; }
protected void createWekaEvaluationObject(Classifier classifier, File evalOutput, Instances trainData, Instances testData) throws Exception { Evaluation eval = new Evaluation(trainData); eval.evaluateModel(classifier, testData); weka.core.SerializationHelper.write(evalOutput.getAbsolutePath(), eval); }
/** * Stores the given statistics. * * @param stats the statistics to store * @return null if successfully stored, otherwise error message */ @Override public String write(List<EvaluationStatistics> stats) { log("Writing " + stats.size() + " statistics to: " + m_File); try { SerializationHelper.write(m_File.getAbsolutePath(), stats); return null; } catch (Exception e) { return handleException("Failed to write statistics to: " + m_File, e); } }
/** * Stores the given statistics. * * @param stats the statistics to store * @return null if successfully stored, otherwise error message */ @Override public String write(List<EvaluationStatistics> stats) { log("Writing " + stats.size() + " statistics to: " + m_File); try { SerializationHelper.write(m_File.getAbsolutePath(), stats); return null; } catch (Exception e) { return handleException("Failed to write statistics to: " + m_File, e); } }
@Override public void storeClassifierModel(File path) throws ClassifierException { if (!modelReady) { throw new ClassifierException("The classifier is not ready for classification; either training, or loading model should be done before calling classify"); } // Okay, store the model in the given path. try { weka.core.SerializationHelper.write(path.getAbsolutePath(), classifier); } catch (Exception e) { throw new ClassifierException("Serializing the trainined Weka classifier model failed, Weka serializationHelper raised an exception: ", e); } }
/** * Train the required classifier with generated Weka ARFF file. * */ private void trainClassifier() { for (int i = 0; i < numOfModelFiles; i++) { Instances data; try { data = loadInstancesFromARFF(wekaArffFile + String.valueOf(i) + ".arff", "class"); java.util.Random rand = new java.util.Random(); data.randomize(rand); // classifier = new LibLINEAR(); // ((LibLINEAR) classifier).setSVMType(new SelectedTag( // LibLINEAR.SVMTYPE_L2_LR, LibLINEAR.TAGS_SVMTYPE)); classifier.buildClassifier(data); // serialize model weka.core.SerializationHelper.write( modelFile + String.valueOf(i), classifier); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
/** * Train the classifier */ protected void trainClassifier(DataSet dataSet) throws Exception { try { //building the classifier this.classifier.buildClassifier(dataSet.getData()); //storing the trained classifier to a file for future use weka.core.SerializationHelper.write(this.classifierModel, this.classifier); //save the list of the features with their index in a file to be used //during the test phase (see the process method) //saveFeaturesList(trainingDataSet); saveFeaturesSet(); //save the list of the classes and their index in a file to be used //during the test phase (see the process method) saveClasses(); } catch (Exception ex) { throw new Exception("Training classifier error:" + ex.getMessage()); } }
s.write("SMO_samp.model", smo); Classifier c = new SMO(); c = (Classifier) s.read("SMO_Samp.model");