Example usage for weka.core SerializationHelper read

List of usage examples for weka.core SerializationHelper read

Introduction

In this page you can find the example usage for weka.core SerializationHelper read.

Prototype

public static Object read(InputStream stream) throws Exception 

Source Link

Document

deserializes from the given stream and returns the object from it.

Usage

From source file:homemadeWEKA.java

public static Classifier load_model(String mdl) throws Exception {
    Classifier cls = (Classifier) SerializationHelper.read(mdl);
    return cls;
}

From source file:ann.ANN.java

public Classifier loadModel(String modeladdress) {
    Classifier model = null;//from   w w w  . j  a va 2s. c  o  m
    try {
        model = (Classifier) SerializationHelper.read(modeladdress);
        System.out.println(model.toString());
        System.out.println(modeladdress + " berhasil diload\n");
    } catch (Exception ex) {
        System.out.println(modeladdress + " tidak bisa diload\n");
    }
    return model;
}

From source file:asap.PostProcess.java

/**
 *
 * @param modelsContainerPath/* w  w w  . jav a  2  s  .co m*/
 */
public void loadModels(String modelsContainerPath) {
    PerformanceCounters.startTimer("loadModels");
    System.out.println("Loading weka models...");

    File folder = new File(modelsContainerPath);
    File[] listOfFiles = folder.listFiles(
            //JDK < 8:                
            new FileFilter() {

                @Override
                public boolean accept(File file) {
                    return (file.getName().contains(".model") && !file.getName().contains(".empty"));
                }
            });

    if (listOfFiles == null ? true : listOfFiles.length == 0) {
        System.out.println("\tNo models found. Can't test without prior model building and training!");
        PerformanceCounters.stopTimer("loadModels");
        throw new RuntimeException("Can't test - no models found.");
    }

    Object obj;
    for (File listOfFile : listOfFiles) {
        String modelFilename = listOfFile.getAbsolutePath();
        try {
            obj = SerializationHelper.read(modelFilename);
        } catch (Exception ex) {
            Logger.getLogger(PostProcess.class.getName()).log(Level.SEVERE, null, ex);
            continue;
        }
        if (obj instanceof AbstractClassifier) {
            AbstractClassifier abCl = (AbstractClassifier) obj;
            //                classifiers.add(abCl);

            System.out.println("\tLoaded model : " + abCl.getClass().getName() + " "
                    + Utils.joinOptions(abCl.getOptions()));
        } else {
            System.out.println("\tModel filename given doesn't contain a valid built model!");
        }
    }

    System.out.println("\tdone.");
    PerformanceCounters.stopTimer("loadModels");
}

From source file:clasificacion.Clasificacion.java

