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

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

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

Instances.setClassIndex介绍

[英]Sets the class index of the set. If the class index is negative there is assumed to be no class. (ie. it is undefined)
[中]设置集合的类索引。如果类索引为负,则假定没有类。(即未定义)

代码示例

代码示例来源:origin: stackoverflow.com

DataSource source = new DataSource(new File("mycsvinputfile"));
   System.out.println(source.getStructure());
   Instances data = source.getDataSet();
   // setting class attribute if the data format does not provide this information
   // For example, the XRFF format saves the class attribute information as well
   if (data.classIndex() == -1)
     data.setClassIndex(data.numAttributes() - 1);
   //initialize svm classifier
   LibSVM svm = new LibSVM();
   svm.buildClassifier(data);

代码示例来源:origin: stackoverflow.com

Instances dataUnlabeled = new Instances("TestInstances", atts, 0);
dataUnlabeled.add(newInst);
dataUnlabeled.setClassIndex(dataUnlabeled.numAttributes() - 1);        
double classif = ibk.classifyInstance(dataUnlabeled.firstInstance());

代码示例来源:origin: sc.fiji/T2-NIT

Operator() {
    ArrayList<Attribute> a = new ArrayList<Attribute>();
    for (int i=0; i<attrs.length-1; i++) {
      a.add(new Attribute(attrs[i])); // numeric
    }
    ArrayList<String> d = new ArrayList<String>();
    d.add("false");
    d.add("true");
    a.add(new Attribute(attrs[attrs.length-1], d)); // nominal attribute
    data = new Instances("Buh", a, 0);
    data.setClassIndex(attrs.length-1); // the CLASS
  }
}

代码示例来源:origin: stackoverflow.com

Instances training_data = new Instances(new BufferedReader(
    new FileReader("tmp-file-duplicate-pairs.arff")));
training_data.setClassIndex(training_data.numAttributes() - 1);

代码示例来源:origin: net.sf.meka/meka

/**
 * Transform - transform dataset D for this node.
 * this.j defines the current node index, e.g., 3
 * this.paY[] defines parents,            e.g., [1,4]
 * we should remove the rest,             e.g., [0,2,5,...,L-1]
 * @return dataset we should remove all variables from D EXCEPT current node, and parents.
 */
public Instances transform(Instances D) throws Exception {
  int L = D.classIndex();
  d = D.numAttributes() - L;
  int keep[] = A.append(this.paY,j);		// keep all parents and self!
  Arrays.sort(keep);
  int remv[] = A.invert(keep,L); 	// i.e., remove the rest < L
  Arrays.sort(remv);
  map = new int[L];
  for(int j = 0; j < L; j++) {
    map[j] = Arrays.binarySearch(keep,j);
  }
  Instances D_ = F.remove(new Instances(D),remv, false); 
  D_.setClassIndex(map[this.j]);
  return D_;
}

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

@Override
public void buildClassifier(Instances D) throws Exception {
   testCapabilities(D);
   
  int L = D.classIndex();
  if(getDebug()) System.out.print("Creating "+L+" models ("+m_Classifier.getClass().getName()+"): ");
  m_MultiClassifiers = AbstractClassifier.makeCopies(m_Classifier,L);
  m_Templates = new Instances[L];
  for(int j = 0; j < L; j++) {
    //Select only class attribute 'j'
    m_Templates[j] = MLUtils.keepAttributesAt(new Instances(D),new int[]{j},L);
    m_Templates[j].setClassIndex(0);
    //Build the classifier for that class
    m_MultiClassifiers[j].buildClassifier(m_Templates[j]);
    if(getDebug()) System.out.print(" " + (m_Templates[j].classAttribute().name()));
    m_Templates[j] = new Instances(m_Templates[j], 0);
  }
}

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

