List of usage examples for weka.core Instance classIndex
public int classIndex();
From source file:net.sf.jclal.classifier.WekaComitteClassifier.java
License:Open Source License
/** * Returns the probability that has the instance to belong to each class. * * @param instance The instance to test//from ww w .j a v a 2 s. co m * @return The probabilities for each class */ @Override public double[] distributionForInstance(Instance instance) { try { double[] consensus = new double[instance.dataset().numDistinctValues(instance.classIndex())]; double sizeCommittee = classifiers.length; for (int i = 0; i < sizeCommittee; i++) { /* modified here */ /*double[] currentProb = classifiers[i] .distributionForInstance(instance); for (int j = 0; j < consensus.length; j++) { consensus[j] += currentProb[j]; }*/ //FIRST MODIFIED VERSION /*Random random = new Random(); for (int j = 0; j < consensus.length; j++) { double cenas = random.nextGaussian(); if(cenas < 0) cenas = 0; else if(cenas > 1) cenas = 1; consensus[j] += cenas; }*/ //SECOND MODIFIED VERSION Random random = new Random(); for (int j = 0; j < consensus.length; j++) { double cenas = random.nextGaussian(); if (cenas < 0) cenas = -0.48 * cenas; else cenas = 0.48 + 0.48 * cenas; consensus[j] += cenas; } } for (int i = 0; i < consensus.length; i++) { consensus[i] /= sizeCommittee; } return consensus; } catch (Exception ex) { Logger.getLogger(VoteEntropyQueryStrategy.class.getName()).log(Level.SEVERE, null, ex); } return null; }
From source file:neuralnetwork.NeuralNet.java
private double trainNetwork(Instance pInst) { double error = 0.0; double temp = runNetwork(pInst); int cIndex = pInst.classIndex(); double target; target = pInst.value(cIndex);/*w ww. j a va2 s. c o m*/ for (int i = mLayers - 1; i >= 0; i--) { // Calculate error if (i == (mLayers - 1)) { // i = input from layer on the left // j = current node // Output layer error // errj = aj(1 - aj)(aj - tj) for (int j = 0; j < mClassNum; j++) { temp = mOutput.get(j); if (target == (double) j) { error = temp * (1 - temp) * (temp - target); } else { error = temp * (1 - temp) * (temp - 0.0); } } } else { // j = current node; // k = node from the layer on the right // Hidden layer error // errj = aj(1 - aj) * sum(wjk * errk) } // Update weight ij // wij <- wij - mlRate * errj * ai //mNetwork.set(j, neurons); } return error; }
From source file:org.esa.nest.gpf.SGD.java
/** * Updates the classifier with the given instance. * * @param instance the new training instance to include in the model * @exception Exception if the instance could not be incorporated in the * model./* w ww .ja v a 2 s .co m*/ */ @Override public void updateClassifier(Instance instance) throws Exception { 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_weights[m_weights.length - 1]); } else { y = instance.classValue(); z = y - (wx + m_weights[m_weights.length - 1]); 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.length - 1; i++) { m_weights[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[indS] += factor * instance.valueSparse(p1); } } // update the bias m_weights[m_weights.length - 1] += factor; } m_t++; } }
From source file:org.esa.nest.gpf.SGD.java
/** * Computes the distribution for a given instance * * @param instance the instance for which distribution is computed * @return the distribution/* w w w.jav a2 s . c om*/ * @throws Exception if the distribution can't be computed successfully */ @Override public double[] distributionForInstance(Instance inst) throws Exception { double[] result = (inst.classAttribute().isNominal()) ? new double[2] : new double[1]; if (m_replaceMissing != null) { m_replaceMissing.input(inst); inst = m_replaceMissing.output(); } if (m_nominalToBinary != null) { m_nominalToBinary.input(inst); inst = m_nominalToBinary.output(); } if (m_normalize != null) { m_normalize.input(inst); inst = m_normalize.output(); } double wx = dotProd(inst, m_weights, inst.classIndex());// * m_wScale; double z = (wx + m_weights[m_weights.length - 1]); 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:org.pentaho.di.scoring.WekaScoringData.java
License:Open Source License
/** * Generates a prediction (more specifically, an output row containing all * input Kettle fields plus new fields that hold the prediction(s)) for an * incoming Kettle row given a Weka model. * * @param inputMeta the meta data for the incoming rows * @param outputMeta the meta data for the output rows * @param inputRow the values of the incoming row * @param meta meta data for this step * @return a Kettle row containing all incoming fields along with new ones * that hold the prediction(s)/*from w w w . java2 s . co m*/ * @throws Exception if an error occurs */ public Object[] generatePrediction(RowMetaInterface inputMeta, RowMetaInterface outputMeta, Object[] inputRow, WekaScoringMeta meta) throws Exception { int[] mappingIndexes = m_mappingIndexes; WekaScoringModel model = getModel(); boolean outputProbs = meta.getOutputProbabilities(); boolean supervised = model.isSupervisedLearningModel(); Attribute classAtt = null; if (supervised) { classAtt = model.getHeader().classAttribute(); } // need to construct an Instance to represent this // input row Instance toScore = constructInstance(inputMeta, inputRow, mappingIndexes, model, false); double[] prediction = model.distributionForInstance(toScore); // Update the model?? if (meta.getUpdateIncrementalModel() && model.isUpdateableModel() && !toScore.isMissing(toScore.classIndex())) { model.update(toScore); } // First copy the input data to the new result... Object[] resultRow = RowDataUtil.resizeArray(inputRow, outputMeta.size()); int index = inputMeta.size(); // output for numeric class or discrete class value if (prediction.length == 1 || !outputProbs) { if (supervised) { if (classAtt.isNumeric()) { Double newVal = new Double(prediction[0]); resultRow[index++] = newVal; } else { int maxProb = Utils.maxIndex(prediction); if (prediction[maxProb] > 0) { String newVal = classAtt.value(maxProb); resultRow[index++] = newVal; } else { String newVal = BaseMessages.getString(WekaScoringMeta.PKG, "WekaScoringData.Message.UnableToPredict"); //$NON-NLS-1$ resultRow[index++] = newVal; } } } else { int maxProb = Utils.maxIndex(prediction); if (prediction[maxProb] > 0) { Double newVal = new Double(maxProb); resultRow[index++] = newVal; } else { String newVal = BaseMessages.getString(WekaScoringMeta.PKG, "WekaScoringData.Message.UnableToPredictCluster"); //$NON-NLS-1$ resultRow[index++] = newVal; } } } else { // output probability distribution for (int i = 0; i < prediction.length; i++) { Double newVal = new Double(prediction[i]); resultRow[index++] = newVal; } } return resultRow; }
From source file:sg.edu.nus.comp.nlp.ims.classifiers.CMultiClassesSVM.java
License:Open Source License
/** * generate instances for classifier classIdx * * @param p_Instances// w ww . j a v a 2s.co m * input instances * @param p_ClassIndex * class index * @param p_ID2Classes * instance ids * @return new instances */ protected Instances genInstances(Instances p_Instances, double p_ClassIndex, Hashtable<String, ArrayList<Double>> p_ID2Classes) { Instances newInsts = new Instances(this.m_OutputFormat, 0); for (int i = 0; i < p_Instances.numInstances(); i++) { Instance inst = p_Instances.instance(i); Instance newInst = null; if (SparseInstance.class.isInstance(inst)) { newInst = new SparseInstance(inst); } else { newInst = new Instance(inst); } if (newInst.value(p_Instances.classIndex()) == p_ClassIndex) { newInst.setValue(inst.classIndex(), 1); } else { if (p_ID2Classes == null || !p_ID2Classes.get(inst.stringValue(this.m_IndexOfID)) .contains(new Double(p_ClassIndex))) { newInst.setValue(inst.classIndex(), 0); } else { continue; } } newInst.deleteAttributeAt(this.m_IndexOfID); newInst.setDataset(newInsts); newInsts.add(newInst); } return newInsts; }
From source file:sirius.trainer.step4.RunClassifierWithNoLocationIndex.java
License:Open Source License
public static Object jackKnifeClassifierOneWithNoLocationIndex(JInternalFrame parent, ApplicationData applicationData, JTextArea classifierOneDisplayTextArea, GenericObjectEditor m_ClassifierEditor, double ratio, GraphPane myGraph, ClassifierResults classifierResults, int range, double threshold, boolean outputClassifier, String classifierName, String[] classifierOptions, boolean returnClassifier, int randomNumberForClassifier) { try {// ww w . j a va 2 s.c om StatusPane statusPane = applicationData.getStatusPane(); long totalTimeStart = System.currentTimeMillis(), totalTimeElapsed; Classifier tempClassifier; if (m_ClassifierEditor != null) tempClassifier = (Classifier) m_ClassifierEditor.getValue(); else tempClassifier = Classifier.forName(classifierName, classifierOptions); //Assume that class attribute is the last attribute - This should be the case for all Sirius produced Arff files //split the instances into positive and negative Instances posInst = new Instances(applicationData.getDataset1Instances()); posInst.setClassIndex(posInst.numAttributes() - 1); for (int x = 0; x < posInst.numInstances();) if (posInst.instance(x).stringValue(posInst.numAttributes() - 1).equalsIgnoreCase("pos")) x++; else posInst.delete(x); posInst.deleteAttributeType(Attribute.STRING); Instances negInst = new Instances(applicationData.getDataset1Instances()); negInst.setClassIndex(negInst.numAttributes() - 1); for (int x = 0; x < negInst.numInstances();) if (negInst.instance(x).stringValue(negInst.numAttributes() - 1).equalsIgnoreCase("neg")) x++; else negInst.delete(x); negInst.deleteAttributeType(Attribute.STRING); //Train classifier one with the full dataset first then do cross-validation to gauge its accuracy long trainTimeStart = 0, trainTimeElapsed = 0; if (statusPane != null) statusPane.setText("Training Classifier One... May take a while... Please wait..."); //Record Start Time trainTimeStart = System.currentTimeMillis(); Instances fullInst = new Instances(applicationData.getDataset1Instances()); fullInst.setClassIndex(fullInst.numAttributes() - 1); Classifier classifierOne; if (m_ClassifierEditor != null) classifierOne = (Classifier) m_ClassifierEditor.getValue(); else classifierOne = Classifier.forName(classifierName, classifierOptions); if (outputClassifier) classifierOne.buildClassifier(fullInst); //Record Total Time used to build classifier one trainTimeElapsed = System.currentTimeMillis() - trainTimeStart; //Training Done String tclassifierName; if (m_ClassifierEditor != null) tclassifierName = m_ClassifierEditor.getValue().getClass().getName(); else tclassifierName = classifierName; if (classifierResults != null) { classifierResults.updateList(classifierResults.getClassifierList(), "Classifier: ", tclassifierName); classifierResults.updateList(classifierResults.getClassifierList(), "Training Data: ", " Jack Knife Validation"); classifierResults.updateList(classifierResults.getClassifierList(), "Time Used: ", Utils.doubleToString(trainTimeElapsed / 1000.0, 2) + " seconds"); } String classifierOneFilename = applicationData.getWorkingDirectory() + File.separator + "ClassifierOne_" + randomNumberForClassifier + ".scores"; BufferedWriter outputCrossValidation = new BufferedWriter(new FileWriter(classifierOneFilename)); //Instances foldTrainingInstance; //Instances foldTestingInstance; int positiveDataset1FromInt = applicationData.getPositiveDataset1FromField(); int positiveDataset1ToInt = applicationData.getPositiveDataset1ToField(); int negativeDataset1FromInt = applicationData.getNegativeDataset1FromField(); int negativeDataset1ToInt = applicationData.getNegativeDataset1ToField(); Step1TableModel positiveStep1TableModel = applicationData.getPositiveStep1TableModel(); Step1TableModel negativeStep1TableModel = applicationData.getNegativeStep1TableModel(); FastaFileManipulation fastaFile = new FastaFileManipulation(positiveStep1TableModel, negativeStep1TableModel, positiveDataset1FromInt, positiveDataset1ToInt, negativeDataset1FromInt, negativeDataset1ToInt, applicationData.getWorkingDirectory()); FastaFormat fastaFormat; String header[] = new String[fullInst.numInstances()]; String data[] = new String[fullInst.numInstances()]; int counter = 0; while ((fastaFormat = fastaFile.nextSequence("pos")) != null) { header[counter] = fastaFormat.getHeader(); data[counter] = fastaFormat.getSequence(); counter++; } while ((fastaFormat = fastaFile.nextSequence("neg")) != null) { header[counter] = fastaFormat.getHeader(); data[counter] = fastaFormat.getSequence(); counter++; } //run jack knife validation for (int x = 0; x < fullInst.numInstances(); x++) { if (applicationData.terminateThread == true) { if (statusPane != null) statusPane.setText("Interrupted - Classifier One Training Completed"); outputCrossValidation.close(); return classifierOne; } if (statusPane != null) statusPane.setText("Running " + (x + 1) + " / " + fullInst.numInstances()); Instances trainPosInst = new Instances(posInst); Instances trainNegInst = new Instances(negInst); Instance testInst; //split data into training and testing if (x < trainPosInst.numInstances()) { testInst = posInst.instance(x); trainPosInst.delete(x); } else { testInst = negInst.instance(x - posInst.numInstances()); trainNegInst.delete(x - posInst.numInstances()); } Instances trainInstances; if (trainPosInst.numInstances() < trainNegInst.numInstances()) { trainInstances = new Instances(trainPosInst); int max = (int) (ratio * trainPosInst.numInstances()); if (ratio == -1) max = trainNegInst.numInstances(); Random rand = new Random(1); for (int y = 0; y < trainNegInst.numInstances() && y < max; y++) { int index = rand.nextInt(trainNegInst.numInstances()); trainInstances.add(trainNegInst.instance(index)); trainNegInst.delete(index); } } else { trainInstances = new Instances(trainNegInst); int max = (int) (ratio * trainNegInst.numInstances()); if (ratio == -1) max = trainPosInst.numInstances(); Random rand = new Random(1); for (int y = 0; y < trainPosInst.numInstances() && y < max; y++) { int index = rand.nextInt(trainPosInst.numInstances()); trainInstances.add(trainPosInst.instance(index)); trainPosInst.delete(index); } } Classifier foldClassifier = tempClassifier; foldClassifier.buildClassifier(trainInstances); double[] results = foldClassifier.distributionForInstance(testInst); int classIndex = testInst.classIndex(); String classValue = testInst.toString(classIndex); outputCrossValidation.write(header[x]); outputCrossValidation.newLine(); outputCrossValidation.write(data[x]); outputCrossValidation.newLine(); if (classValue.equals("pos")) outputCrossValidation.write("pos,0=" + results[0]); else if (classValue.equals("neg")) outputCrossValidation.write("neg,0=" + results[0]); else { outputCrossValidation.close(); throw new Error("Invalid Class Type!"); } outputCrossValidation.newLine(); outputCrossValidation.flush(); } outputCrossValidation.close(); PredictionStats classifierOneStatsOnJackKnife = new PredictionStats(classifierOneFilename, range, threshold); totalTimeElapsed = System.currentTimeMillis() - totalTimeStart; if (classifierResults != null) classifierResults.updateList(classifierResults.getResultsList(), "Total Time Used: ", Utils.doubleToString(totalTimeElapsed / 60000, 2) + " minutes " + Utils.doubleToString((totalTimeElapsed / 1000.0) % 60.0, 2) + " seconds"); //if(classifierOneDisplayTextArea != null) classifierOneStatsOnJackKnife.updateDisplay(classifierResults, classifierOneDisplayTextArea, true); applicationData.setClassifierOneStats(classifierOneStatsOnJackKnife); if (myGraph != null) myGraph.setMyStats(classifierOneStatsOnJackKnife); if (statusPane != null) statusPane.setText("Done!"); if (returnClassifier) return classifierOne; else return classifierOneStatsOnJackKnife; } catch (Exception e) { e.printStackTrace(); JOptionPane.showMessageDialog(parent, e.getMessage(), "ERROR", JOptionPane.ERROR_MESSAGE); return null; } }
From source file:tr.gov.ulakbim.jDenetX.streams.filters.AddNoiseFilter.java
License:Open Source License
public Instance nextInstance() { Instance inst = (Instance) this.inputStream.nextInstance().copy(); for (int i = 0; i < inst.numAttributes(); i++) { double noiseFrac = i == inst.classIndex() ? this.classNoiseFractionOption.getValue() : this.attNoiseFractionOption.getValue(); if (inst.attribute(i).isNominal()) { DoubleVector obs = (DoubleVector) this.attValObservers.get(i); if (obs == null) { obs = new DoubleVector(); this.attValObservers.set(i, obs); }//from w w w .ja v a2 s .com int originalVal = (int) inst.value(i); if (!inst.isMissing(i)) { obs.addToValue(originalVal, inst.weight()); } if ((this.random.nextDouble() < noiseFrac) && (obs.numNonZeroEntries() > 1)) { do { inst.setValue(i, this.random.nextInt(obs.numValues())); } while (((int) inst.value(i) == originalVal) || (obs.getValue((int) inst.value(i)) == 0.0)); } } else { GaussianEstimator obs = (GaussianEstimator) this.attValObservers.get(i); if (obs == null) { obs = new GaussianEstimator(); this.attValObservers.set(i, obs); } obs.addObservation(inst.value(i), inst.weight()); inst.setValue(i, inst.value(i) + this.random.nextGaussian() * obs.getStdDev() * noiseFrac); } } return inst; }
From source file:xlong.urlclassify.others.SPegasos.java
License:Open Source License
/** * Updates the classifier with the given instance. * * @param instance the new training instance to include in the model * @exception Exception if the instance could not be incorporated in * the model./* w w w .java2 s.com*/ */ public void updateClassifier(Instance instance) throws Exception { if (!instance.classIsMissing()) { double learningRate = 1.0 / (m_lambda * m_t); //double scale = 1.0 - learningRate * m_lambda; double scale = 1.0 - 1.0 / m_t; double y = (instance.classValue() == 0) ? -1 : 1; double wx = dotProd(instance, m_weights, instance.classIndex()); double z = y * (wx + m_weights[m_weights.length - 1]); for (int j = 0; j < m_weights.length - 1; j++) { if (j != instance.classIndex()) { m_weights[j] *= scale; } } if (m_loss == LOGLOSS || (z < 1)) { double loss = dloss(z); int n1 = instance.numValues(); for (int p1 = 0; p1 < n1; p1++) { int indS = instance.index(p1); if (indS != instance.classIndex() && !instance.isMissingSparse(p1)) { double m = learningRate * loss * (instance.valueSparse(p1) * y); m_weights[indS] += m; } } // update the bias m_weights[m_weights.length - 1] += learningRate * loss * y; } double norm = 0; for (int k = 0; k < m_weights.length - 1; k++) { if (k != instance.classIndex()) { norm += (m_weights[k] * m_weights[k]); } } double scale2 = Math.min(1.0, (1.0 / (m_lambda * norm))); if (scale2 < 1.0) { scale2 = Math.sqrt(scale2); for (int j = 0; j < m_weights.length - 1; j++) { if (j != instance.classIndex()) { m_weights[j] *= scale2; } } } m_t++; } }
From source file:xlong.urlclassify.others.SPegasos.java
License:Open Source License
/** * Computes the distribution for a given instance * * @param instance the instance for which distribution is computed * @return the distribution/* w w w.ja v a 2s . c om*/ * @throws Exception if the distribution can't be computed successfully */ public double[] distributionForInstance(Instance inst) throws Exception { double[] result = new double[2]; if (m_replaceMissing != null) { m_replaceMissing.input(inst); inst = m_replaceMissing.output(); } if (m_nominalToBinary != null) { m_nominalToBinary.input(inst); inst = m_nominalToBinary.output(); } if (m_normalize != null) { m_normalize.input(inst); inst = m_normalize.output(); } double wx = dotProd(inst, m_weights, inst.classIndex());// * m_wScale; double z = (wx + m_weights[m_weights.length - 1]); //System.out.print("" + z + ": "); // System.out.println(1.0 / (1.0 + Math.exp(-z))); 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; }