DataConverter { public Instance convert(CharSequence string) { int nball = dataset.nbAttributes() + dataset.getIgnored().length; String[] tokens = COMMA_SPACE.split(string); Preconditions.checkArgument(tokens.length == nball, "Wrong number of attributes in the string: " + tokens.length + ". Must be " + nball); int nbattrs = dataset.nbAttributes(); DenseVector vector = new DenseVector(nbattrs); int aId = 0; for (int attr = 0; attr < nball; attr++) { if (!ArrayUtils.contains(dataset.getIgnored(), attr)) { String token = tokens[attr].trim(); if ("?".equals(token)) { return null; } if (dataset.isNumerical(aId)) { vector.set(aId++, Double.parseDouble(token)); } else { vector.set(aId, dataset.valueOf(aId, token)); aId++; } } } return new Instance(vector); } DataConverter(Dataset dataset); Instance convert(CharSequence string);  }
@Test public void testConvert() throws Exception { Random rng = RandomUtils.getRandom(); String descriptor = Utils.randomDescriptor(rng, ATTRIBUTE_COUNT); double[][] source = Utils.randomDoubles(rng, descriptor, false, INSTANCE_COUNT); String[] sData = Utils.double2String(source); Dataset dataset = DataLoader.generateDataset(descriptor, false, sData); Data data = DataLoader.loadData(dataset, sData); DataConverter converter = new DataConverter(dataset); for (int index = 0; index < data.size(); index++) { assertEquals(data.get(index), converter.convert(sData[index])); } source = Utils.randomDoubles(rng, descriptor, true, INSTANCE_COUNT); sData = Utils.double2String(source); dataset = DataLoader.generateDataset(descriptor, true, sData); data = DataLoader.loadData(dataset, sData); converter = new DataConverter(dataset); for (int index = 0; index < data.size(); index++) { assertEquals(data.get(index), converter.convert(sData[index])); } }