No prediction obtained
Bob Matthews <[email protected]>
| Newsgroups | gmane.comp.ai.weka |
|---|---|
| Message-ID | <[email protected]> |
Hi
I have just noticed that I am not getting a weka prediction !
i.e. I get "Failed to make prediction" message ! (and I am unsure why)
In my main program I have the following code:-
|||if| |((DCC_conf_small_down) || (DCC_conf_small_up)) {|
|||// run Weka, create new Instance and make prediction for BBTheta|
|||WekaApp wekaApp = ||new| |WekaApp();|
|||// this should only be called once (or whenever the model has changed
and needs reloading)|
|||try| |{|
|||wekaApp.init();|
|||}|
|||catch| |(Exception e) {|
|||System.err.println(||"ERROR init: "| |+ e);|
|||e.printStackTrace();|
|||}|
|||// make a prediction|
|||try| |{|
|||int| |pred = wekaApp.currentInstance(OSV);|
|||if| |(pred != -||1||)|
|||myConsole.getOut().println(||"Predicted label: "| |+
wekaApp.getLabel(pred));|
|||else|
|||System.out.println(||"Failed to make prediction!"||);|
|||}|
|||catch| |(Exception e) {|
|||System.err.println(||"ERROR predict: "| |+ e);|
|||e.printStackTrace();|
|||}|
|||myConsole.getOut().println(||"Weka finished"||);|
OSV is a double variable and is calculated prior to the above code
At the end of my main code I have the following class:-
|||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/SimpleLogistic.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);|
|||System.out.println(||"prediction: "| |+ prediction);|
|||return| |(||int||) prediction;|
|||}|
|||catch| |(Exception e) {|
|||System.err.println(||"Weka exception occurred: "| |+ e);|
|||e.printStackTrace();|
|||return| |-||1||;|
|||}|
|||}|
|||} ||// end of class WekaApp|
Any suggestions as to why - no prediction ?
Bob M
_______________________________________________
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