Quantcast
Channel: Pentaho Community Forums
Viewing all articles
Browse latest Browse all 16689

What is wrong here ?

$
0
0
Hi

Error message is as follows:-
weka.core.UnassignedClassException: weka.classifiers.lazy.KStar: Class attribute not set!
*************************************
I am connecting weka to Apache derby database - all OK
weka splits the no. of instances into training and testing - all OK
I am running a loop of 6 classifiers and KStar is Number 1.............

Each instance record is made up as follows........................
First two fields are Date and Time - used as primary key - but they are not attributes as such
Fields 3 thru 11 are 9 attributes
Fields 12 and 13 are the predictive attributes:- 12 decimal(numeric) and 13 varchar(nominal)

In addition just before this error message I print out the ClassIndex and get.........

ClassIndex: CLASS_PREDICTED - which seems to be correct :)

and I print out the options
and get..........

Options: [-B, 20, -M, a] which is not the same as the options I am feeding in (which were [-B, 35, -M, a]) ???

Bob M
******************************************************

// create an array of weka filters - select attributes to be unused
int[][] filtersArray = new int[][] {

{ 1, 2, 3, 5, 7, 8, 10, 12}, // KStar
{ 1, 2, 3, 5, 7, 8, 10, 12}, // J48
{ 1, 2, 3, 5, 7, 8, 9, 10, 12}, // JRip
{ 1, 2, 3, 5, 8, 9, 10, 11, 12}, // NaiveBayes
{ 1, 2, 3, 4, 5, 7, 6, 8, 10, 12}, // LMT
{ 1, 2, 3, 5, 7, 8, 10, 13}, // KStar
};

// create an array of the number of the class attribute for each classifier prior to filtering
int[] class AttributeArray = new int[] {
13, 13, 13, 13, 13, 12};

// create strings of weka options

String[][] optionsArray = new String[][] {
weka.core.Utils.splitOptions("-B 35 -M a"), // KStar
weka.core.Utils.splitOptions("-C 0.25 -M 2"), // J48
weka.core.Utils.splitOptions("--F 3 -N 2.0 -0 2 -S 1 -E"), // JRip
weka.core.Utils.splitOptions(""), // NaiveBayes
weka.core.Utils.splitOptions("-I -1 -M 15 -W 0.0 -A"), // LMT
weka.core.Utils.splitOptions("-B 35 -M a") // KStar
};

//************************************************************************

private class WekaApp {

public BufferedReader readDataFile(String filename) {
BufferedReader inputReader = null;

return inputReader;
}

void doInit() throws Exception {

BufferedReader datafile = readDataFile("C:/Databases/us_copiosus");

InstanceQuery query = new InstanceQuery();
query.setQuery("SELECT * from USD_JPY");
Instances data = query.retrieveInstances();

// Split instances into training and testing (the split percentage is 97.56%)
double percent = 97.56;
int trainingSize = (int) Math.round(data.numInstances() * percent / 100);
int testingSize = data.numInstances() - trainingSize;
Instances training = new Instances(data, 0, trainingSize);
Instances testing = new Instances(data, trainingSize, testingSize);

// Choose a set of classifiers
Classifier[] models = new Classifier[] {
new KStar(),
new J48(),
new JRip(),
new NaiveBayes(),
new LMT(),
new KStar()
};

Predicted_Trend = 0;

// Run for each classifier model
for(int j = 0; j < models.length; j++) {

training.setClassIndex(classAttributeArray[j] - 1);
testing.setClassIndex(classAttributeArray[j] - 1);

Remove filter = new Remove(); // First, we create the base object.

filter.setAttributeIndicesArray(filtersArray[j]); // Finally, we provide an array of integer indexes.

//build classifier
FilteredClassifier fc = new FilteredClassifier(); // Create a FilteredClassifier object
((OptionHandler)models[j]).setOptions(optionsArray[j]);

try{
myConsole.getOut().println("Options: " = Arrays.toString(((OptionHandler)models[j].getOptions()));
}
catch(Exception e)
{
myConsole.getout().println(e);
}

fc.setClassifier(models[j]);

fc.setFilter(filter);
fc.buildClassifier(training);

// test the model
Evaluation eval = new Evaluation(training);
eval.evaluateModel(fc, testing);

// print the results a la weka Explorer:
String strSummary = eval.toSummaryString();

myConsole.getOut().println("results: "+ strSummary);

// get the confusion matrix
double[][] cmMatrix = eval.confusionMatrix();

for(int row_i=0;row_i<cmMatrix.length; row_i++){
for(int col_i=0;col_i<cmMatrix.length;col_i++){
myConsole.getOut().println(cmMatrix[row_i][col_i]);
myConsole.getOut().println("|");
}
myConsole.getOut().println();
}

//retrieve the last instance in the testing set
Instance lastInstance = testing.instance(testing.numInstances() - 1);

myConsole.getOut().println("lastInstance: "+ lastInstance);

// classifyInstance() just returns the index of the predicted label (the one with the highest probability) as a double
double pred = fc.classifyInstance(lastInstance);

myConsole.getOut().println("pred: "+ pred);

// add the prediction of the final testing instance to trend
// note prediction is class for classifiers 1 thru 5, and return for 6 & 7
if (pred >= 1) {
prediction = 1;
} else {
prediction = -1;
}
Predicted_Trend = Predicted_Trend + prediction;

myConsole.getOut().println("prediction: "+ prediction);

myConsole.getOut().println("Prediction_Trend: "+ Predicted_Trend);

}
}
}
}
************************************************

Viewing all articles
Browse latest Browse all 16689

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>