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

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

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

Instances.classIndex介绍

[英]Returns the class attribute's index. Returns negative number if it's 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: org.dkpro.tc/dkpro-tc-ml-weka

private static ArrayList<String> getLabels(Instances data)
  {
    int numLabels = data.classIndex();
    ArrayList<String> list = new ArrayList<String>();
    for (int i = 0; i < numLabels; i++) {
      list.add(data.attribute(i).name());
    }
    return list;
  }
}

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

public static final String toDebugString(Instances D) {
  int L = D.classIndex();
  StringBuilder sb = new StringBuilder();  
  sb.append("D="+D.numInstances());
  sb.append(" L="+L+" {");
  for(int j = 0; j < L; j++) {
    sb.append(D.attribute(j).name()+" ");
  }
  sb.append("}");
  return sb.toString();
}

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

protected Instances convert(Instances D, int j, int k) {
  int L = D.classIndex();
  D = new Instances(D);
  D.insertAttributeAt(classAttribute,0);
  D.setClassIndex(0);
  for(int i = 0; i < D.numInstances(); i++) {
    String c = (String)((int)Math.round(D.instance(i).value(j+1))+""+(int)Math.round(D.instance(i).value(k+1)));
    D.instance(i).setClassValue(c);
  }
  for (int i = 0; i < L; i++)
    D.deleteAttributeAt(1);
  m_InstancesTemplate = new Instances(D,0);
  return D;
}

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

