Refine search
System.out.println("--------------------------"); double[] instanceValue1 = new double[dataRaw.numAttributes()]; dataRaw.add(new DenseInstance(1.0, instanceValue1)); System.out.println("--------------------------"); double[] instanceValue2 = new double[dataRaw.numAttributes()]; dataRaw.add(new DenseInstance(1.0, instanceValue2));
ArrayList<Attribute> attributes = new ArrayList<Attribute>(); attributes.add(new Attribute("x")); attributes.add(new Attribute("y")); attributes.add(new Attribute("z")); Instances dataRaw = new Instances("TestInstances", attributes , 0); dataRaw.setClassIndex(dataRaw.numAttributes() - 1); // Assuming z (z on lastindex) as classindex for (Double[] a: myValues) { dataRaw.add(new DenseInstance(1.0, a)); } // Then train or build the algorithm/model on instances (dataRaw) created above. MultilayerPerceptron mlp = new MultilayerPerceptron(); // Sample algorithm, go through about neural networks to use this or replace with appropriate algorithm. mlp.buildClassifier(dataRaw); // Create a test instance,I think you can create testinstance without // classindex value but cross check in weka as I forgot about it. double[] values = new double[]{-818.84, 9186.82, 2436.73}; // sample values DenseInstance testInstance = new DenseInstance(1.0, values); testInstance.setDataset(dataRaw); // To associate with instances object // now you can clasify double classify = mlp.classifyInstance(testInstance);
/** * Calculates the centroid pivot of a node. The node is given * in the form of an indices array that contains the * indices of the points inside the node. * @param instList The indices array pointing to the * instances in the node. * @param insts The actual instances. The instList * points to instances in this object. * @return The calculated centre/pivot of the node. */ public static Instance calcCentroidPivot(int[] instList, Instances insts) { double[] attrVals = new double[insts.numAttributes()]; Instance temp; for(int i=0; i<instList.length; i++) { temp = insts.instance(instList[i]); for(int j=0; j<temp.numValues(); j++) { attrVals[j] += temp.valueSparse(j); } } for(int j=0, numInsts=instList.length; j<attrVals.length; j++) { attrVals[j] /= numInsts; } temp = new DenseInstance(1.0, attrVals); return temp; }
private Instance createInstance(Instances data, FeatureDefinition fd, FeatureVector fv) { // relevant features + one target Instance currInst = new DenseInstance(data.numAttributes()); currInst.setDataset(data); // read only relevant features for (String attName : this.featureNames) { int featNr = fd.getFeatureIndex(attName); String value = fv.getFeatureAsString(featNr, fd); currInst.setValue(data.attribute(attName), value); } return currInst; }
/** * Calculates the centroid pivot of a node. The node is given * in the form of an indices array that contains the * indices of the points inside the node. * @param instList The indices array pointing to the * instances in the node. * @param insts The actual instances. The instList * points to instances in this object. * @return The calculated centre/pivot of the node. */ public static Instance calcCentroidPivot(int[] instList, Instances insts) { double[] attrVals = new double[insts.numAttributes()]; Instance temp; for(int i=0; i<instList.length; i++) { temp = insts.instance(instList[i]); for(int j=0; j<temp.numValues(); j++) { attrVals[j] += temp.valueSparse(j); } } for(int j=0, numInsts=instList.length; j<attrVals.length; j++) { attrVals[j] /= numInsts; } temp = new DenseInstance(1.0, attrVals); return temp; }
/** * Adds random instances to the dataset. * * @param dataset the dataset * @param numInstances the number of instances * @param random a random number generator */ protected void addRandomInstances( Instances dataset, int numInstances, Random random ) { int n = dataset.numAttributes(); double [] v = new double[ n ]; for( int i = 0; i < numInstances; i++ ) { for( int j = 0; j < n; j++ ) { Attribute att = dataset.attribute( j ); if( att.isNumeric() ) { v[ j ] = random.nextDouble(); } else if ( att.isNominal() ) { v[ j ] = random.nextInt( att.numValues() ); } } dataset.add( new DenseInstance( 1, v ) ); } }
double[] attrVals = new double[insts.numAttributes()]; Instance temp; for(int i=start; i<=end; i++) { temp = new DenseInstance(1.0, attrVals); return temp;
@Override public Instances getDataSet() throws IOException { if (m_sourceFile == null) { throw new IOException("No source has been specified"); } if (getRetrieval() == INCREMENTAL) { throw new IOException("This loader cannot load instances incrementally."); } setRetrieval(BATCH); if (m_structure == null) { getStructure(); } Instances result = new Instances(m_structure); for (String word : vec.getVocab().words()) { double[] values = new double[result.numAttributes()]; for (int i = 0; i < this.vec.getWordVector(word).length; i++) values[i] = this.vec.getWordVector(word)[i]; values[result.numAttributes() - 1] = result.attribute("word_id").addStringValue(word); Instance inst = new DenseInstance(1, values); inst.setDataset(result); result.add(inst); } return result; }
newInst = new DenseInstance(model.numAttributes()); newInst.setDataset(model); for (int i = 0, j = 0; i < model.numAttributes(); i++) { if (model.attribute(i).isNumeric()) { if (j >= m_Elements.length)
newInst = new DenseInstance(model.numAttributes()); newInst.setDataset(model); for (int i = 0, j = 0; i < model.numAttributes(); i++) { if (model.attribute(i).isNumeric()) { if (j >= m_Elements.length)
/** * Transforms the instance in the prediction process before given to the internal multi-label * or multi-target classifier. The instance is passed having the original set of labels, these * must be replaced with the transformed labels (attributes) so that the internla classifier * can predict them. * * @param x The instance to transform. Consists of features and labels. * @return The transformed instance. Consists of features and transformed labels. */ @Override public Instance transformInstance(Instance x) throws Exception{ Instances tmpInst = new Instances(x.dataset()); tmpInst.delete(); tmpInst.add(x); Instances features = this.extractPart(tmpInst, false); Instances labels = new Instances(this.m_PatternInstances); labels.add(new DenseInstance(labels.numAttributes())); Instances result = Instances.mergeInstances(labels, features); result.setClassIndex(labels.numAttributes()); return result.instance(0); }
/** * Makes a level-1 instance from the given instance. * * @param instance the instance to be transformed * @return the level-1 instance * @throws Exception if the instance generation fails */ protected Instance metaInstance(Instance instance) throws Exception { double[] values = new double[m_MetaFormat.numAttributes()]; Instance metaInstance; int i = 0; for (int k = 0; k < m_Classifiers.length; k++) { Classifier classifier = getClassifier(k); if (m_BaseFormat.classAttribute().isNumeric()) { values[i++] = classifier.classifyInstance(instance); } else { double[] dist = classifier.distributionForInstance(instance); for (int j = 0; j < dist.length; j++) { values[i++] = dist[j]; } } } values[i] = instance.classValue(); metaInstance = new DenseInstance(1, values); metaInstance.setDataset(m_MetaFormat); return metaInstance; }
/** * Makes a level-1 instance from the given instance. * * @param instance the instance to be transformed * @return the level-1 instance * @throws Exception if the instance generation fails */ protected Instance metaInstance(Instance instance) throws Exception { double[] values = new double[m_MetaFormat.numAttributes()]; Instance metaInstance; int i = 0; for (int k = 0; k < m_Classifiers.length; k++) { Classifier classifier = getClassifier(k); if (m_BaseFormat.classAttribute().isNumeric()) { values[i++] = classifier.classifyInstance(instance); } else { double[] dist = classifier.distributionForInstance(instance); for (int j = 0; j < dist.length; j++) { values[i++] = dist[j]; } } } values[i] = instance.classValue(); metaInstance = new DenseInstance(1, values); metaInstance.setDataset(m_MetaFormat); return metaInstance; }
/** * Transforms the instance in the prediction process before given to the internal multi-label * or multi-target classifier. The instance is passed having the original set of labels, these * must be replaced with the transformed labels (attributes) so that the internla classifier * can predict them. * * @param x The instance to transform. Consists of features and labels. * @return The transformed instance. Consists of features and transformed labels. */ @Override public Instance transformInstance(Instance x) throws Exception{ Instances tmpInst = new Instances(x.dataset()); tmpInst.delete(); tmpInst.add(x); Instances features = this.extractPart(tmpInst, false); Instances labels = new Instances(this.m_PatternInstances); labels.add(new DenseInstance(labels.numAttributes())); Instances result = Instances.mergeInstances(labels, features); result.setClassIndex(labels.numAttributes()); return result.instance(0); }
/** * converts a single instance to the required format * * @param instance the instance to convert * @return the converted instance */ protected Instance convertInstance(Instance instance) throws Exception { double vals[] = new double[outputFormatPeek().numAttributes()]; for (int j = 0; j < m_k; j++) { for (int i = 0; i < instance.numValues(); i++) { int index = instance.index(i); if (index != instance.classIndex()) { double value = instance.valueSparse(i); if (!Utils.isMissingValue(value)) { vals[j] += m_rmatrix[j][index] * value; } } else { vals[m_k] = instance.valueSparse(i); } } } return new DenseInstance(instance.weight(), vals); }
/** * Makes a set of level-1 instances from the given instances. More efficient if at least one base classifier * implements efficient batch prediction. Requires all base classifiers to implement BatchPredictor. * * @param instances the instances to be transformed * @return the level-1 instances * @throws Exception if the instance generation fails */ protected Instances metaInstances(Instances instances) throws Exception { double[][][] predictions = new double[m_Classifiers.length][][]; for (int k = 0; k < m_Classifiers.length; k++) { predictions[k] = ((BatchPredictor) getClassifier(k)).distributionsForInstances(instances); } Instances metaData = new Instances(m_MetaFormat, 0); for (int l = 0; l < instances.numInstances(); l++) { double[] values = new double[m_MetaFormat.numAttributes()]; int i = 0; for (int k = 0; k < m_Classifiers.length; k++) { if (m_BaseFormat.classAttribute().isNumeric()) { values[i++] = predictions[k][l][0]; } else { System.arraycopy(predictions[k][l], 0, values, i, predictions[k][l].length); i += predictions[k][l].length; } } values[i] = instances.instance(l).classValue(); metaData.add(new DenseInstance(1, values)); } return metaData; }
vals = new double[testing.numAttributes()]; vals[0] = attAge.indexOf("10-19"); testing.add(new DenseInstance(1.0, vals));
/** * converts a single instance to the required format * * @param instance the instance to convert * @return the converted instance */ protected Instance convertInstance(Instance instance) throws Exception { double vals[] = new double[outputFormatPeek().numAttributes()]; for (int j = 0; j < m_k; j++) { for (int i = 0; i < instance.numValues(); i++) { int index = instance.index(i); if (index != instance.classIndex()) { double value = instance.valueSparse(i); if (!Utils.isMissingValue(value)) { vals[j] += m_rmatrix[j][index] * value; } } else { vals[m_k] = instance.valueSparse(i); } } } return new DenseInstance(instance.weight(), vals); }
/** * Make an output instance given an input one * * @param inputI the input instance to process * @return the output instance with substrings replaced */ public Instance makeOutputInstance(Instance inputI) { double[] vals = new double[m_outputStructure.numAttributes()]; String[] stringVals = new String[m_outputStructure.numAttributes()]; for (int i = 0; i < inputI.numAttributes(); i++) { if (inputI.attribute(i).isString() && !inputI.isMissing(i)) { stringVals[i] = inputI.stringValue(i); } else { vals[i] = inputI.value(i); } } for (SubstringReplacerMatchRule mr : m_matchRules) { mr.apply(stringVals); } for (int i = 0; i < m_outputStructure.numAttributes(); i++) { if (m_outputStructure.attribute(i).isString() && stringVals[i] != null) { m_outputStructure.attribute(i).setStringValue(stringVals[i]); } } Instance result = new DenseInstance(inputI.weight(), vals); result.setDataset(m_outputStructure); return result; }
/** * Make an output instance given an input one * * @param inputI the input instance to process * @return the output instance with substrings replaced */ public Instance makeOutputInstance(Instance inputI) { double[] vals = new double[m_outputStructure.numAttributes()]; String[] stringVals = new String[m_outputStructure.numAttributes()]; for (int i = 0; i < inputI.numAttributes(); i++) { if (inputI.attribute(i).isString() && !inputI.isMissing(i)) { stringVals[i] = inputI.stringValue(i); } else { vals[i] = inputI.value(i); } } for (SubstringReplacerMatchRule mr : m_matchRules) { mr.apply(stringVals); } for (int i = 0; i < m_outputStructure.numAttributes(); i++) { if (m_outputStructure.attribute(i).isString() && stringVals[i] != null) { m_outputStructure.attribute(i).setStringValue(stringVals[i]); } } Instance result = new DenseInstance(inputI.weight(), vals); result.setDataset(m_outputStructure); return result; }