Java Code Examples for weka.core.DenseInstance#setDataset()
The following examples show how to use
weka.core.DenseInstance#setDataset() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MergeInfrequentNominalValues.java From tsml with GNU General Public License v3.0 | 6 votes |
/** * Processes the given data. * * @param instances the data to process * @return the modified data * @throws Exception in case the processing goes wrong */ public Instances process(Instances instances) throws Exception { // Generate the output and return it Instances result = new Instances(getOutputFormat(), instances.numInstances()); for (int i = 0; i < instances.numInstances(); i++) { Instance inst = instances.instance(i); double[] newData = new double[instances.numAttributes()]; for (int j = 0; j < instances.numAttributes(); j++) { if (m_AttToBeModified[j] && !inst.isMissing(j)) { newData[j] = m_NewValues[j][(int)inst.value(j)]; } else { newData[j] = inst.value(j); } } DenseInstance instNew = new DenseInstance(1.0, newData); instNew.setDataset(result); // copy possible strings, relational values... copyValues(instNew, false, inst.dataset(), getOutputFormat()); // Add instance to output result.add(instNew); } return result; }
Example 2
Source File: DataSetUtilsTest.java From AILibs with GNU Affero General Public License v3.0 | 6 votes |
public void cifar10InstancesAttributesTest() { ArrayList<Attribute> atts = new ArrayList<>(); for (int i = 0; i < 32 * 32 * 3 + 1; i++) { atts.add(new Attribute("blub" + i)); } Instances instances = new Instances("test", atts, 1); DenseInstance inst = new DenseInstance(atts.size()); for (int i = 0; i < inst.numAttributes(); i++) { inst.setValue(i, 1d); } inst.setDataset(instances); instances.add(inst); INDArray result = DataSetUtils.cifar10InstanceToMatrix(inst); Assert.assertArrayEquals(new long[]{32, 32, 3}, result.shape()); }
Example 3
Source File: WekaInstancesUtil.java From AILibs with GNU Affero General Public License v3.0 | 6 votes |
public static Instances datasetToWekaInstances(final ILabeledDataset<? extends ILabeledInstance> dataset) throws UnsupportedAttributeTypeException { Instances wekaInstances = createDatasetFromSchema(dataset.getInstanceSchema()); int expectedAttributes = dataset.getInstanceSchema().getNumAttributes(); for (ILabeledInstance inst : dataset) { if (inst.getNumAttributes() != expectedAttributes) { throw new IllegalStateException("Dataset scheme defines a number of " + expectedAttributes + " attributes, but instance has " + inst.getNumAttributes() + ". Attributes in scheme: " + dataset.getInstanceSchema().getAttributeList().stream().map(a -> "\n\t" + a.getName() + " (" + a.toString() + ")").collect(Collectors.joining()) + ". Attributes in instance: " + Arrays.stream(inst.getAttributes()).map(a -> "\n\t" + a.toString()).collect(Collectors.joining())); } double[] point = inst.getPoint(); double[] pointWithLabel = Arrays.copyOf(point, point.length + 1); DenseInstance iNew = new DenseInstance(1, pointWithLabel); iNew.setDataset(wekaInstances); if (dataset.getLabelAttribute() instanceof ICategoricalAttribute) { iNew.setClassValue(((ICategoricalAttribute) dataset.getLabelAttribute()).getLabelOfCategory((int)inst.getLabel())); } else { iNew.setClassValue(Double.parseDouble(inst.getLabel().toString())); } wekaInstances.add(iNew); // this MUST come here AFTER having set the class value; otherwise, the class is not registered correctly in the Instances object!! } return wekaInstances; }
Example 4
Source File: MekaInstancesUtil.java From AILibs with GNU Affero General Public License v3.0 | 6 votes |
public static Instances datasetToWekaInstances(final ILabeledDataset<? extends ILabeledInstance> dataset) throws UnsupportedAttributeTypeException { Instances wekaInstances = createDatasetFromSchema(dataset.getInstanceSchema()); for (ILabeledInstance inst : dataset) { double[] point = inst.getPoint(); double[] pointWithLabel = Arrays.copyOf(point, point.length + 1); DenseInstance iNew = new DenseInstance(1, pointWithLabel); iNew.setDataset(wekaInstances); if (dataset.getLabelAttribute() instanceof ICategoricalAttribute) { iNew.setClassValue(((ICategoricalAttribute) dataset.getLabelAttribute()).getLabelOfCategory((int) inst.getLabel())); } else { iNew.setClassValue((Double) inst.getLabel()); } wekaInstances.add(iNew); // this MUST come here AFTER having set the class value; otherwise, the class is not registered correctly in the Instances object!! } return wekaInstances; }
Example 5
Source File: DecisionTree.java From chuidiang-ejemplos with GNU Lesser General Public License v3.0 | 6 votes |
public static void main(String[] args) throws Exception { Classifier j48 = new J48(); Instances trainingData = GenerateTestVessels.getData(); j48.buildClassifier(trainingData); System.out.println(j48); double[] vesselUnderTest = GenerateTestVessels.getBarco(5); DenseInstance inst = new DenseInstance(1.0,vesselUnderTest); inst.setDataset(trainingData); inst.setClassMissing(); System.out.println(inst); double result = j48.classifyInstance(inst); System.out.println(GenerateTestVessels.types[(int)result]); SerializationHelper.write(new FileOutputStream("tmp"), j48); J48 j48Read = (J48)SerializationHelper.read(new FileInputStream("tmp")); }
Example 6
Source File: MergeNominalValues.java From tsml with GNU General Public License v3.0 | 5 votes |
/** * Processes the given data. * * @param instances the data to process * @return the modified data * @throws Exception in case the processing goes wrong */ @Override public Instances process(Instances instances) throws Exception { // Generate the output and return it Instances result = new Instances(getOutputFormat(), instances.numInstances()); for (int i = 0; i < instances.numInstances(); i++) { Instance inst = instances.instance(i); double[] newData = new double[instances.numAttributes()]; for (int j = 0; j < instances.numAttributes(); j++) { if (m_AttToBeModified[j] && !inst.isMissing(j)) { newData[j] = m_Indicators[j][(int) inst.value(j)]; } else { newData[j] = inst.value(j); } } DenseInstance instNew = new DenseInstance(1.0, newData); instNew.setDataset(result); // copy possible strings, relational values... copyValues(instNew, false, inst.dataset(), getOutputFormat()); // Add instance to output result.add(instNew); } return result; }
Example 7
Source File: CSVLoader.java From tsml with GNU General Public License v3.0 | 4 votes |
protected Instance makeInstance() throws IOException { if (m_current == null) { return null; } double[] vals = new double[m_structure.numAttributes()]; for (int i = 0; i < m_structure.numAttributes(); i++) { Object val = m_current.get(i); if (val.toString().equals("?")) { vals[i] = Utils.missingValue(); } else if (m_structure.attribute(i).isString()) { vals[i] = 0; m_structure.attribute(i).setStringValue(Utils.unquote(val.toString())); } else if (m_structure.attribute(i).isDate()) { String format = m_structure.attribute(i).getDateFormat(); SimpleDateFormat sdf = new SimpleDateFormat(format); try { vals[i] = sdf.parse(val.toString()).getTime(); } catch (ParseException e) { throw new IOException("Unable to parse date value " + val.toString() + " using date format " + format + " for date attribute " + m_structure.attribute(i)); } } else if (m_structure.attribute(i).isNumeric()) { try { Double v = Double.parseDouble(val.toString()); vals[i] = v.doubleValue(); } catch (NumberFormatException ex) { throw new IOException("Was expecting a number for attribute " + m_structure.attribute(i).name() + " but read " + val.toString() + " instead."); } } else { // nominal double index = m_structure.attribute(i).indexOfValue( Utils.unquote(val.toString())); if (index < 0) { throw new IOException("Read unknown nominal value " + val.toString() + "for attribute " + m_structure.attribute(i).name()); } vals[i] = index; } } DenseInstance inst = new DenseInstance(1.0, vals); inst.setDataset(m_structure); return inst; }