/** Need to remove non-nominal attributes, set class index */
protected void setUp() throws Exception {
 super.setUp();
 // class index
 m_Instances.setClassIndex(1);
 
 // remove attributes that are not nominal/numeric
 int i = 0;
 while (i < m_Instances.numAttributes()) {
  if (    !m_Instances.attribute(i).isNominal()
     && !m_Instances.attribute(i).isNumeric() )
   m_Instances.deleteAttributeAt(i);
  else
   i++;
 }
}

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

protected Instances getSummaryInsts(Instances orig, String[] args)
 throws Exception {
 List<String> attNames = new ArrayList<String>();
 for (int i = 0; i < orig.numAttributes(); i++) {
  attNames.add(orig.attribute(i).name());
 }
 CSVToARFFHeaderMapTask arffTask = new CSVToARFFHeaderMapTask();
 arffTask.setOptions(args);
 for (int i = 0; i < orig.numInstances(); i++) {
  arffTask.processRow(orig.instance(i).toString(), attNames);
 }
 Instances withSummary = arffTask.getHeader();
 CSVToARFFHeaderReduceTask arffReduce = new CSVToARFFHeaderReduceTask();
 List<Instances> instList = new ArrayList<Instances>();
 instList.add(withSummary);
 withSummary = arffReduce.aggregate(instList);
 withSummary.setClassIndex(orig.classIndex());
 return withSummary;
}

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

protected Instances getData2() throws Exception {
 Instances data2 = new Instances(new StringReader(DATA2));
 data2.setClassIndex(1);
 return data2;
}

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

