Re: weka - failed to make a prediction
Peter Reutemann <[email protected]> Wed, 13 Jul 2022 20:57:54 +1200
| Newsgroups | gmane.comp.ai.weka |
|---|---|
| Message-ID | <CAHoQ12+_vGOhffgV+0SsYVRhPxgTjezVidwa-icNmiP=5se_fQ@mail.gmail.com> |
[Please reply to the list]
> //This one file must be in the "files" Directory (usually: Documents/JForex/files)
> @Library("C:/Users/rgmat/Documents/JForex/files/weka.jar")
This is only the core Weka library, not the AutoWeka (Weka) package.
> public class WekaApp {
>
> /** the model to use. */
> protected Classifier cls;
>
> /** the training data header. */
> protected Instances header;
>
> /**
> * Load model and header.
> *
> * @throws Exception if deserialization fails
> */
> public void init() throws Exception {
> // [A] load model and header of data
> // deserialize model
> Object[] objs = SerializationHelper.readAll("C:/TSFDC Trading 2022/JRip_EURNZD.model");
> cls = (Classifier) objs[0];
> header = (Instances) objs[1];
> }
>
> /**
> * Returns the class label associated with the index.
> *
> * @param index the class label index
> * @return the label
> * @throws IllegalStateException if no header present
> */
> public String getLabel(int index) throws IllegalStateException {
> if (header == null)
> throw new IllegalStateException("No model/header loaded?");
> return header.classAttribute().value(index);
> }
>
> /**
> * Performs a prediction.
> *
> * @param v the value of the input variable to use for making a prediction
> * @return the predicted value (class index), returns -1 if it fails to make prediction
> * @throws Exception if prediction fails
> */
> public int currentInstance(double v) throws Exception {
> try {
> // [B] create instance
> // create the current instance
> double[] values = new double[header.numAttributes()];
> for (int i = 0; i < values.length; i++)
> values[i] = Utils.missingValue();
> // - numeric
> values[0] = v;
> DenseInstance currentInstance = new DenseInstance(1.0, values);
> currentInstance.setDataset(header);
>
> // [C] classify nominal class
> // classifyInstance() just returns the index of the predicted label (the one with the highest probability) as a double
> double prediction = cls.classifyInstance(currentInstance);
> myConsole.getOut().println("predition: " + prediction);
>
> return (int) prediction;
> }
> catch (Exception e) {
> System.err.println("Weka exception occurred: " + e);
> e.printStackTrace();
> return -1;
> }
> }
> } // end of class WekaApp
I don't see a main method that initializes the Weka packages, e.g. using:
weka.gui.GenericObjectEditor.determineClasses();
Cheers, Peter
--
Peter Reutemann
Dept. of Computer Science
University of Waikato, NZ
+64 (7) 858-5174 (office)
+64 (7) 577-5304 (home office)
https://www.cs.waikato.ac.nz/~fracpete/
http://www.data-mining.co.nz/
_______________________________________________
Wekalist mailing list -- [email protected]
Send posts to [email protected]
To unsubscribe send an email to [email protected]
To subscribe, unsubscribe, etc., visit https://list.waikato.ac.nz/postorius/lists/wekalist.list.waikato.ac.nz
List etiquette: http://www.cs.waikato.ac.nz/~ml/weka/mailinglist_etiquette.html