public String clasificar(String[] testCases) throws Exception {
    String ruta = "nursery_model.model";

    InputStream classModelStream;
    classModelStream = getClass().getResourceAsStream(ruta);
    //classModel = (Classifier)SerializationHelper.read(classModelStream);
    Classifier clasify = (Classifier) SerializationHelper.read(classModelStream);

    FastVector parents = new FastVector();
    parents.addElement("usual");
    parents.addElement("pretentious");
    parents.addElement("great_pret");
    Attribute _parent = new Attribute("parents", parents);

    FastVector nurs = new FastVector();
    nurs.addElement("proper");
    nurs.addElement("less_proper");
    nurs.addElement("improper");
    nurs.addElement("critical");
    nurs.addElement("very_crit");
    Attribute _has_nurs = new Attribute("has_nurs", nurs);

    FastVector form = new FastVector();
    form.addElement("complete");
    form.addElement("completed");
    form.addElement("incomplete");
    form.addElement("foster");
    Attribute _form = new Attribute("form", form);

    FastVector children = new FastVector();
    children.addElement("1");
    children.addElement("2");
    children.addElement("3");
    children.addElement("more");
    Attribute _children = new Attribute("children", children);

    FastVector housing = new FastVector();
    housing.addElement("convenient");
    housing.addElement("less_conv");
    housing.addElement("critical");
    Attribute _housing = new Attribute("housing", housing);

    FastVector finance = new FastVector();
    finance.addElement("convenient");
    finance.addElement("inconv");
    Attribute _finance = new Attribute("finance", finance);

    FastVector social = new FastVector();
    social.addElement("nonprob");
    social.addElement("slightly_prob");
    social.addElement("problematic");
    Attribute _social = new Attribute("social", social);

    FastVector health = new FastVector();
    health.addElement("recommended");
    health.addElement("priority");
    health.addElement("not_recom");
    Attribute _health = new Attribute("health", health);

    FastVector Class = new FastVector();
    Class.addElement("not_recom");
    Class.addElement("recommend");
    Class.addElement("very_recom");
    Class.addElement("priority");
    Class.addElement("spec_prior");
    Attribute _Class = new Attribute("class", Class);

    FastVector atributos = new FastVector(9);
    atributos.addElement(_parent);//from w  w w  . j  av  a 2s  .c om
    atributos.addElement(_has_nurs);
    atributos.addElement(_form);
    atributos.addElement(_children);
    atributos.addElement(_housing);
    atributos.addElement(_finance);
    atributos.addElement(_social);
    atributos.addElement(_health);
    atributos.addElement(_Class);

    ArrayList<Attribute> atributs = new ArrayList<>();
    atributs.add(_parent);
    atributs.add(_has_nurs);
    atributs.add(_form);
    atributs.add(_children);
    atributs.add(_housing);
    atributs.add(_finance);
    atributs.add(_social);
    atributs.add(_health);
    atributs.add(_Class);

    //Aqu se crea la instacia, que tiene todos los atributos del modelo
    Instances dataTest = new Instances("TestCases", atributos, 1);
    dataTest.setClassIndex(8);

    Instance setPrueba = new Instance(9);

    int index = -1;
    for (int i = 0; i < 8; i++) {
        index = atributs.get(i).indexOfValue(testCases[i]);
        //System.out.println(i + " " + atributs.get(i)  + " " + index + " " + testCases[i]);
        setPrueba.setValue(atributs.get(i), index);
    }

    //Agregando el set que se desea evaluar.
    dataTest.add(setPrueba);

    //Realizando la Prediccin
    //La instancia es la 0 debido a que es la unica que se encuentra.
    double valorP = clasify.classifyInstance(dataTest.instance(0));
    //get the name of the class value
    String prediccion = dataTest.classAttribute().value((int) valorP);

    return prediccion;
}

From source file:classifier.SellerClassifier.java

private void loadModelFile(String path) throws Exception {
    myClassifier = (Classifier) SerializationHelper.read(path);
}

From source file:classifier.SellerClassifier.java

private void loadFilterFile(String path) throws Exception {
    myFilter = (Filter) SerializationHelper.read(path);
}

From source file:com.deafgoat.ml.prognosticator.AppClassifier.java

License:Apache License

/**
 * Reads the trained model/*  w  ww . j  a va2  s  .  com*/
 * 
 * @throws Exception
 *             If the model can not be read.
 */
public void readModel() throws Exception {
    if (_logger.isDebugEnabled()) {
        _logger.debug("Deserializing model");
    }

    if (_config._writeToMongoDB) {
        MongoResult mongoResult = new MongoResult(_config._host, _config._port, _config._db,
                _config._modelCollection);
        _cls = mongoResult.readModel(_config._relation);
        mongoResult.close();
    }

    if (_config._writeToFile) {
        _cls = (Classifier) SerializationHelper.read(_config._modelFile);
    }
}

From source file:com.gamerecommendation.Weatherconditions.Clasificacion.java