ArrayList<String> newVals) {
Instances miningSchemaI = getFieldsAsInstances();
if (miningSchemaI.attribute(index).isNominal()) {
 throw new IllegalArgumentException("[MiningSchema] convertNumericAttToNominal: attribute is "
                   + "already nominal!");
 newValues.add(newVals.get(i));
for (int i = 0; i < miningSchemaI.numAttributes(); i++) {
 Attribute tempA = miningSchemaI.attribute(i);
 if (i == index) {
  Attribute newAtt = new Attribute(tempA.name(), newValues);
  attInfo.add(newAtt);
 } else {
  attInfo.add(tempA);
Instances newI = new Instances("miningSchema", attInfo, 0);
if (m_fieldInstancesStructure.classIndex() >= 0) {
 newI.setClassIndex(m_fieldInstancesStructure.classIndex());

代码示例来源:origin: stackoverflow.com

JFrame1 form = new JFrame1();
form.setVisible(true);
form.addPropertyChangeListener(new PropertyChangeListener() {

  @Override
  public void propertyChange(PropertyChangeEvent pce) {
    // Handle the change here

    String pth = (String) pce.getNewValue();
    BufferedReader datafile = readDataFile(pth);

    Instances data = new Instances(datafile);
    data.setClassIndex(data.numAttributes() - 1);

    (...)
  }

});

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

/**
 * Transform - transform dataset D for this node.
 * this.j defines the current node index, e.g., 3
 * this.paY[] defines parents,            e.g., [1,4]
 * we should remove the rest,             e.g., [0,2,5,...,L-1]
 * @return dataset we should remove all variables from D EXCEPT current node, and parents.
 */
public Instances transform(Instances D) throws Exception {
  int L = D.classIndex();
  d = D.numAttributes() - L;
  int keep[] = A.append(this.paY,j);		// keep all parents and self!
  Arrays.sort(keep);
  int remv[] = A.invert(keep,L); 	// i.e., remove the rest < L
  Arrays.sort(remv);
  map = new int[L];
  for(int j = 0; j < L; j++) {
    map[j] = Arrays.binarySearch(keep,j);
  }
  Instances D_ = F.remove(new Instances(D),remv, false); 
  D_.setClassIndex(map[this.j]);
  return D_;
}

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

@Override
public void buildClassifier(Instances D) throws Exception {
  testCapabilities(D);
   
  int L = D.classIndex();
  if(getDebug()) System.out.print("Creating "+L+" models ("+m_Classifier.getClass().getName()+"): ");
  m_MultiClassifiers = AbstractClassifier.makeCopies(m_Classifier,L);
  m_InstancesTemplates = new Instances[L];
  for(int j = 0; j < L; j++) {
    //Select only class attribute 'j'
    Instances D_j = F.keepLabels(new Instances(D),L,new int[]{j});
    D_j.setClassIndex(0);
    //Build the classifier for that class
    m_MultiClassifiers[j].buildClassifier(D_j);
    if(getDebug()) System.out.print(" " + (D_j.classAttribute().name()));
    m_InstancesTemplates[j] = new Instances(D_j, 0);
  }
}

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

/** Need to remove non-nominal attributes, set class index */
protected void setUp() throws Exception {
 super.setUp();
 // class index
 m_Instances.setClassIndex(1);
 
 // remove attributes that are not nominal/numeric
 int i = 0;
 while (i < m_Instances.numAttributes()) {
  if (    !m_Instances.attribute(i).isNominal()
     && !m_Instances.attribute(i).isNumeric() )
   m_Instances.deleteAttributeAt(i);
  else
   i++;
 }
}

代码示例来源:origin: stackoverflow.com

BufferedReader reader =
 new BufferedReader(new FileReader("/some/where/file.arff"));
ArffReader arff = new ArffReader(reader, 1000);
Instances data = arff.getStructure();
data.setClassIndex(data.numAttributes() - 1);
Instance inst;
while ((inst = arff.readInstance(data)) != null) {
 data.add(inst);
}

代码示例来源:origin: stackoverflow.com

// rootPath should be where the .arff file is held
// filename should hold the complete name of the .arff file
public static Instances instanceData(String rootPath, String filename) throws Exception
{ 
 // initialize source 
 DataSource source = null;
 Instances data = null;
 source = new DataSource(rootPath + filename);
 data = source.getDataSet();
 <br>
 // set the class to the last attribute of the data (may need to tweak) 
 if (data.classIndex() == -1)
  data.setClassIndex(data.numAttributes() -1 );
 return data;
}
 </br>

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

protected Instances getData2() throws Exception {
 Instances data2 = new Instances(new StringReader(DATA2));
 data2.setClassIndex(1);
 return data2;
}

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

ArrayList<String> newVals) {
Instances miningSchemaI = getFieldsAsInstances();
if (miningSchemaI.attribute(index).isNominal()) {
 throw new IllegalArgumentException("[MiningSchema] convertNumericAttToNominal: attribute is "
                   + "already nominal!");
 newValues.add(newVals.get(i));
for (int i = 0; i < miningSchemaI.numAttributes(); i++) {
 Attribute tempA = miningSchemaI.attribute(i);
 if (i == index) {
  Attribute newAtt = new Attribute(tempA.name(), newValues);
  attInfo.add(newAtt);
 } else {
  attInfo.add(tempA);
Instances newI = new Instances("miningSchema", attInfo, 0);
if (m_fieldInstancesStructure.classIndex() >= 0) {
 newI.setClassIndex(m_fieldInstancesStructure.classIndex());

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

/**
 * Load an arbitrary arff file
 *
 * @return Instances
 * @throws Exception IO error.
 */
public static Instances loadArff(String path) throws Exception {
 Instances data = new Instances(new FileReader(path));
 data.setClassIndex(data.numAttributes() - 1);
 return data;
}
/**

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

/**
 * Tests the copying of the header of a dataset.
 * 
 * @see Instances#Instances(Instances, int)
 */
public void testHeaderCopy() {
 Instances     data;
 
 data = new Instances(m_Instances, 0);
 assertEquals("# of attributes differ", m_Instances.numAttributes(), data.numAttributes());
 assertEquals("class index differs", m_Instances.classIndex(), data.classIndex());
 assertEquals("Unexpected instances", 0, data.numInstances());
 m_Instances.setClassIndex(m_Instances.numAttributes() - 1);
 data = new Instances(m_Instances, 0);
 assertEquals("class index differs", m_Instances.classIndex(), data.classIndex());
}

相关文章

Instances类方法