private void assignCanopiesToCanopyCenters() { // assign canopies to each canopy center m_clusterCanopies = new ArrayList<long[]>(); for (int i = 0; i < m_canopies.size(); i++) { Instance inst = m_canopies.instance(i); try { long[] assignments = assignCanopies(inst); m_clusterCanopies.add(assignments); } catch (Exception ex) { ex.printStackTrace(); } } }
private void assignCanopiesToCanopyCenters() { // assign canopies to each canopy center m_clusterCanopies = new ArrayList<long[]>(); for (int i = 0; i < m_canopies.size(); i++) { Instance inst = m_canopies.instance(i); try { long[] assignments = assignCanopies(inst); m_clusterCanopies.add(assignments); } catch (Exception ex) { ex.printStackTrace(); } } }
/** * Print the supplied instances and their canopies * * @param dataPoints the instances to print * @param canopyAssignments the canopy assignments, one assignment array for * each instance * @return a string containing the printed assignments */ public static String printCanopyAssignments(Instances dataPoints, List<long[]> canopyAssignments) { StringBuilder temp = new StringBuilder(); for (int i = 0; i < dataPoints.size(); i++) { temp.append("Cluster " + i + ": "); temp.append(dataPoints.instance(i)); if (canopyAssignments != null && canopyAssignments.size() == dataPoints.size()) { long[] assignments = canopyAssignments.get(i); temp.append(printSingleAssignment(assignments)); } temp.append("\n"); } return temp.toString(); }
/** * Print the supplied instances and their canopies * * @param dataPoints the instances to print * @param canopyAssignments the canopy assignments, one assignment array for * each instance * @return a string containing the printed assignments */ public static String printCanopyAssignments(Instances dataPoints, List<long[]> canopyAssignments) { StringBuilder temp = new StringBuilder(); for (int i = 0; i < dataPoints.size(); i++) { temp.append("Cluster " + i + ": "); temp.append(dataPoints.instance(i)); if (canopyAssignments != null && canopyAssignments.size() == dataPoints.size()) { long[] assignments = canopyAssignments.get(i); temp.append(printSingleAssignment(assignments)); } temp.append("\n"); } return temp.toString(); }
public List<String> performPrediction(Classifier cl, Instances data) throws Exception { StringBuffer classVals = new StringBuffer(); for (int i = 0; i < data.classAttribute().numValues(); i++) { if (classVals.length() > 0) { classVals.append(","); } classVals.append(data.classAttribute().value(i)); } // get predictions List<String> predictions = new ArrayList<String>(); for (int i = 0; i < data.size(); i++) { Double pred = cl.classifyInstance(data.instance(i)); predictions.add(pred.toString()); } return predictions; }
public List<String> performPrediction(Classifier cl, Instances data) throws Exception { StringBuffer classVals = new StringBuffer(); for (int i = 0; i < data.classAttribute().numValues(); i++) { if (classVals.length() > 0) { classVals.append(","); } classVals.append(data.classAttribute().value(i)); } // get predictions List<String> predictions = new ArrayList<String>(); for (int i = 0; i < data.size(); i++) { Double pred = cl.classifyInstance(data.instance(i)); predictions.add(pred.toString()); } return predictions; }
public List<String> performPrediction(Classifier cl, Instances data) throws Exception { List<String> results = new ArrayList<>(); for (int j = 0; j < data.size(); j++) { double[] vals = null; try { vals = cl.distributionForInstance(data.instance(j)); } catch (Exception e) { throw new AnalysisEngineProcessException(e); } List<String> outcomes = new ArrayList<String>(); for (int i = 0; i < vals.length; i++) { if (vals[i] >= threshold) { String label = data.instance(j).attribute(i).name(); outcomes.add(label); } } results.add(StringUtils.join(outcomes, ",")); } return results; }
public List<String> performPrediction(Classifier cl, Instances data) throws Exception { List<String> results = new ArrayList<>(); for (int j = 0; j < data.size(); j++) { double[] vals = null; try { vals = cl.distributionForInstance(data.instance(j)); } catch (Exception e) { throw new AnalysisEngineProcessException(e); } List<String> outcomes = new ArrayList<String>(); for (int i = 0; i < vals.length; i++) { if (vals[i] >= threshold) { String label = data.instance(j).attribute(i).name(); outcomes.add(label); } } results.add(StringUtils.join(outcomes, ",")); } return results; }
public Instances getPredictionInstancesSingleLabel(Instances testData, Classifier cl) throws Exception { StringBuffer classVals = new StringBuffer(); for (int i = 0; i < testData.classAttribute().numValues(); i++) { if (classVals.length() > 0) { classVals.append(","); } classVals.append(testData.classAttribute().value(i)); } // get predictions List<Double> labelPredictionList = new ArrayList<Double>(); for (int i = 0; i < testData.size(); i++) { labelPredictionList.add(cl.classifyInstance(testData.instance(i))); } // add an attribute with the predicted values at the end off the attributes Add filter = new Add(); filter.setAttributeName(WekaTestTask.PREDICTION_CLASS_LABEL_NAME); if (classVals.length() > 0) { filter.setAttributeType(new SelectedTag(Attribute.NOMINAL, Add.TAGS_TYPE)); filter.setNominalLabels(classVals.toString()); } filter.setInputFormat(testData); testData = Filter.useFilter(testData, filter); // fill predicted values for each instance for (int i = 0; i < labelPredictionList.size(); i++) { testData.instance(i).setValue(testData.classIndex() + 1, labelPredictionList.get(i)); } return testData; }
public Instances getPredictionInstancesSingleLabel(Instances testData, Classifier cl) throws Exception { StringBuffer classVals = new StringBuffer(); for (int i = 0; i < testData.classAttribute().numValues(); i++) { if (classVals.length() > 0) { classVals.append(","); } classVals.append(testData.classAttribute().value(i)); } // get predictions List<Double> labelPredictionList = new ArrayList<Double>(); for (int i = 0; i < testData.size(); i++) { labelPredictionList.add(cl.classifyInstance(testData.instance(i))); } // add an attribute with the predicted values at the end off the attributes Add filter = new Add(); filter.setAttributeName(WekaTestTask.PREDICTION_CLASS_LABEL_NAME); if (classVals.length() > 0) { filter.setAttributeType(new SelectedTag(Attribute.NOMINAL, Add.TAGS_TYPE)); filter.setNominalLabels(classVals.toString()); } filter.setInputFormat(testData); testData = Filter.useFilter(testData, filter); // fill predicted values for each instance for (int i = 0; i < labelPredictionList.size(); i++) { testData.instance(i).setValue(testData.classIndex() + 1, labelPredictionList.get(i)); } return testData; }
@Override public double[] distributionForInstance(Instance instance) throws Exception { if (m_canopies == null || m_canopies.size() == 0) { throw new Exception("No canopies available to cluster with!"); } double[] d = new double[numberOfClusters()]; if (m_missingValuesReplacer != null) { m_missingValuesReplacer.input(instance); instance = m_missingValuesReplacer.output(); } for (int i = 0; i < m_canopies.numInstances(); i++) { double distance = m_distanceFunction.distance(instance, m_canopies.instance(i)); d[i] = 1.0 / (1.0 + distance); } Utils.normalize(d); return d; }
@Override public double[] distributionForInstance(Instance instance) throws Exception { if (m_canopies == null || m_canopies.size() == 0) { throw new Exception("No canopies available to cluster with!"); } double[] d = new double[numberOfClusters()]; if (m_missingValuesReplacer != null) { m_missingValuesReplacer.input(instance); instance = m_missingValuesReplacer.output(); } for (int i = 0; i < m_canopies.numInstances(); i++) { double distance = m_distanceFunction.distance(instance, m_canopies.instance(i)); d[i] = 1.0 / (1.0 + distance); } Utils.normalize(d); return d; }
/** Test getDataSetIterator */ @Test public void testGetIteratorNominalClass() throws Exception { final Instances data = DatasetLoader.loadAngerMetaClassification(); final int batchSize = 1; final DataSetIterator it = this.cteii.getDataSetIterator(data, SEED, batchSize); Set<Integer> labels = new HashSet<>(); for (int i = 0; i < data.size(); i++) { Instance inst = data.get(i); int label = Integer.parseInt(inst.stringValue(data.classIndex())); final DataSet next = it.next(); int itLabel = next.getLabels().argMax().getInt(0); Assert.assertEquals(label, itLabel); labels.add(label); } final Set<Integer> collect = it.getLabels().stream().map(s -> Double.valueOf(s).intValue()).collect(Collectors.toSet()); Assert.assertEquals(2, labels.size()); Assert.assertTrue(labels.containsAll(collect)); Assert.assertTrue(collect.containsAll(labels)); }
double[] densities = new double[m_canopies.size()]; for (int i = 0; i < m_canopies.numInstances(); i++) { double[] density = m_canopyT2Density.get(i);
/** Test getDataSetIterator */ @Test public void testGetIteratorNumericClass() throws Exception { final Instances data = DatasetLoader.loadAngerMeta(); final int batchSize = 1; final DataSetIterator it = this.cteii.getDataSetIterator(data, SEED, batchSize); Set<Double> labels = new HashSet<>(); for (int i = 0; i < data.size(); i++) { Instance inst = data.get(i); double label = inst.value(data.classIndex()); final DataSet next = it.next(); double itLabel = next.getLabels().getDouble(0); Assert.assertEquals(label, itLabel, 1e-5); labels.add(label); } }
/** * tests the data whether the filter can actually handle it. * * @param instanceInfo the data to test * @throws Exception if the test fails */ @Override protected void testInputFormat(Instances instanceInfo) throws Exception { for (int i = 0; i < getRanges().length; i++) { Instances newi = new Instances(instanceInfo, 0); if (instanceInfo.size() > 0) { newi.add((Instance) instanceInfo.get(0).copy()); } Range range = getRanges()[i]; range.setUpper(instanceInfo.numAttributes() - 1); Instances subset = generateSubset(newi, range); getFilters()[i].setInputFormat(subset); } }
/** * tests the data whether the filter can actually handle it. * * @param instanceInfo the data to test * @throws Exception if the test fails */ @Override protected void testInputFormat(Instances instanceInfo) throws Exception { for (int i = 0; i < getRanges().length; i++) { Instances newi = new Instances(instanceInfo, 0); if (instanceInfo.size() > 0) { newi.add((Instance) instanceInfo.get(0).copy()); } Range range = getRanges()[i]; range.setUpper(instanceInfo.numAttributes() - 1); Instances subset = generateSubset(newi, range); getFilters()[i].setInputFormat(subset); } }
for (int i = 0; i < mnistMiniArff.size(); i++) { Instance inst = mnistMiniArff.get(i); int instLabel = Integer.parseInt(inst.stringValue(inst.numAttributes() - 1));
/** Test getDataSetIterator */ @Test public void testGetIteratorNumericClass() throws Exception { final Instances data = makeData(); final int batchSize = 1; final DataSetIterator it = this.cteii.getDataSetIterator(data, SEED, batchSize); Set<Double> labels = new HashSet<>(); for (int i = 0; i < data.size(); i++) { Instance inst = data.get(i); double label = inst.value(data.classIndex()); final DataSet next = it.next(); double itLabel = next.getLabels().getDouble(0); Assert.assertEquals(label, itLabel, 1e-5); labels.add(label); } }
protected void checkLayer(Dl4jMlpClassifier clf, Instances iris, String transformationLayerName, String clfPath) throws Exception { Instances activationsExpected = clf.getActivationsAtLayer(transformationLayerName, iris); Dl4jMlpFilter filter = new Dl4jMlpFilter(); filter.setModelFile(new File(clfPath)); filter.setTransformationLayerName(transformationLayerName); filter.setInputFormat(iris); Instances activationsActual = Filter.useFilter(iris, filter); for (int i = 0; i < activationsActual.size(); i++) { Instance expected = activationsExpected.get(i); Instance actual = activationsActual.get(i); for (int j = 0; j < expected.numAttributes(); j++) { assertEquals(expected.value(j), actual.value(j), 1e-6); } } } }