public String clasificar(String[] testCases) throws Exception {
    String ruta = "model.model";

    InputStream classModelStream;
    classModelStream = getClass().getResourceAsStream(ruta);
    Classifier clasify = (Classifier) SerializationHelper.read(classModelStream);
    FastVector condition = new FastVector();
    condition.addElement("Cloudy");
    condition.addElement("Clear");
    condition.addElement("Sunny");
    condition.addElement("Fair");
    condition.addElement("Partly_Cloudy");
    condition.addElement("Mostly_Cloudy");
    condition.addElement("Showers");
    condition.addElement("Haze");
    condition.addElement("Dust");
    condition.addElement("Other");
    Attribute _condition = new Attribute("contition", condition);

    FastVector temperature = new FastVector();
    temperature.addElement("Hot");
    temperature.addElement("Mild");
    temperature.addElement("Cool");
    Attribute _temperature = new Attribute("temperature", temperature);

    FastVector chill = new FastVector();
    chill.addElement("Regrettable");
    chill.addElement("Mint");
    Attribute _chill = new Attribute("chill", chill);

    FastVector direction = new FastVector();
    direction.addElement("Mint");
    direction.addElement("Fair");
    direction.addElement("Regular");
    Attribute _direction = new Attribute("direction", direction);

    FastVector speed = new FastVector();
    speed.addElement("Mint");
    speed.addElement("Fair");
    speed.addElement("Regular");
    Attribute _speed = new Attribute("speed", speed);

    FastVector humidity = new FastVector();
    humidity.addElement("High");
    humidity.addElement("Normal");
    humidity.addElement("Low");
    Attribute _humidity = new Attribute("humidity", humidity);

    FastVector visibility = new FastVector();
    visibility.addElement("Recommended");
    visibility.addElement("Not_Recommended");
    Attribute _visibility = new Attribute("visibility", visibility);

    FastVector preassure = new FastVector();
    preassure.addElement("Fair");
    preassure.addElement("Mint");
    Attribute _preassure = new Attribute("preassure", preassure);

    FastVector Class = new FastVector();
    Class.addElement("Recommended");
    Class.addElement("Not_Recommended");
    Attribute _Class = new Attribute("class", Class);

    FastVector atributos = new FastVector(9);
    atributos.addElement(_condition);/* w w  w .  j av a2 s . co m*/
    atributos.addElement(_temperature);
    atributos.addElement(_chill);
    atributos.addElement(_direction);
    atributos.addElement(_speed);
    atributos.addElement(_humidity);
    atributos.addElement(_visibility);
    atributos.addElement(_preassure);
    atributos.addElement(_Class);

    ArrayList<Attribute> atributs = new ArrayList<>();
    atributs.add(_condition);
    atributs.add(_temperature);
    atributs.add(_chill);
    atributs.add(_direction);
    atributs.add(_speed);
    atributs.add(_humidity);
    atributs.add(_visibility);
    atributs.add(_preassure);
    atributs.add(_Class);

    //Aqu se crea la instacia, que tiene todos los atributos del modelo
    Instances dataTest = new Instances("TestCases", atributos, 1);
    dataTest.setClassIndex(8);

    Instance setPrueba = new Instance(9);

    int index = -1;
    for (int i = 0; i < 8; i++) {
        index = atributs.get(i).indexOfValue(testCases[i]);
        //System.out.println(i + " " + atributs.get(i)  + " " + index + " " + testCases[i]);
        setPrueba.setValue(atributs.get(i), index);
    }

    //Agregando el set que se desea evaluar.
    dataTest.add(setPrueba);

    //Realizando la Prediccin
    //La instancia es la 0 debido a que es la unica que se encuentra.
    double valorP = clasify.classifyInstance(dataTest.instance(0));
    //get the name of the class value
    String prediccion = dataTest.classAttribute().value((int) valorP);

    return prediccion;
}

From source file:com.hoho.android.usbserial.examples.SerialConsoleActivity.java

License:Open Source License

void svmInit() throws Exception {
    SVMRecognition.numberOfDataitems = 2 * NUMBER_OF_INPUTS - 1;
    SVMLevelRecognition.numberOfDataitems = 2 * NUMBER_OF_INPUTS - 1;
    svml = new SVMLevelRecognition();
    AssetManager assetManager = getAssets();

    classifierSVML = (Classifier) SerializationHelper.read(assetManager.open(SVMLevelRecognition.modelPath));
    svml.init();/*from  ww  w. j  a  v  a2  s  . co m*/
    svm = new SVMRecognition();
    classifierSVMH = (Classifier) SerializationHelper.read(assetManager.open(SVMRecognition.modelPath));
    svm.init();

    if (recognize) {

        SVMGestureRecognition.numberOfStates = numberOfGestures;
        SVMGestureRecognition.numberOfDataitems = numberOfGuestureInputs;

        svmg = new SVMGestureRecognition();
        classifierSVMG = (Classifier) SerializationHelper
                .read(assetManager.open(SVMGestureRecognition.modelPath));
        // should be called after training generation
        svmg.init();
    }

}

From source file:core.classification.Classifiers.java

License:Open Source License

public MultilayerPerceptron readSC(String filename1, String filename2, String filename3, String filename4,
        String filename5) throws Exception {
    SCA = (BayesNet) SerializationHelper.read(filename1);
    SCB = (MultilayerPerceptron) SerializationHelper.read(filename2);
    SCC1 = (MultilayerPerceptron) SerializationHelper.read(filename3);
    SCC2 = (MultilayerPerceptron) SerializationHelper.read(filename4);
    SCC3 = (MultilayerPerceptron) SerializationHelper.read(filename5);
    return SCC1;//from  w w  w. j av a  2  s  .  c om
}