List of usage examples for weka.core Instance numClasses
public int numClasses();
From source file:edu.umbc.cs.maple.utils.WekaUtils.java
License:Open Source License
/** Converts a set of instances to svm-light format * @param data the weka instances/*from w w w . j a v a 2 s . com*/ * @return the weka instances in svm-light format */ public static String arffToSVMLight(Instance data, SVMLightLabelFormat labelFormat) { if (labelFormat == SVMLightLabelFormat.CLASSIFICATION && data.numClasses() != 2) { throw new IllegalArgumentException( "SVM-light classification label format requires that the data contain only two classes."); } String str = ""; String endline = System.getProperty("line.separator"); int numAttributes = data.numAttributes(); int classAttIdx = data.classIndex(); // convert the instance label if (labelFormat == SVMLightLabelFormat.CLASSIFICATION) { str += (data.classValue() == 0) ? "-1" : "1"; } else { str += data.classValue(); } str += " "; // convert each feature for (int attIdx = 0; attIdx < numAttributes; attIdx++) { // skip the class attribute if (attIdx == classAttIdx) continue; str += (attIdx + 1) + ":" + data.value(attIdx) + " "; } // append the instance info string str += "#"; str += endline; return str; }
From source file:functions.kernelPerceptron.java
License:Open Source License
@Override public void trainOnInstanceImpl(Instance inst) { //Init Perceptron if (this.reset == true) { this.reset = false; this.numberAttributes = inst.numAttributes(); this.numberClasses = inst.numClasses(); this.weightAttribute = new double[inst.numClasses()][inst.numAttributes()]; for (int i = 0; i < inst.numClasses(); i++) { for (int j = 0; j < inst.numAttributes(); j++) { weightAttribute[i][j] = 0.2 * this.classifierRandom.nextDouble() - 0.1; }// w ww . ja va 2 s . c o m } } double[] preds = new double[inst.numClasses()]; for (int i = 0; i < inst.numClasses(); i++) { preds[i] = prediction(inst, i); } double learningRatio = learningRatioOption.getValue(); int actualClass = (int) inst.classValue(); for (int i = 0; i < inst.numClasses(); i++) { double actual = (i == actualClass) ? 1.0 : 0.0; double delta = (actual - preds[i]) * preds[i] * (1 - preds[i]); for (int j = 0; j < inst.numAttributes() - 1; j++) { this.weightAttribute[i][j] += learningRatio * delta * inst.value(j); } //this.weightAttribute[i][inst.numAttributes() - 1] += learningRatio * delta; } }
From source file:functions.kernelPerceptron.java
License:Open Source License
@Override public double[] getVotesForInstance(Instance inst) { double[] votes = new double[inst.numClasses()]; if (this.reset == false) { for (int i = 0; i < votes.length; i++) { votes[i] = prediction(inst, i); }/*from w w w .j av a2 s.com*/ try { weka.core.Utils.normalize(votes); } catch (Exception e) { // ignore all zero votes error } } return votes; }
From source file:gyc.OverBoostM1.java
License:Open Source License
/** * Calculates the class membership probabilities for the given test instance. * * @param instance the instance to be classified * @return predicted class probability distribution * @throws Exception if instance could not be classified * successfully/*w ww . j a v a2 s. co m*/ */ public double[] distributionForInstance(Instance instance) throws Exception { // default model? if (m_ZeroR != null) { return m_ZeroR.distributionForInstance(instance); } if (m_NumIterationsPerformed == 0) { throw new Exception("No model built"); } double[] sums = new double[instance.numClasses()]; if (m_NumIterationsPerformed == 1) { return m_Classifiers[0].distributionForInstance(instance); } else { for (int i = 0; i < m_NumIterationsPerformed; i++) { sums[(int) m_Classifiers[i].classifyInstance(instance)] += m_Betas[i]; } return Utils.logs2probs(sums); } }
From source file:gyc.SMOTEBagging.java
License:Open Source License
/** * Calculates the class membership probabilities for the given test * instance./*from w w w . j a v a 2 s . com*/ * * @param instance the instance to be classified * @return preedicted class probability distribution * @throws Exception if distribution can't be computed successfully */ public double[] distributionForInstance(Instance instance) throws Exception { double[] sums = new double[instance.numClasses()], newProbs; for (int i = 0; i < m_NumIterations; i++) { if (instance.classAttribute().isNumeric() == true) { sums[0] += m_Classifiers[i].classifyInstance(instance); } else { newProbs = m_Classifiers[i].distributionForInstance(instance); for (int j = 0; j < newProbs.length; j++) sums[j] += newProbs[j]; } } if (instance.classAttribute().isNumeric() == true) { sums[0] /= (double) m_NumIterations; return sums; } else if (Utils.eq(Utils.sum(sums), 0)) { return sums; } else { Utils.normalize(sums); return sums; } }
From source file:hr.irb.fastRandomForest.FastRfBagging.java
License:Open Source License
/** * Calculates the class membership probabilities for the given test * instance./*from w w w. j av a 2 s. c om*/ * * @param instance the instance to be classified * * @return predicted class probability distribution * * @throws Exception if distribution can't be computed successfully */ @Override public double[] distributionForInstance(Instance instance) throws Exception { double[] sums = new double[instance.numClasses()], newProbs; for (int i = 0; i < m_NumIterations; i++) { if (instance.classAttribute().isNumeric()) { sums[0] += m_Classifiers[i].classifyInstance(instance); } else { newProbs = m_Classifiers[i].distributionForInstance(instance); for (int j = 0; j < newProbs.length; j++) sums[j] += newProbs[j]; } } if (instance.classAttribute().isNumeric()) { sums[0] /= (double) m_NumIterations; return sums; } else if (Utils.eq(Utils.sum(sums), 0)) { return sums; } else { Utils.normalize(sums); return sums; } }
From source file:j48.ClassifierTree.java
License:Open Source License
/** * Classifies an instance.//from ww w. j a v a2 s. c om * * @param instance the instance to classify * @return the classification * @throws Exception if something goes wrong */ public double classifyInstance(Instance instance) throws Exception { double maxProb = -1; double currentProb; int maxIndex = 0; int j; for (j = 0; j < instance.numClasses(); j++) { currentProb = getProbs(j, instance, 1); if (Utils.gr(currentProb, maxProb)) { maxIndex = j; maxProb = currentProb; } } return (double) maxIndex; }
From source file:j48.ClassifierTree.java
License:Open Source License
/** * Returns class probabilities for a weighted instance. * * @param instance the instance to get the distribution for * @param useLaplace whether to use laplace or not * @return the distribution//from ww w .java 2s . c o m * @throws Exception if something goes wrong */ public final double[] distributionForInstance(Instance instance, boolean useLaplace) throws Exception { double[] doubles = new double[instance.numClasses()]; for (int i = 0; i < doubles.length; i++) { if (!useLaplace) { doubles[i] = getProbs(i, instance, 1); } else { doubles[i] = getProbsLaplace(i, instance, 1); } } return doubles; }
From source file:library.MikeC45PruneableClassifierTree.java
License:Open Source License
public double[] getDisjunct(Instance instance) throws Exception { double[] doubles = new double[instance.numClasses()]; for (int i = 0; i < doubles.length; i++) { doubles[i] = getNum(i, instance, 1); }//from w w w .ja v a 2 s .c o m System.out.print(" " + getDepth(instance, 0)); return doubles; }
From source file:machinelearningproject.DecisionTree.java
@Override public double classifyInstance(Instance instance) throws Exception { String classification = mainTree.traverseTree(instance); double result = 0.0; for (int i = 0; i < instance.numClasses(); i++) { if (classification.equals(instance.attribute(instance.classIndex()).value(i))) { result = (double) i; }/*w w w. j a v a 2 s .c o m*/ } return result; }