List of usage examples for weka.core Instance classValue
public double classValue();
From source file:ml.engine.LibSVM.java
License:Open Source License
/** * builds the classifier//from w ww. j a v a 2 s. com * * @param insts the training instances * @throws Exception if libsvm classes not in classpath or libsvm encountered * a problem */ @Override public void buildClassifier(Instances insts) throws Exception { m_Filter = null; if (!isPresent()) { throw new Exception("libsvm classes not in CLASSPATH!"); } // remove instances with missing class insts = new Instances(insts); insts.deleteWithMissingClass(); if (!getDoNotReplaceMissingValues()) { m_ReplaceMissingValues = new ReplaceMissingValues(); m_ReplaceMissingValues.setInputFormat(insts); insts = Filter.useFilter(insts, m_ReplaceMissingValues); } // can classifier handle the data? // we check this here so that if the user turns off // replace missing values filtering, it will fail // if the data actually does have missing values getCapabilities().testWithFail(insts); if (getNormalize()) { m_Filter = new Normalize(); m_Filter.setInputFormat(insts); insts = Filter.useFilter(insts, m_Filter); } // nominal to binary m_NominalToBinary = new NominalToBinary(); m_NominalToBinary.setInputFormat(insts); insts = Filter.useFilter(insts, m_NominalToBinary); Vector vy = new Vector(); Vector vx = new Vector(); int max_index = 0; for (int d = 0; d < insts.numInstances(); d++) { Instance inst = insts.instance(d); Object x = instanceToArray(inst); int m = Array.getLength(x); if (m > 0) { max_index = Math.max(max_index, ((Integer) getField(Array.get(x, m - 1), "index")).intValue()); } vx.addElement(x); vy.addElement(new Double(inst.classValue())); } // calculate actual gamma if (getGamma() == 0) { m_GammaActual = 1.0 / max_index; } else { m_GammaActual = m_Gamma; } // check parameter String error_msg = (String) invokeMethod(Class.forName(CLASS_SVM).newInstance(), "svm_check_parameter", new Class[] { Class.forName(CLASS_SVMPROBLEM), Class.forName(CLASS_SVMPARAMETER) }, new Object[] { getProblem(vx, vy), getParameters() }); if (error_msg != null) { throw new Exception("Error: " + error_msg); } // make probability estimates deterministic from run to run Class svmClass = Class.forName(CLASS_SVM); Field randF = svmClass.getField("rand"); Random rand = (Random) randF.get(null); // static field rand.setSeed(m_Seed); // train model m_Model = invokeMethod(Class.forName(CLASS_SVM).newInstance(), "svm_train", new Class[] { Class.forName(CLASS_SVMPROBLEM), Class.forName(CLASS_SVMPARAMETER) }, new Object[] { getProblem(vx, vy), getParameters() }); }
From source file:moa.classifiers.AbstractClassifier.java
License:Open Source License
@Override public boolean correctlyClassifies(Instance inst) { return Utils.maxIndex(getVotesForInstance(inst)) == (int) inst.classValue(); }
From source file:moa.classifiers.AccuracyWeightedEnsemble.java
License:Open Source License
@Override public void trainOnInstanceImpl(Instance inst) { this.initVariables(); this.classDistributions[(int) inst.classValue()]++; this.currentChunk.add(inst); this.processedInstances++; if (this.processedInstances % this.chunkSize == 0) { this.processChunk(); }/*w w w.j a v a 2 s . c o m*/ }
From source file:moa.classifiers.bayes.NaiveBayes.java
License:Open Source License
@Override public void trainOnInstanceImpl(Instance inst) { this.observedClassDistribution.addToValue((int) inst.classValue(), inst.weight()); for (int i = 0; i < inst.numAttributes() - 1; i++) { int instAttIndex = modelAttIndexToInstanceAttIndex(i, inst); AttributeClassObserver obs = this.attributeObservers.get(i); if (obs == null) { obs = inst.attribute(instAttIndex).isNominal() ? newNominalClassObserver() : newNumericClassObserver(); this.attributeObservers.set(i, obs); }/*w w w . ja v a 2 s . com*/ obs.observeAttributeClass(inst.value(instAttIndex), (int) inst.classValue(), inst.weight()); } }
From source file:moa.classifiers.DecisionStump.java
License:Open Source License
@Override public void trainOnInstanceImpl(Instance inst) { this.observedClassDistribution.addToValue((int) inst.classValue(), inst.weight()); for (int i = 0; i < inst.numAttributes() - 1; i++) { int instAttIndex = modelAttIndexToInstanceAttIndex(i, inst); AttributeClassObserver obs = this.attributeObservers.get(i); if (obs == null) { obs = inst.attribute(instAttIndex).isNominal() ? newNominalClassObserver() : newNumericClassObserver(); this.attributeObservers.set(i, obs); }//from w w w.j a v a 2 s . c o m obs.observeAttributeClass(inst.value(instAttIndex), (int) inst.classValue(), inst.weight()); } if (this.trainingWeightSeenByModel - this.weightSeenAtLastSplit >= this.gracePeriodOption.getValue()) { this.bestSplit = findBestSplit((SplitCriterion) getPreparedClassOption(this.splitCriterionOption)); this.weightSeenAtLastSplit = this.trainingWeightSeenByModel; } }
From source file:moa.classifiers.drift.SingleClassifierDrift.java
License:Open Source License
@Override public void trainOnInstanceImpl(Instance inst) { //this.numberInstances++; int trueClass = (int) inst.classValue(); boolean prediction; if (Utils.maxIndex(this.classifier.getVotesForInstance(inst)) == trueClass) { prediction = true;/*from w ww . ja v a 2 s.c om*/ } else { prediction = false; } this.ddmLevel = this.driftDetectionMethod.computeNextVal(prediction); switch (this.ddmLevel) { case DriftDetectionMethod.DDM_WARNING_LEVEL: //System.out.println("1 0 W"); //System.out.println("DDM_WARNING_LEVEL"); this.warningDetected++; if (newClassifierReset == true) { this.newclassifier.resetLearning(); newClassifierReset = false; } this.newclassifier.trainOnInstance(inst); break; case DriftDetectionMethod.DDM_OUTCONTROL_LEVEL: //System.out.println("0 1 O"); //System.out.println("DDM_OUTCONTROL_LEVEL"); this.changeDetected++; this.classifier = null; this.classifier = this.newclassifier; if (this.classifier instanceof WEKAClassifier) { ((WEKAClassifier) this.classifier).buildClassifier(); } this.newclassifier = ((Classifier) getPreparedClassOption(this.baseLearnerOption)).copy(); this.newclassifier.resetLearning(); break; case DriftDetectionMethod.DDM_INCONTROL_LEVEL: //System.out.println("0 0 I"); //System.out.println("DDM_INCONTROL_LEVEL"); newClassifierReset = true; break; default: //System.out.println("ERROR!"); } this.classifier.trainOnInstance(inst); }
From source file:moa.classifiers.featureselection.OFSL.java
License:Open Source License
@Override public void trainOnInstanceImpl(Instance inst) { double y_t, m_bias_p1, m_bias_p2, m_bias; double[] m_weights_p1, m_weights_p2, m_weights; if (this.weights == null) { this.weights = new double[inst.numValues()]; for (int i = 0; i < this.weights.length; i++) this.weights[i] = 0.0; this.bias = 0.0; }/* w w w . j a v a2s . co m*/ if (inst.classAttribute().isNominal()) { y_t = (inst.classValue() == 0) ? -1 : 1; } else { y_t = inst.classValue(); } double f_t = dot(inst.toDoubleArray(), this.weights); f_t += this.bias; if (y_t * f_t < 0) { m_weights_p1 = scalar_vector(1.0 - this.stepSizeOption.getValue() * this.learningRateOption.getValue(), this.weights); m_bias_p1 = (1.0 - this.stepSizeOption.getValue() * this.learningRateOption.getValue()) * this.bias; m_weights_p2 = scalar_vector(this.learningRateOption.getValue() * y_t, inst.toDoubleArray()); m_bias_p2 = this.learningRateOption.getValue() * y_t; m_weights = vector_add(m_weights_p1, m_weights_p2); m_bias = m_bias_p1 + m_bias_p2; m_weights = l2_projection(m_weights, m_bias, this.learningRateOption.getValue()); m_weights = truncate(m_weights, this.numSelectOption.getValue()); for (int i = 0; i < m_weights_p1.length; i++) this.weights[i] = m_weights[i]; this.bias = m_weights[m_weights.length - 1]; } else { this.weights = scalar_vector(1.0 - this.stepSizeOption.getValue() * this.learningRateOption.getValue(), this.weights); this.bias = (1.0 - this.stepSizeOption.getValue() * this.learningRateOption.getValue()) * this.bias; } }
From source file:moa.classifiers.featureselection.OFSP.java
License:Open Source License
@Override public void trainOnInstanceImpl(Instance inst) { double y_t, f_t, denom, m_bias; int[] indices = new int[this.numSelectOption.getValue()]; double[] m_weights; if (this.weights == null) { this.weights = new double[inst.numValues()]; for (int i = 0; i < this.weights.length; i++) this.weights[i] = this.rand.nextGaussian(); this.bias = 0.0; this.weights = truncate(this.weights, this.numSelectOption.getValue()); }/* ww w . ja v a2s . co m*/ if (inst.classAttribute().isNominal()) { y_t = (inst.classValue() == 0) ? -1 : 1; } else { y_t = inst.classValue(); } double[] x_t = inst.toDoubleArray(); double[] x_hat = inst.toDoubleArray(); if (this.rand.nextDouble() < this.searchOption.getValue()) { int[] indices_perm = perm(inst.numAttributes()); for (int i = 0; i < this.numSelectOption.getValue(); i++) indices[i] = indices_perm[i]; } else { int[] sorted_indices = bubblesort_index(abs_vector(this.weights)); for (int i = 0; i < inst.numAttributes() - this.numSelectOption.getValue(); i++) x_hat[sorted_indices[i]] = 0.0; for (int i = 0; i < this.numSelectOption.getValue(); i++) indices[i] = sorted_indices[sorted_indices.length - i - 1]; } f_t = 0; for (int i = 0; i < this.numSelectOption.getValue(); i++) f_t += this.weights[indices[i]] * x_t[indices[i]]; f_t += this.bias; if (f_t * y_t < 0) { for (int i = 0; i < x_hat.length; i++) { denom = this.numSelectOption.getValue() / x_hat.length * this.searchOption.getValue(); if (this.weights[i] != 0) denom += (1 - this.searchOption.getValue()) * this.weights[i]; x_hat[i] /= denom; } m_weights = scalar_vector(y_t * this.stepSizeOption.getValue(), x_hat); m_bias = y_t * this.stepSizeOption.getValue() * this.bias; m_weights = vector_add(m_weights, this.weights); m_bias += m_bias + this.bias; m_weights = l2_projection(m_weights, m_bias, this.boundOption.getValue()); m_weights = truncate(m_weights, this.numSelectOption.getValue()); for (int i = 0; i < m_weights.length - 1; i++) this.weights[i] = m_weights[i]; this.bias = m_weights[m_weights.length - 1]; } }
From source file:moa.classifiers.functions.AbsurdOracleClassifier.java
License:Apache License
@Override public double[] getVotesForInstance(Instance i) { DoubleVector observedClassDistribution = new DoubleVector(); if (this.randNumGen.nextFloat() < this.desiredAccuracyOption.getValue()) { observedClassDistribution.addToValue((int) i.classValue(), i.weight()); } else {//from w w w. j a v a 2 s. c om observedClassDistribution.addToValue(((int) i.classValue() + 1) % i.numClasses(), i.weight()); } return observedClassDistribution.getArrayCopy(); }
From source file:moa.classifiers.functions.MajorityClass.java
License:Open Source License
@Override public void trainOnInstanceImpl(Instance inst) { this.observedClassDistribution.addToValue((int) inst.classValue(), inst.weight()); }