List of usage examples for weka.core Instance setValue
public void setValue(Attribute att, String value);
From source file:moa.streams.generators.MySEAGenerator.java
License:Open Source License
@Override public Instance nextInstance() { double attrib1 = 0, attrib2 = 0, attrib3 = 0; int group = 0; boolean desiredClassFound = false; while (!desiredClassFound) { // generate attributes attrib1 = 10 * this.instanceRandom.nextDouble(); attrib2 = 10 * this.instanceRandom.nextDouble(); attrib3 = 10 * this.instanceRandom.nextDouble(); // determine class group = classificationFunctions[this.functionOption.getValue() - 1].determineClass(attrib1, attrib2, attrib3);/*w ww . j a va 2 s . com*/ if (!this.balanceClassesOption.isSet()) { desiredClassFound = true; } else { // balance the classes if ((this.nextClassShouldBeZero && (group == 0)) || (!this.nextClassShouldBeZero && (group == 1))) { desiredClassFound = true; this.nextClassShouldBeZero = !this.nextClassShouldBeZero; } // else keep searching } } //Add Noise if ((1 + (this.instanceRandom.nextInt(100))) <= this.noisePercentageOption.getValue()) { group = (group == 0 ? 1 : 0); } // construct instance InstancesHeader header = getHeader(); Instance inst = new DenseInstance(header.numAttributes()); inst.setValue(0, attrib1); inst.setValue(1, attrib2); inst.setValue(2, attrib3); inst.setDataset(header); inst.setClassValue(group); return inst; }
From source file:moa.streams.generators.RandomTreeGenerator.java
License:Open Source License
@Override public Instance nextInstance() { double[] attVals = new double[this.numNominalsOption.getValue() + this.numNumericsOption.getValue()]; InstancesHeader header = getHeader(); Instance inst = new DenseInstance(header.numAttributes()); for (int i = 0; i < attVals.length; i++) { attVals[i] = i < this.numNominalsOption.getValue() ? this.instanceRandom.nextInt(this.numValsPerNominalOption.getValue()) : this.instanceRandom.nextDouble(); inst.setValue(i, attVals[i]); }/*from w w w. jav a2s. c om*/ inst.setDataset(header); inst.setClassValue(classifyInstance(this.treeRoot, attVals)); return inst; }
From source file:moa.streams.generators.STAGGERGenerator.java
License:Open Source License
@Override public Instance nextInstance() { int size = 0, color = 0, shape = 0, group = 0; boolean desiredClassFound = false; while (!desiredClassFound) { // generate attributes size = this.instanceRandom.nextInt(3); color = this.instanceRandom.nextInt(3); shape = this.instanceRandom.nextInt(3); // determine class group = classificationFunctions[this.functionOption.getValue() - 1].determineClass(size, color, shape); if (!this.balanceClassesOption.isSet()) { desiredClassFound = true;//ww w.j a v a2 s .co m } else { // balance the classes if ((this.nextClassShouldBeZero && (group == 0)) || (!this.nextClassShouldBeZero && (group == 1))) { desiredClassFound = true; this.nextClassShouldBeZero = !this.nextClassShouldBeZero; } // else keep searching } } // construct instance InstancesHeader header = getHeader(); Instance inst = new DenseInstance(header.numAttributes()); inst.setValue(0, size); inst.setValue(1, color); inst.setValue(2, shape); inst.setDataset(header); inst.setClassValue(group); return inst; }
From source file:moa.streams.generators.STAGGERGenerator2.java
License:Open Source License
@Override public Instance nextInstance() { int size = 0, color = 0, shape = 0, group = 0; boolean desiredClassFound = false; while (!desiredClassFound) { // generate attributes size = this.instanceRandom.nextInt(3); color = this.instanceRandom.nextInt(3); shape = this.instanceRandom.nextInt(3); // determine class group = classificationFunctions[this.functionOption.getValue() - 1].determineClass(size, color, shape); if (!this.balanceClassesOption.isSet()) { desiredClassFound = true;/* w w w.jav a 2 s . c o m*/ } else { // balance the classes if ((this.nextClassShouldBeZero && (group == 0)) || (!this.nextClassShouldBeZero && (group == 1))) { desiredClassFound = true; this.nextClassShouldBeZero = !this.nextClassShouldBeZero; } // else keep searching } } //TODO refactor //sudden drift flag = flag + 1; if (flag > this.changeIntervalOption.getValue()) { this.functionOption.setValue(3); } if (flag > 2 * this.changeIntervalOption.getValue()) { this.functionOption.setValue(2); } if (flag > 3 * this.changeIntervalOption.getValue()) { this.functionOption.setValue(2); } // construct instance InstancesHeader header = getHeader(); Instance inst = new DenseInstance(header.numAttributes()); inst.setValue(0, size); inst.setValue(1, color); inst.setValue(2, shape); inst.setDataset(header); inst.setClassValue(group); return inst; }
From source file:moa.streams.generators.WaveformGenerator.java
License:Open Source License
@Override public Instance nextInstance() { InstancesHeader header = getHeader(); Instance inst = new DenseInstance(header.numAttributes()); inst.setDataset(header);/*from w w w. ja v a2s.c o m*/ int waveform = this.instanceRandom.nextInt(NUM_CLASSES); int choiceA = 0, choiceB = 0; switch (waveform) { case 0: choiceA = 0; choiceB = 1; break; case 1: choiceA = 0; choiceB = 2; break; case 2: choiceA = 1; choiceB = 2; break; } double multiplierA = this.instanceRandom.nextDouble(); double multiplierB = 1.0 - multiplierA; for (int i = 0; i < NUM_BASE_ATTRIBUTES; i++) { inst.setValue(i, (multiplierA * hFunctions[choiceA][i]) + (multiplierB * hFunctions[choiceB][i]) + this.instanceRandom.nextGaussian()); } if (this.addNoiseOption.isSet()) { for (int i = NUM_BASE_ATTRIBUTES; i < TOTAL_ATTRIBUTES_INCLUDING_NOISE; i++) { inst.setValue(i, this.instanceRandom.nextGaussian()); } } inst.setClassValue(waveform); return inst; }
From source file:moa.streams.generators.WaveformGeneratorDrift.java
License:Open Source License
@Override public Instance nextInstance() { InstancesHeader header = getHeader(); Instance inst = new DenseInstance(header.numAttributes()); inst.setDataset(header);// ww w .j a va 2 s . c om int waveform = this.instanceRandom.nextInt(NUM_CLASSES); int choiceA = 0, choiceB = 0; switch (waveform) { case 0: choiceA = 0; choiceB = 1; break; case 1: choiceA = 0; choiceB = 2; break; case 2: choiceA = 1; choiceB = 2; break; } double multiplierA = this.instanceRandom.nextDouble(); double multiplierB = 1.0 - multiplierA; for (int i = 0; i < NUM_BASE_ATTRIBUTES; i++) { inst.setValue(this.numberAttribute[i], (multiplierA * hFunctions[choiceA][i]) + (multiplierB * hFunctions[choiceB][i]) + this.instanceRandom.nextGaussian()); } if (this.addNoiseOption.isSet()) { for (int i = NUM_BASE_ATTRIBUTES; i < TOTAL_ATTRIBUTES_INCLUDING_NOISE; i++) { inst.setValue(this.numberAttribute[i], this.instanceRandom.nextGaussian()); } } inst.setClassValue(waveform); return inst; }
From source file:mulan.classifier.meta.thresholding.Meta.java
License:Open Source License
/** * A method that modify an instance//from w w w .ja v a2 s .c o m * * @param instance to modified * @param xBased the type for constructing the meta dataset * @return a transformed instance for the predictor of labels/threshold */ protected Instance modifiedInstanceX(Instance instance, String xBased) { Instance modifiedIns = null; MultiLabelOutput mlo = null; if (xBased.compareTo("Content-Based") == 0) { Instance tempInstance = RemoveAllLabels.transformInstance(instance, labelIndices); modifiedIns = DataUtils.createInstance(tempInstance, tempInstance.weight(), tempInstance.toDoubleArray()); } else if (xBased.compareTo("Score-Based") == 0) { double[] arrayOfScores = new double[numLabels]; try { mlo = baseLearner.makePrediction(instance); } catch (InvalidDataException ex) { Logger.getLogger(Meta.class.getName()).log(Level.SEVERE, null, ex); } catch (ModelInitializationException ex) { Logger.getLogger(Meta.class.getName()).log(Level.SEVERE, null, ex); } catch (Exception ex) { Logger.getLogger(Meta.class.getName()).log(Level.SEVERE, null, ex); } arrayOfScores = mlo.getConfidences(); modifiedIns = DataUtils.createInstance(instance, numLabels); for (int i = 0; i < numLabels; i++) { modifiedIns.setValue(i, arrayOfScores[i]); } } else { //Rank-Based try { //Rank-Based double[] arrayOfScores = new double[numLabels]; mlo = baseLearner.makePrediction(instance); arrayOfScores = mlo.getConfidences(); ArrayList<Double> list = new ArrayList(); for (int i = 0; i < numLabels; i++) { list.add(arrayOfScores[i]); } Collections.sort(list); modifiedIns = DataUtils.createInstance(instance, numLabels); int j = numLabels - 1; for (Double x : list) { modifiedIns.setValue(j, x); j--; } } catch (InvalidDataException ex) { Logger.getLogger(Meta.class.getName()).log(Level.SEVERE, null, ex); } catch (ModelInitializationException ex) { Logger.getLogger(Meta.class.getName()).log(Level.SEVERE, null, ex); } catch (Exception ex) { Logger.getLogger(Meta.class.getName()).log(Level.SEVERE, null, ex); } } return modifiedIns; }
From source file:mulan.classifier.neural.NormalizationFilter.java
License:Open Source License
/** * Performs a normalization of numerical attributes on given instance. * The instance must conform to format of instances data the {@link NormalizationFilter} * was initialized with.// w ww.j a va 2s . c o m * @param instance the instance to be normalized */ public void normalize(Instance instance) { Set<Integer> normScope = attStats.keySet(); for (Integer attIndex : normScope) { double[] stats = attStats.get(attIndex); double attMin = stats[0]; double attMax = stats[1]; double value = instance.value(attIndex); if (attMin == attMax) { instance.setValue(attIndex, minValue); } else { instance.setValue(attIndex, (((value - stats[0]) / (stats[1] - stats[0])) * (maxValue - minValue)) + minValue); } } }
From source file:mulan.classifier.transformation.CalibratedLabelRanking.java
License:Open Source License
@Override protected void buildInternal(MultiLabelInstances trainingSet) throws Exception { // Virtual label models debug("Building calibration label models"); System.out.println("Building calibration label models"); virtualLabelModels = new BinaryRelevance(getBaseClassifier()); virtualLabelModels.setDebug(getDebug()); virtualLabelModels.build(trainingSet); // One-vs-one models numModels = ((numLabels) * (numLabels - 1)) / 2; oneVsOneModels = AbstractClassifier.makeCopies(getBaseClassifier(), numModels); nodata = new boolean[numModels]; metaDataTest = new Instances[numModels]; Instances trainingData = trainingSet.getDataSet(); int counter = 0; // Creation of one-vs-one models for (int label1 = 0; label1 < numLabels - 1; label1++) { // Attribute of label 1 Attribute attrLabel1 = trainingData.attribute(labelIndices[label1]); for (int label2 = label1 + 1; label2 < numLabels; label2++) { debug("Building one-vs-one model " + (counter + 1) + "/" + numModels); System.out.println("Building one-vs-one model " + (counter + 1) + "/" + numModels); // Attribute of label 2 Attribute attrLabel2 = trainingData.attribute(labelIndices[label2]); // initialize training set Instances dataOneVsOne = new Instances(trainingData, 0); // filter out examples with no preference for (int i = 0; i < trainingData.numInstances(); i++) { Instance tempInstance; if (trainingData.instance(i) instanceof SparseInstance) { tempInstance = new SparseInstance(trainingData.instance(i)); } else { tempInstance = new DenseInstance(trainingData.instance(i)); }//w w w.j ava 2 s .c o m int nominalValueIndex; nominalValueIndex = (int) tempInstance.value(labelIndices[label1]); String value1 = attrLabel1.value(nominalValueIndex); nominalValueIndex = (int) tempInstance.value(labelIndices[label2]); String value2 = attrLabel2.value(nominalValueIndex); if (!value1.equals(value2)) { tempInstance.setValue(attrLabel1, value1); dataOneVsOne.add(tempInstance); } } // remove all labels apart from label1 and place it at the end Reorder filter = new Reorder(); int numPredictors = trainingData.numAttributes() - numLabels; int[] reorderedIndices = new int[numPredictors + 1]; for (int i = 0; i < numPredictors; i++) { reorderedIndices[i] = featureIndices[i]; } reorderedIndices[numPredictors] = labelIndices[label1]; filter.setAttributeIndicesArray(reorderedIndices); filter.setInputFormat(dataOneVsOne); dataOneVsOne = Filter.useFilter(dataOneVsOne, filter); //System.out.println(dataOneVsOne.toString()); dataOneVsOne.setClassIndex(numPredictors); // build model label1 vs label2 if (dataOneVsOne.size() > 0) { oneVsOneModels[counter].buildClassifier(dataOneVsOne); } else { nodata[counter] = true; } dataOneVsOne.delete(); metaDataTest[counter] = dataOneVsOne; counter++; } } }
From source file:mulan.classifier.transformation.ClassifierChain.java
License:Open Source License
protected MultiLabelOutput makePredictionInternal(Instance instance) throws Exception { boolean[] bipartition = new boolean[numLabels]; double[] confidences = new double[numLabels]; Instance tempInstance = DataUtils.createInstance(instance, instance.weight(), instance.toDoubleArray()); for (int counter = 0; counter < numLabels; counter++) { double distribution[] = new double[2]; try {/*from ww w . ja v a 2s . c om*/ distribution = ensemble[counter].distributionForInstance(tempInstance); } catch (Exception e) { System.out.println(e); return null; } int maxIndex = (distribution[0] > distribution[1]) ? 0 : 1; // Ensure correct predictions both for class values {0,1} and {1,0} Attribute classAttribute = ensemble[counter].getFilter().getOutputFormat().classAttribute(); bipartition[chain[counter]] = (classAttribute.value(maxIndex).equals("1")) ? true : false; // The confidence of the label being equal to 1 confidences[chain[counter]] = distribution[classAttribute.indexOfValue("1")]; tempInstance.setValue(labelIndices[chain[counter]], maxIndex); } MultiLabelOutput mlo = new MultiLabelOutput(bipartition, confidences); return mlo; }