List of usage examples for weka.core Instance classIndex
public int classIndex();
From source file:meka.core.MLUtils.java
License:Open Source License
/** * GetxfromInstances - Extract attributes as a double x[] from an Instance. *//*from w w w. j a v a 2 s .com*/ public static double[] getxfromInstance(Instance xy) { int L = xy.classIndex(); double xy_[] = xy.toDoubleArray(); return Arrays.copyOfRange(xy_, L, xy_.length); }
From source file:milk.core.Exemplar.java
License:Open Source License
/** * Constructor using one instance to form an exemplar * // w w w . ja v a 2 s . com * @param instance the given instance * @param id the ID index */ public Exemplar(Instance inst, int id) { m_IdIndex = id; m_IdValue = inst.value(id); m_ClassIndex = inst.classIndex(); m_ClassValue = inst.classValue(); m_Instances = new Instances(inst.dataset(), 1); m_Instances.add(inst); }
From source file:ml.engine.LibSVM.java
License:Open Source License
/** * returns an instance into a sparse libsvm array * /*w w w .j av a 2 s. c o m*/ * @param instance the instance to work on * @return the libsvm array * @throws Exception if setup of array fails */ protected Object instanceToArray(Instance instance) throws Exception { int index; int count; int i; Object result; // determine number of non-zero attributes /* * for (i = 0; i < instance.numAttributes(); i++) { if (i == * instance.classIndex()) continue; if (instance.value(i) != 0) count++; } */ count = 0; for (i = 0; i < instance.numValues(); i++) { if (instance.index(i) == instance.classIndex()) { continue; } if (instance.valueSparse(i) != 0) { count++; } } // fill array /* * result = Array.newInstance(Class.forName(CLASS_SVMNODE), count); index = * 0; for (i = 0; i < instance.numAttributes(); i++) { if (i == * instance.classIndex()) continue; if (instance.value(i) == 0) continue; * * Array.set(result, index, Class.forName(CLASS_SVMNODE).newInstance()); * setField(Array.get(result, index), "index", new Integer(i + 1)); * setField(Array.get(result, index), "value", new * Double(instance.value(i))); index++; } */ result = Array.newInstance(Class.forName(CLASS_SVMNODE), count); index = 0; for (i = 0; i < instance.numValues(); i++) { int idx = instance.index(i); if (idx == instance.classIndex()) { continue; } if (instance.valueSparse(i) == 0) { continue; } Array.set(result, index, Class.forName(CLASS_SVMNODE).newInstance()); setField(Array.get(result, index), "index", new Integer(idx + 1)); setField(Array.get(result, index), "value", new Double(instance.valueSparse(i))); index++; } return result; }
From source file:moa.classifiers.AbstractClassifier.java
License:Open Source License
/** * Gets the index of the attribute in the instance, * given the index of the attribute in the learner. * * @param index the index of the attribute in the learner * @param inst the instance//w w w . ja va 2s . co m * @return the index in the instance */ protected static int modelAttIndexToInstanceAttIndex(int index, Instance inst) { return inst.classIndex() > index ? index : index + 1; }
From source file:moa.classifiers.bayes.NaiveBayesMultinomial.java
License:Open Source License
/** * Trains the classifier with the given instance. * * @param instance the new training instance to include in the model *//*ww w. ja v a 2 s . c om*/ @Override public void trainOnInstanceImpl(Instance inst) { if (this.reset == true) { this.m_numClasses = inst.numClasses(); double laplace = this.laplaceCorrectionOption.getValue(); int numAttributes = inst.numAttributes(); m_probOfClass = new double[m_numClasses]; Arrays.fill(m_probOfClass, laplace); m_classTotals = new double[m_numClasses]; Arrays.fill(m_classTotals, laplace * numAttributes); m_wordTotalForClass = new DoubleVector[m_numClasses]; for (int i = 0; i < m_numClasses; i++) { //Arrays.fill(wordTotal, laplace); m_wordTotalForClass[i] = new DoubleVector(); } this.reset = false; } // Update classifier int classIndex = inst.classIndex(); int classValue = (int) inst.value(classIndex); double w = inst.weight(); m_probOfClass[classValue] += w; m_classTotals[classValue] += w * totalSize(inst); double total = m_classTotals[classValue]; for (int i = 0; i < inst.numValues(); i++) { int index = inst.index(i); if (index != classIndex && !inst.isMissing(i)) { //m_wordTotalForClass[index][classValue] += w * inst.valueSparse(i); double laplaceCorrection = 0.0; if (m_wordTotalForClass[classValue].getValue(index) == 0) { laplaceCorrection = this.laplaceCorrectionOption.getValue(); } m_wordTotalForClass[classValue].addToValue(index, w * inst.valueSparse(i) + laplaceCorrection); } } }
From source file:moa.classifiers.bayes.NaiveBayesMultinomial.java
License:Open Source License
/** * Calculates the class membership probabilities for the given test * instance.// ww w . j a va 2 s . c om * * @param instance the instance to be classified * @return predicted class probability distribution */ @Override public double[] getVotesForInstance(Instance instance) { if (this.reset == true) { return new double[2]; } double[] probOfClassGivenDoc = new double[m_numClasses]; double totalSize = totalSize(instance); for (int i = 0; i < m_numClasses; i++) { probOfClassGivenDoc[i] = Math.log(m_probOfClass[i]) - totalSize * Math.log(m_classTotals[i]); } for (int i = 0; i < instance.numValues(); i++) { int index = instance.index(i); if (index == instance.classIndex() || instance.isMissing(i)) { continue; } double wordCount = instance.valueSparse(i); for (int c = 0; c < m_numClasses; c++) { double value = m_wordTotalForClass[c].getValue(index); probOfClassGivenDoc[c] += wordCount * Math.log(value == 0 ? this.laplaceCorrectionOption.getValue() : value); } } return Utils.logs2probs(probOfClassGivenDoc); }
From source file:moa.classifiers.bayes.NaiveBayesMultinomial.java
License:Open Source License
public double totalSize(Instance instance) { int classIndex = instance.classIndex(); double total = 0.0; for (int i = 0; i < instance.numValues(); i++) { int index = instance.index(i); if (index == classIndex || instance.isMissing(i)) { continue; }/* w w w . j a v a 2 s. co m*/ double count = instance.valueSparse(i); if (count >= 0) { total += count; } else { //throw new Exception("Numeric attribute value is not >= 0. " + i + " " + index + " " + // instance.valueSparse(i) + " " + " " + instance); } } return total; }
From source file:moa.classifiers.functions.SGD.java
License:Open Source License
/** * Trains the classifier with the given instance. * * @param instance the new training instance to include in the model *//* w w w. ja v a 2 s . c o m*/ @Override public void trainOnInstanceImpl(Instance instance) { if (m_weights == null) { m_weights = new DoubleVector(); m_bias = 0.0; } if (!instance.classIsMissing()) { double wx = dotProd(instance, m_weights, instance.classIndex()); double y; double z; if (instance.classAttribute().isNominal()) { y = (instance.classValue() == 0) ? -1 : 1; z = y * (wx + m_bias); } else { y = instance.classValue(); z = y - (wx + m_bias); y = 1; } // Compute multiplier for weight decay double multiplier = 1.0; if (m_numInstances == 0) { multiplier = 1.0 - (m_learningRate * m_lambda) / m_t; } else { multiplier = 1.0 - (m_learningRate * m_lambda) / m_numInstances; } for (int i = 0; i < m_weights.numValues(); i++) { m_weights.setValue(i, m_weights.getValue(i) * multiplier); } // Only need to do the following if the loss is non-zero if (m_loss != HINGE || (z < 1)) { // Compute Factor for updates double factor = m_learningRate * y * dloss(z); // Update coefficients for attributes int n1 = instance.numValues(); for (int p1 = 0; p1 < n1; p1++) { int indS = instance.index(p1); if (indS != instance.classIndex() && !instance.isMissingSparse(p1)) { m_weights.addToValue(indS, factor * instance.valueSparse(p1)); } } // update the bias m_bias += factor; } m_t++; } }
From source file:moa.classifiers.functions.SGD.java
License:Open Source License
/** * Calculates the class membership probabilities for the given test * instance./*from ww w .j a va 2 s. c o m*/ * * @param instance the instance to be classified * @return predicted class probability distribution */ @Override public double[] getVotesForInstance(Instance inst) { if (m_weights == null) { return new double[inst.numClasses()]; } double[] result = (inst.classAttribute().isNominal()) ? new double[2] : new double[1]; double wx = dotProd(inst, m_weights, inst.classIndex());// * m_wScale; double z = (wx + m_bias); if (inst.classAttribute().isNumeric()) { result[0] = z; return result; } if (z <= 0) { // z = 0; if (m_loss == LOGLOSS) { result[0] = 1.0 / (1.0 + Math.exp(z)); result[1] = 1.0 - result[0]; } else { result[0] = 1; } } else { if (m_loss == LOGLOSS) { result[1] = 1.0 / (1.0 + Math.exp(-z)); result[0] = 1.0 - result[1]; } else { result[1] = 1; } } return result; }
From source file:moa.classifiers.functions.SGDMultiClass.java
License:Open Source License
public void trainOnInstanceImpl(Instance instance, int classLabel) { if (!instance.classIsMissing()) { double wx = dotProd(instance, m_weights[classLabel], instance.classIndex()); double y; double z; if (instance.classAttribute().isNominal()) { y = (instance.classValue() != classLabel) ? -1 : 1; z = y * (wx + m_bias[classLabel]); } else {/*from ww w.j av a 2 s.c o m*/ y = instance.classValue(); z = y - (wx + m_bias[classLabel]); y = 1; } // Compute multiplier for weight decay double multiplier = 1.0; if (m_numInstances == 0) { multiplier = 1.0 - (m_learningRate * m_lambda) / m_t; } else { multiplier = 1.0 - (m_learningRate * m_lambda) / m_numInstances; } for (int i = 0; i < m_weights[classLabel].numValues(); i++) { m_weights[classLabel].setValue(i, m_weights[classLabel].getValue(i) * multiplier); } // Only need to do the following if the loss is non-zero if (m_loss != HINGE || (z < 1)) { // Compute Factor for updates double factor = m_learningRate * y * dloss(z); // Update coefficients for attributes int n1 = instance.numValues(); for (int p1 = 0; p1 < n1; p1++) { int indS = instance.index(p1); if (indS != instance.classIndex() && !instance.isMissingSparse(p1)) { m_weights[classLabel].addToValue(indS, factor * instance.valueSparse(p1)); } } // update the bias m_bias[classLabel] += factor; } } }