List of usage examples for weka.core Instance numAttributes
public int numAttributes();
From source file:moa.clusterers.AmidstClusteringAlgorithm.java
License:Apache License
/** * {@inheritDoc}// ww w. j a v a2 s . c om */ @Override public void trainOnInstanceImpl(Instance instance) { if (batch_ == null) { setParallelMode_(parallelModeOption.isSet()); setNumClusters(numberClustersOption.getValue()); attributes_ = Converter .convertAttributes(getDataset(instance.numAttributes(), 0).enumerateAttributes()); Variables modelHeader = new Variables(attributes_); clusterVar_ = modelHeader.newMultinomialVariable("clusterVar", getNumClusters()); batch_ = new DataOnMemoryListContainer(attributes_); predictions_ = new VMP(); predictions_.setSeed(this.randomSeed); dag = new DAG(modelHeader); /* Set DAG structure. */ /* Add the hidden cluster variable as a parent of all the predictive attributes. */ if (isParallelMode_()) { dag.getParentSets().parallelStream() .filter(w -> w.getMainVar().getVarID() != clusterVar_.getVarID()) .forEach(w -> w.addParent(clusterVar_)); } else { dag.getParentSets().stream().filter(w -> w.getMainVar().getVarID() != clusterVar_.getVarID()) .filter(w -> w.getMainVar().isObservable()).forEach(w -> w.addParent(clusterVar_)); } System.out.println(dag.toString()); parameterLearningAlgorithm_ = new SVB(); } if (windowCounter >= timeWindowOption.getValue()) { batch_ = new DataOnMemoryListContainer(attributes_); windowCounter = 0; } DataInstance dataInstance = new DataInstanceFromDataRow(new DataRowWeka(instance, attributes_)); windowCounter++; batch_.add(dataInstance); }
From source file:moa.clusterers.clustree.ClusTree.java
License:Apache License
@Override public void trainOnInstanceImpl(Instance instance) { timestamp++;/*from ww w . j a v a2s . com*/ //TODO check if instance contains label if (root == null) { numberDimensions = instance.numAttributes(); root = new Node(numberDimensions, 0); } else { if (numberDimensions != instance.numAttributes()) System.out.println( "Wrong dimensionality, expected:" + numberDimensions + "found:" + instance.numAttributes()); } ClusKernel newPointAsKernel = new ClusKernel(instance.toDoubleArray(), numberDimensions); insert(newPointAsKernel, new SimpleBudget(1000), timestamp); }
From source file:moa.clusterers.outliers.AnyOut.AnyOut.java
License:Apache License
@Override protected void ProcessNewStreamObj(Instance i) { if (trainingSetSize >= trainingCount) { if (trainingSet == null) { trainingSet = new DataSet(i.numAttributes() - 1); }/*w ww. ja v a 2 s .co m*/ //fill training set DataObject o = new DataObject(idCounter++, i); trainingSet.addObject(o); trainingCount++; } else { // Train once. if (trainingSetSize != -1) { anyout.train(trainingSet); trainingSet.clear(); trainingSetSize = -1; outlierClass = i.classAttribute().numValues() - 1; } // Create DataObject from instance. DataObject o = new DataObject(idCounter++, i); objects.add(o); // Count ground truth. if (o.getClassLabel() == outlierClass) { totalOutliers += 1; } // Update window objects. if (objects.size() > windowSize) { DataObject obj = objects.get(0); objects.remove(0); anyout.removeObject(obj.getId()); RemoveExpiredOutlier(new Outlier(obj.getInstance(), obj.getId(), obj)); } // Calculate scores for the object. anyout.initObject(o.getId(), o.getFeatures()); // Simulate anyout characteristics. double depth = Math.random(); if (depth < minDepth) { depth = minDepth; } else if (depth > maxDepth) { depth = maxDepth; } while (anyout.moreImprovementsPossible(o.getId(), depth)) { anyout.improveObjectOnce(o.getId()); } // Learn object into ClusTree. anyout.learnObject(o.getFeatures()); // Evaluation of the window objects. for (DataObject obj : objects) { int id = obj.getId(); if (anyout.isOutlier(id)) { if (obj.isOutiler() == false) { // not already outlier. // Statistics gathering. if (obj.getClassLabel() == outlierClass) { truePositive += 1; } else { falsePositive += 1; } AddOutlier(new Outlier(obj.getInstance(), id, obj)); obj.setOutiler(true); } } else { RemoveOutlier(new Outlier(obj.getInstance(), id, obj)); obj.setOutiler(false); } } } }
From source file:moa.evaluation.BasicConceptDriftPerformanceEvaluator.java
License:Open Source License
@Override public void addResult(Instance inst, double[] classVotes) { //classVotes[0] -> is Change //classVotes[1] -> is in Warning Zone //classVotes[2] -> delay //classVotes[3] -> estimation this.inputValues = inst.value(2); if (inst.weight() > 0.0 && classVotes.length == 4) { if (inst.numAttributes() > 1) { //if there is ground truth we monitor delay this.delay++; }// ww w.java2 s . co m this.weightObserved += inst.weight(); if (classVotes[0] == 1.0) { //Change detected //System.out.println("Change detected with delay "+ this.delay ); this.numberDetections += inst.weight(); if (this.hasChangeOccurred == true) { this.totalDelay += this.delay - classVotes[2]; this.numberDetectionsOccurred += inst.weight(); this.hasChangeOccurred = false; } } if (this.hasChangeOccurred && classVotes[1] == 1.0) { //Warning detected //System.out.println("Warning detected at "+getTotalWeightObserved()); if (this.isWarningZone == false) { this.numberWarnings += inst.weight(); this.isWarningZone = true; } } else { this.isWarningZone = false; } if (inst.numAttributes() > 1) { if (inst.value(inst.numAttributes() - 2) == 1.0) {//Attribute 1 //Ground truth Change this.numberChanges += inst.weight(); this.delay = 0; this.hasChangeOccurred = true; } } //Compute error prediction if (classVotes.length > 1) { this.errorPrediction += Math.abs(classVotes[3] - inst.value(0)); } } }
From source file:moa.streams.clustering.FileStream.java
License:Apache License
protected boolean readNextInstanceFromFile() { try {//from w w w . j a v a2 s . c o m if (this.instances.readInstance(this.fileReader)) { Instance rawInstance = this.instances.instance(0); //remove dataset from instance so we can delete attributes rawInstance.setDataset(null); for (int i = removeAttributes.length - 1; i >= 0; i--) { rawInstance.deleteAttributeAt(removeAttributes[i]); } //set adjusted dataset for instance rawInstance.setDataset(filteredDataset); if (normalizeOption.isSet() && valuesMinMaxDiff != null) { for (int i = 0; i < rawInstance.numAttributes(); i++) { if (valuesMinMaxDiff.get(i)[2] != 1 && // Already normalized valuesMinMaxDiff.get(i)[2] != 0 && // Max. value is 0 (unable to be normalized) i != rawInstance.classIndex()) { // Class label is not subject to be normalized double v = rawInstance.value(i); v = (v - valuesMinMaxDiff.get(i)[0]) / valuesMinMaxDiff.get(i)[2]; rawInstance.setValue(i, v); } } } this.lastInstanceRead = rawInstance; this.instances.delete(); // keep instances clean this.numInstancesRead++; return true; } if (this.fileReader != null) { this.fileReader.close(); this.fileReader = null; } return false; } catch (IOException ioe) { throw new RuntimeException("ArffFileStream failed to read instance from stream.", ioe); } }
From source file:moa.streams.filters.AddNoiseFilter.java
License:Open Source License
@Override 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 ww . ja v a 2 s . c o m*/ 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:moa.streams.filters.ReplacingMissingValuesFilter.java
License:Open Source License
@Override public Instance nextInstance() { Instance inst = (Instance) this.inputStream.nextInstance().copy(); // Initialization if (numAttributes < 0) { numAttributes = inst.numAttributes(); columnsStatistics = new double[numAttributes]; numberOfSamples = new long[numAttributes]; lastNominalValues = new String[numAttributes]; frequencies = new HashMap[numAttributes]; for (int i = 0; i < inst.numAttributes(); i++) { if (inst.attribute(i).isNominal()) frequencies[i] = new HashMap<String, Integer>(); }// w w w . j a va2 s. com numericalSelectedStrategy = this.numericReplacementStrategyOption.getChosenIndex(); nominalSelectedStrategy = this.nominalReplacementStrategyOption.getChosenIndex(); } for (int i = 0; i < numAttributes; i++) { // ---- Numerical values ---- if (inst.attribute(i).isNumeric()) { // Handle missing value if (inst.isMissing(i)) { switch (numericalSelectedStrategy) { case 0: // NOTHING break; case 1: // LAST KNOWN VALUE case 2: // MEAN case 3: // MAX case 4: // MIN inst.setValue(i, columnsStatistics[i]); break; case 5: // CONSTANT inst.setValue(i, numericalConstantValueOption.getValue()); break; default: continue; } } // Update statistics with non-missing values else { switch (numericalSelectedStrategy) { case 1: // LAST KNOWN VALUE columnsStatistics[i] = inst.value(i); break; case 2: // MEAN numberOfSamples[i]++; columnsStatistics[i] = columnsStatistics[i] + (inst.value(i) - columnsStatistics[i]) / numberOfSamples[i]; break; case 3: // MAX columnsStatistics[i] = columnsStatistics[i] < inst.value(i) ? inst.value(i) : columnsStatistics[i]; break; case 4: // MIN columnsStatistics[i] = columnsStatistics[i] > inst.value(i) ? inst.value(i) : columnsStatistics[i]; break; default: continue; } } } // ---- Nominal values ---- else if (inst.attribute(i).isNominal()) { // Handle missing value if (inst.isMissing(i)) { switch (nominalSelectedStrategy) { case 0: // NOTHING break; case 1: // LAST KNOWN VALUE if (lastNominalValues[i] != null) { inst.setValue(i, lastNominalValues[i]); } break; case 2: // MODE if (!frequencies[i].isEmpty()) { // Sort the map to get the most frequent value Map<String, Integer> sortedMap = MapUtil.sortByValue(frequencies[i]); inst.setValue(i, sortedMap.entrySet().iterator().next().getKey()); } break; default: continue; } } // Update statistics with non-missing values else { switch (nominalSelectedStrategy) { case 1: // LAST KNOWN VALUE lastNominalValues[i] = inst.stringValue(i); break; case 2: // MODE Integer previousCounter = frequencies[i].get(inst.stringValue(i)); if (previousCounter == null) previousCounter = 0; frequencies[i].put(inst.stringValue(i), ++previousCounter); break; default: continue; } } } } return inst; }
From source file:mulan.classifier.lazy.IBLR_ML.java
License:Open Source License
protected MultiLabelOutput makePredictionInternal(Instance instance) throws Exception { double[] conf_corrected = new double[numLabels]; double[] confidences = new double[numLabels]; Instances knn = new Instances(lnn.kNearestNeighbours(instance, numOfNeighbors)); /*// w w w . ja va 2 s . co m * Get the label confidence vector. */ for (int i = 0; i < numLabels; i++) { // compute sum of counts for each label in KNN double count_for_label_i = 0; for (int k = 0; k < numOfNeighbors; k++) { double value = Double.parseDouble( train.attribute(labelIndices[i]).value((int) knn.instance(k).value(labelIndices[i]))); if (Utils.eq(value, 1.0)) { count_for_label_i++; } } confidences[i] = count_for_label_i / numOfNeighbors; } double[] attvalue = new double[numLabels + 1]; if (addFeatures == true) { attvalue = new double[instance.numAttributes() + 1]; // Copy the original features for (int m = 0; m < featureIndices.length; m++) { attvalue[m] = instance.value(featureIndices[m]); } // Copy the label confidences as additional features for (int m = 0; m < confidences.length; m++) { attvalue[train.numAttributes() - numLabels + m] = confidences[m]; } } else { // Copy the label confidences as additional features for (int m = 0; m < confidences.length; m++) { attvalue[m] = confidences[m]; } } // Add the class labels and finish the new training data for (int j = 0; j < numLabels; j++) { attvalue[attvalue.length - 1] = instance.value(train.numAttributes() - numLabels + j); Instance newInst = DataUtils.createInstance(instance, 1, attvalue); conf_corrected[j] = classifier[j].distributionForInstance(newInst)[1]; } MultiLabelOutput mlo = new MultiLabelOutput(conf_corrected, 0.5); return mlo; }
From source file:mulan.classifier.meta.HOMER.java
License:Open Source License
protected MultiLabelOutput makePredictionInternal(Instance instance) throws Exception { Instance transformed = DataUtils.createInstance(instance, instance.weight(), instance.toDoubleArray()); for (int i = 0; i < numMetaLabels; i++) { transformed.insertAttributeAt(transformed.numAttributes()); }/* ww w. j a v a2 s . com*/ transformed.setDataset(header); MultiLabelOutput mlo = hmc.makePrediction(transformed); boolean[] oldBipartition = mlo.getBipartition(); //System.out.println("old:" + Arrays.toString(oldBipartition)); boolean[] newBipartition = new boolean[numLabels]; System.arraycopy(oldBipartition, 0, newBipartition, 0, numLabels); //System.out.println("new:" + Arrays.toString(newBipartition)); double[] oldConfidences = mlo.getConfidences(); double[] newConfidences = new double[numLabels]; System.arraycopy(oldConfidences, 0, newConfidences, 0, numLabels); MultiLabelOutput newMLO = new MultiLabelOutput(newBipartition, newConfidences); return newMLO; }
From source file:mulan.classifier.meta.thresholding.MetaLabeler.java
License:Open Source License
@Override protected MultiLabelOutput makePredictionInternal(Instance instance) throws Exception { //System.out.println(instance); MultiLabelOutput mlo = baseLearner.makePrediction(instance); int[] arrayOfRankink = new int[numLabels]; boolean[] predictedLabels = new boolean[numLabels]; Instance modifiedIns = modifiedInstanceX(instance, metaDatasetChoice); //System.out.println(modifiedIns); modifiedIns.insertAttributeAt(modifiedIns.numAttributes()); // set dataset to instance modifiedIns.setDataset(classifierInstances); //get the bipartition_key after classify the instance int bipartition_key; if (classChoice.compareTo("Nominal-Class") == 0) { double classify_key = classifier.classifyInstance(modifiedIns); String s = classifierInstances.attribute(classifierInstances.numAttributes() - 1) .value((int) classify_key); bipartition_key = Integer.valueOf(s); } else { //Numeric-Class double classify_key = classifier.classifyInstance(modifiedIns); bipartition_key = (int) Math.round(classify_key); }// w w w. j a v a2 s . c o m if (mlo.hasRanking()) { arrayOfRankink = mlo.getRanking(); for (int i = 0; i < numLabels; i++) { if (arrayOfRankink[i] <= bipartition_key) { predictedLabels[i] = true; } else { predictedLabels[i] = false; } } } MultiLabelOutput final_mlo = new MultiLabelOutput(predictedLabels, mlo.getConfidences()); return final_mlo; }