for (i = 0; i < data.numAttributes(); i++) {
 if (i == data.classIndex()) {
  atts.add((Attribute) data.attribute(i).copy());
 } else {
  atts.add(data.attribute(i).copy(prefix + data.attribute(i).name()));
result = new Instances(data.relationName(), atts, data.numInstances());
for (i = 0; i < data.numInstances(); i++) {
 result.add((Instance) data.instance(i).copy());
if (data.classIndex() > -1) {
 result.setClassIndex(data.classIndex());

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

/**
 * GetXfromD - Extract attributes as a double X[][] from Instances D.
 * TODO: getXfromInstances would be a better name.
 */
public static double[][] getXfromD(Instances D) {
  int N = D.numInstances();
  int L = D.classIndex();
  int d = D.numAttributes()-L;
  //System.out.println("d="+d);
  double X[][] = new double[N][d];
  for(int i = 0; i < N; i++) {
    for(int k = 0; k < d; k++) {
      X[i][k] = D.instance(i).value(k+L);
    }
  }
  return X;
}

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

@Override
public void validate(Instances data) throws InvalidInputDataException {
 if (!((data.attribute(0).isString() && data.classIndex() == 1)
   || (data.attribute(1).isString() && data.classIndex() == 0))) {
  throw new InvalidInputDataException(
    "An ARFF is required with a string attribute and a class attribute");
 }
 if (data.numAttributes() != 2) {
  throw new InvalidInputDataException(
    "There must be exactly two attributes: 1) Text 2) Label. "
      + "The given data consists of "
      + data.numAttributes()
      + " attributes.");
 }
}

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

/**
 * SetLabelsMissing - Set all labels in D to missing.
 */
public static Instances setLabelsMissing(Instances D) {
  int L = D.classIndex();
  for(int i = 0; i < D.numInstances(); i++) {
    for(int j = 0; j < L ; j++) {
      D.instance(i).setMissing(j);
    }
  }
  return D;
}

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

@Override
public void buildClassifier(Instances D) throws Exception {
   testCapabilities(D);
   
  int L = D.classIndex();
  // Transform Instances
  if(getDebug()) System.out.print("Transforming Instances ...");
  Instances D_ = PSUtils.LCTransformation(D,L);
  m_InstancesTemplate = new Instances(D_,0);
  // Set Info ; Build Classifier
  info = "K = "+m_InstancesTemplate.attribute(0).numValues() + ", N = "+D_.numInstances();
  if(getDebug()) System.out.print("Building Classifier ("+info+"), ...");
  m_Classifier.buildClassifier(D_);
  if(getDebug()) System.out.println("Done");
}

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

@Override
public void buildClassifier(Instances D) throws Exception {
  int L = D.classIndex();
  int N = D.numInstances();
  // Get partition from dataset hierarchy
  kMap = SuperLabelUtils.getPartitionFromDatasetHierarchy(D); 
  m_M = kMap.length;
  m_Classifiers = AbstractClassifier.makeCopies(m_Classifier,m_M);
  m_InstancesTemplates = new Instances[m_M];
  for(int i = 0; i < m_M; i++) {
    if (getDebug()) 
      System.out.println("Building model "+(i+1)+"/"+m_M+": "+Arrays.toString(kMap[i]));
    Instances D_i = SuperLabelUtils.makePartitionDataset(D,kMap[i]);
    m_Classifiers[i].buildClassifier(D_i);
    m_InstancesTemplates[i] = new Instances(D_i,0);
  }
}

代码示例来源: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

/**
 * Sets up the UI's attributes lists
 */
public void setupAttribLists() {
 String[] tempAttribNames = new String[m_data.numAttributes()];
 String type;
 m_classAttrib.removeAllItems();
 for (int i = 0; i < tempAttribNames.length; i++) {
  type = " (" + Attribute.typeToStringShort(m_data.attribute(i)) + ")";
  tempAttribNames[i] =
   new String("Colour: " + m_data.attribute(i).name() + " " + type);
  m_classAttrib.addItem(tempAttribNames[i]);
 }
 if (m_data.classIndex() == -1) {
  m_classAttrib.setSelectedIndex(tempAttribNames.length - 1);
 } else {
  m_classAttrib.setSelectedIndex(m_data.classIndex());
 }
 m_attribList.setListData(tempAttribNames);
 m_attribList.setSelectionInterval(0, tempAttribNames.length - 1);
}

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

/**
 * generates a consequence of length 1 for a class association rule.
 * 
 * @param instances the instances under consideration
 * @return FastVector with consequences of length 1
 */
public static ArrayList<Object> singleConsequence(Instances instances) {
 ItemSet consequence;
 ArrayList<Object> consequences = new ArrayList<Object>();
 for (int j = 0; j < (instances.classAttribute()).numValues(); j++) {
  consequence = new ItemSet(instances.numInstances());
  int[] consequenceItems = new int[instances.numAttributes()];
  consequence.setItem(consequenceItems);
  for (int k = 0; k < instances.numAttributes(); k++) {
   consequence.setItemAt(-1, k);
  }
  consequence.setItemAt(j, instances.classIndex());
  consequences.add(consequence);
 }
 return consequences;
}

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

/**
 * GetKs - return [K_1,K_2,...,K_L] where each Y_j \in {1,...,K_j}.
 * @param    D    a dataset
 */
private static int[] getKs(Instances D) {
  int L = D.classIndex();
  int K[] = new int[L];
  for(int k = 0; k < L; k++) {
    K[k] = D.attribute(k).numValues();
  }
  return K;
}

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

/**
  * 
  * @param bayesNet
  * @param instances the instances to work with
  * @throws Exception if something goes wrong
  */
public void buildStructure (BayesNet bayesNet, Instances instances) throws Exception {
 for (int iAttribute = 0; iAttribute < instances.numAttributes(); iAttribute++) {
  if (iAttribute != instances.classIndex()) {
   bayesNet.getParentSet(iAttribute).addParent(instances.classIndex(), instances);
  }
 }
} // buildStructure

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

@Override
public Instances determineOutputFormat(Instances D) throws Exception {
  //System.out.println("DETERMINE OUTPUT FORMAT = "+D.numInstances());
  Instances D_out = new Instances(D,0);
  int L = D.classIndex();
  for(int i = 0; i < L-indices.length; i++) {
    D_out.deleteAttributeAt(0);
  }
  return D_out;
}

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

protected Instances convert(Instances D, int j, int k) {
  int L = D.classIndex();
  D = new Instances(D);
  D.insertAttributeAt(classAttribute,0);
  D.setClassIndex(0);
  for(int i = 0; i < D.numInstances(); i++) {
    String c = (String)((int)Math.round(D.instance(i).value(j+1))+""+(int)Math.round(D.instance(i).value(k+1)));
    D.instance(i).setClassValue(c);
  }
  for (int i = 0; i < L; i++)
    D.deleteAttributeAt(1);
  m_InstancesTemplate = new Instances(D,0);
  return D;
}

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

for (i = 0; i < data.numAttributes(); i++) {
 if (i == data.classIndex()) {
  atts.add((Attribute) data.attribute(i).copy());
 } else {
  atts.add(data.attribute(i).copy(prefix + data.attribute(i).name()));
result = new Instances(data.relationName(), atts, data.numInstances());
for (i = 0; i < data.numInstances(); i++) {
 result.add((Instance) data.instance(i).copy());
if (data.classIndex() > -1) {
 result.setClassIndex(data.classIndex());

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

/**
 * GetXfromD - Extract attributes as a double X[][] from Instances D.
 * TODO: getXfromInstances would be a better name.
 */
public static double[][] getXfromD(Instances D) {
  int N = D.numInstances();
  int L = D.classIndex();
  int d = D.numAttributes()-L;
  //System.out.println("d="+d);
  double X[][] = new double[N][d];
  for(int i = 0; i < N; i++) {
    for(int k = 0; k < d; k++) {
      X[i][k] = D.instance(i).value(k+L);
    }
  }
  return X;
}

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

/**
 * Returns true if all attribute weights are the same and false otherwise. Returns true if there are no attributes.
 * The class attribute (if set) is skipped when this test is performed.
 */
public boolean allAttributeWeightsIdentical() {
 boolean foundOne = false;
 double weight = 0;
 for (int i = 0; i < numAttributes(); i++) {
  if (i != classIndex()) {
   if (foundOne && (attribute(i).weight() != weight)) {
    return false;
   } else if (!foundOne) {
    foundOne = true;
    weight = attribute(i).weight();
   }
  }
 }
 return true;
}

相关文章

Instances类方法