List of usage examples for weka.core Instance insertAttributeAt
public void insertAttributeAt(int position);
From source file:ca.uottawa.balie.WekaLearner.java
License:Open Source License
private Instance CreateDoubleInstance(Object[] pi_Instance, String pi_Class) { double[] instval = new double[pi_Instance.length]; for (int i = 0; i != pi_Instance.length; ++i) { instval[i] = ((Double) pi_Instance[i]).doubleValue(); }/*w ww .j a va2 s. com*/ Instance inst = new Instance(1, instval); inst.insertAttributeAt(inst.numAttributes()); inst.setValue((Attribute) m_WekaAttributes.lastElement(), pi_Class); return inst; }
From source file:es.jarias.FMC.ClassCompoundTransformation.java
License:Open Source License
/** * //w w w . j a v a 2 s.c om * @param instance * @param labelIndices * @return tranformed instance * @throws Exception */ public Instance transformInstance(Instance instance, int[] labelIndices) throws Exception { Instance transformedInstance = RemoveAllLabels.transformInstance(instance, labelIndices); transformedInstance.setDataset(null); transformedInstance.insertAttributeAt(transformedInstance.numAttributes()); transformedInstance.setDataset(transformedFormat); return transformedInstance; }
From source file:meka.classifiers.multilabel.meta.MBR.java
License:Open Source License
@Override public double[] distributionForInstance(Instance instance) throws Exception { int c = instance.classIndex(); double result[] = m_BASE.distributionForInstance(instance); instance.setDataset(null);/* ww w . j a v a 2 s. co m*/ for (int i = 0; i < c; i++) { instance.insertAttributeAt(c); } instance.setDataset(m_InstancesTemplate); for (int i = 0; i < c; i++) { instance.setValue(c + i, result[i]); } return m_META.distributionForInstance(instance); }
From source file:meka.core.PSUtils.java
License:Open Source License
/** * Convert a multi-label instance into a multi-class instance, according to a template. *///w ww . j ava2 s. c om public static Instance convertInstance(Instance x, int L, Instances template) { Instance x_ = (Instance) x.copy(); x_.setDataset(null); for (int i = 0; i < L; i++) x_.deleteAttributeAt(0); x_.insertAttributeAt(0); x_.setDataset(template); return x_; }
From source file:moa.clusterers.AmidstClusteringAlgorithm.java
License:Apache License
/** * {@inheritDoc}// w w w .j ava2 s .c o m */ @Override public Clustering getClusteringResult() { //sourceClustering = new Clustering(); Instances dataset = getDataset(attributes_.getNumberOfAttributes(), getNumClusters()); Instances newInstances = new Instances(dataset); if (bnModel_ == null) { //parameterLearningAlgorithm_.setParallelMode(isParallelMode_()); parameterLearningAlgorithm_.setDAG(dag); ((SVB) parameterLearningAlgorithm_).setWindowsSize(timeWindowOption.getValue()); parameterLearningAlgorithm_.initLearning(); parameterLearningAlgorithm_.updateModel(batch_); } else { parameterLearningAlgorithm_.updateModel(batch_); } bnModel_ = parameterLearningAlgorithm_.getLearntBayesianNetwork(); predictions_.setModel(bnModel_); for (DataInstance dataInstance : batch_) { this.predictions_.setEvidence(dataInstance); this.predictions_.runInference(); Multinomial multinomial = this.predictions_.getPosterior(clusterVar_); double[] results = multinomial.getProbabilities(); int cnum = IntStream.rangeClosed(0, getNumClusters() - 1) .reduce((a, b) -> (results[a] > results[b]) ? a : b).getAsInt(); double[] attValues = dataInstance.toArray(); Instance newInst = new DenseInstance(1.0, attValues); newInst.insertAttributeAt(attributes_.getNumberOfAttributes()); newInst.setDataset(dataset); newInst.setClassValue(cnum); newInstances.add(newInst); } clustering = new Clustering(newInstances); return clustering; }
From source file:moa.core.utils.Converter.java
License:Open Source License
public Instance formatInstance(Instance original) { //Copy the original instance Instance converted = (Instance) original.copy(); converted.setDataset(null);//from w w w .j a v a 2 s .c o m //Delete all class attributes for (int j = 0; j < m_L; j++) { converted.deleteAttributeAt(0); } //Add one of those class attributes at the begginning converted.insertAttributeAt(0); //Hopefully setting the dataset will configure that attribute properly converted.setDataset(m_InstancesTemplate); return converted; }
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()); }//from w w w . j av a 2 s . c o m 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); }/* www. ja va 2s . 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; }
From source file:mulan.classifier.meta.thresholding.ThresholdPrediction.java
License:Open Source License
@Override protected MultiLabelOutput makePredictionInternal(Instance instance) throws Exception { boolean[] predictedLabels = new boolean[numLabels]; Instance modifiedIns = modifiedInstanceX(instance, metaDatasetChoice); modifiedIns.insertAttributeAt(modifiedIns.numAttributes()); // set dataset to instance modifiedIns.setDataset(classifierInstances); double bipartition_key = classifier.classifyInstance(modifiedIns); MultiLabelOutput mlo = baseLearner.makePrediction(instance); double[] arrayOfScores = new double[numLabels]; arrayOfScores = mlo.getConfidences(); for (int i = 0; i < numLabels; i++) { if (arrayOfScores[i] >= bipartition_key) { predictedLabels[i] = true;// w ww. j a v a 2s . co m } else { predictedLabels[i] = false; } } MultiLabelOutput final_mlo = new MultiLabelOutput(predictedLabels, mlo.getConfidences()); return final_mlo; }
From source file:mulan.classifier.transformation.CalibratedLabelRanking.java
License:Open Source License
/** * This method does a prediction for an instance with the values of label missing * @param instance/*from ww w . j a v a 2 s.c om*/ * @return prediction * @throws java.lang.Exception */ public MultiLabelOutput makePredictionStandard(Instance instance) throws Exception { boolean[] bipartition = new boolean[numLabels]; double[] confidences = new double[numLabels]; int[] voteLabel = new int[numLabels + 1]; //System.out.println("Instance:" + instance.toString()); // delete all labels and add a new atribute at the end Instance newInstance = RemoveAllLabels.transformInstance(instance, labelIndices); newInstance.insertAttributeAt(newInstance.numAttributes()); //initialize the array voteLabel Arrays.fill(voteLabel, 0); int counter = 0; for (int label1 = 0; label1 < numLabels - 1; label1++) { for (int label2 = label1 + 1; label2 < numLabels; label2++) { if (!nodata[counter]) { double distribution[] = new double[2]; try { newInstance.setDataset(metaDataTest[counter]); distribution = oneVsOneModels[counter].distributionForInstance(newInstance); } 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 = metaDataTest[counter].classAttribute(); if (classAttribute.value(maxIndex).equals("1")) { voteLabel[label1]++; } else { voteLabel[label2]++; } } counter++; } } int voteVirtual = 0; MultiLabelOutput virtualMLO = virtualLabelModels.makePrediction(instance); boolean[] virtualBipartition = virtualMLO.getBipartition(); for (int i = 0; i < numLabels; i++) { if (virtualBipartition[i]) { voteLabel[i]++; } else { voteVirtual++; } } for (int i = 0; i < numLabels; i++) { if (voteLabel[i] >= voteVirtual) { bipartition[i] = true; } else { bipartition[i] = false; } confidences[i] = 1.0 * voteLabel[i] / numLabels; } MultiLabelOutput mlo = new MultiLabelOutput(bipartition, confidences); return mlo; }