weka.core.Instances.size()方法的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(12.1k)|赞(0)|评价(0)|浏览(86)

本文整理了Java中weka.core.Instances.size()方法的一些代码示例,展示了Instances.size()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Instances.size()方法的具体详情如下:
包路径:weka.core.Instances
类名称:Instances
方法名:size

Instances.size介绍

[英]Returns the number of instances in the dataset.
[中]返回数据集中的实例数。

代码示例

代码示例来源:origin: nz.ac.waikato.cms.weka/weka-stable

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();
  }
 }
}

代码示例来源:origin: Waikato/weka-trunk

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();
  }
 }
}

代码示例来源:origin: nz.ac.waikato.cms.weka/weka-stable

/**
 * 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();
}

代码示例来源:origin: Waikato/weka-trunk

/**
 * 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();
}

代码示例来源:origin: org.dkpro.tc/dkpro-tc-ml-weka

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;
}

代码示例来源:origin: dkpro/dkpro-tc

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;
}

代码示例来源:origin: org.dkpro.tc/dkpro-tc-ml-weka

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;
}

代码示例来源:origin: dkpro/dkpro-tc

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;
}

代码示例来源:origin: org.dkpro.tc/dkpro-tc-ml-weka

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;
}

代码示例来源:origin: dkpro/dkpro-tc

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;
}

代码示例来源:origin: nz.ac.waikato.cms.weka/weka-stable

@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;
}

代码示例来源:origin: Waikato/weka-trunk

@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;
}

代码示例来源:origin: Waikato/wekaDeeplearning4j

/** 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));
}

代码示例来源:origin: nz.ac.waikato.cms.weka/weka-stable

double[] densities = new double[m_canopies.size()];
for (int i = 0; i < m_canopies.numInstances(); i++) {
 double[] density = m_canopyT2Density.get(i);

代码示例来源:origin: Waikato/wekaDeeplearning4j

/** 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);
 }
}

代码示例来源:origin: nz.ac.waikato.cms.weka/weka-stable

/**
 * 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);
 }
}

代码示例来源:origin: Waikato/weka-trunk

/**
 * 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);
 }
}

代码示例来源:origin: Waikato/wekaDeeplearning4j

for (int i = 0; i < mnistMiniArff.size(); i++) {
 Instance inst = mnistMiniArff.get(i);
 int instLabel = Integer.parseInt(inst.stringValue(inst.numAttributes() - 1));

代码示例来源:origin: Waikato/wekaDeeplearning4j

/** 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);
 }
}

代码示例来源:origin: Waikato/wekaDeeplearning4j

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);
   }
  }
 }
}

相关文章

Instances类方法