⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nominaltobinary.java

📁 代码是一个分类器的实现,其中使用了部分weka的源代码。可以将项目导入eclipse运行
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
  public boolean getBinaryAttributesNominal() {    return !m_Numeric;  }  /**   * Sets if binary attributes are to be treates as nominal ones.   *   * @param bool true if binary attributes are to be treated as nominal ones   */  public void setBinaryAttributesNominal(boolean bool) {    m_Numeric = !bool;  }  /**   * Returns the tip text for this property   *   * @return tip text for this property suitable for   * displaying in the explorer/experimenter gui   */  public String transformAllValuesTipText() {    return "Whether all nominal values are turned into new attributes, not only if there are more than 2.";  }  /**   * Gets if all nominal values are turned into new attributes, not only if   * there are more than 2.   *   * @return true all nominal values are transformed into new attributes   */  public boolean getTransformAllValues() {    return m_TransformAll;  }  /**   * Sets whether all nominal values are transformed into new attributes, not   * just if there are more than 2.   *   * @param bool true if all nominal value are transformed into new attributes   */  public void setTransformAllValues(boolean bool) {    m_TransformAll = bool;  }  /** Computes average class values for each attribute and value */  private void computeAverageClassValues() {    double totalCounts, sum;    Instance instance;    double [] counts;    double [][] avgClassValues = new double[getInputFormat().numAttributes()][0];    m_Indices = new int[getInputFormat().numAttributes()][0];    for (int j = 0; j < getInputFormat().numAttributes(); j++) {      Attribute att = getInputFormat().attribute(j);      if (att.isNominal()) {	avgClassValues[j] = new double [att.numValues()];	counts = new double [att.numValues()];	for (int i = 0; i < getInputFormat().numInstances(); i++) {	  instance = getInputFormat().instance(i);	  if (!instance.classIsMissing() && 	      (!instance.isMissing(j))) {	    counts[(int)instance.value(j)] += instance.weight();	    avgClassValues[j][(int)instance.value(j)] += 	      instance.weight() * instance.classValue();	  }	}	sum = Utils.sum(avgClassValues[j]);	totalCounts = Utils.sum(counts);	if (Utils.gr(totalCounts, 0)) {	  for (int k = 0; k < att.numValues(); k++) {	    if (Utils.gr(counts[k], 0)) {	      avgClassValues[j][k] /= (double)counts[k];	    } else {	      avgClassValues[j][k] = sum / (double)totalCounts;	    }	  }	}	m_Indices[j] = Utils.sort(avgClassValues[j]);      }    }  }  /** Set the output format. */  private void setOutputFormat() {    if (getInputFormat().classAttribute().isNominal()) {      setOutputFormatNominal();    } else {      setOutputFormatNumeric();    }  }  /**   * Convert a single instance over. The converted instance is    * added to the end of the output queue.   *   * @param instance the instance to convert   */  private void convertInstance(Instance inst) {    if (getInputFormat().classAttribute().isNominal()) {      convertInstanceNominal(inst);    } else {      convertInstanceNumeric(inst);    }  }  /**   * Set the output format if the class is nominal.   */  private void setOutputFormatNominal() {    FastVector newAtts;    int newClassIndex;    StringBuffer attributeName;    Instances outputFormat;    FastVector vals;    // Compute new attributes    newClassIndex = getInputFormat().classIndex();    newAtts = new FastVector();    for (int j = 0; j < getInputFormat().numAttributes(); j++) {      Attribute att = getInputFormat().attribute(j);      if ((!att.isNominal()) || 	  (j == getInputFormat().classIndex())) {	newAtts.addElement(att.copy());      } else {	if ( (att.numValues() <= 2) && (!m_TransformAll) ) {	  if (m_Numeric) {	    newAtts.addElement(new Attribute(att.name()));	  } else {	    newAtts.addElement(att.copy());	  }	} else {	  if (j < getInputFormat().classIndex()) {	    newClassIndex += att.numValues() - 1;	  }	  // Compute values for new attributes	  for (int k = 0; k < att.numValues(); k++) {	    attributeName = 	      new StringBuffer(att.name() + "=");	    attributeName.append(att.value(k));	    if (m_Numeric) {	      newAtts.		addElement(new Attribute(attributeName.toString()));	    } else {	      vals = new FastVector(2);	      vals.addElement("f"); vals.addElement("t");	      newAtts.		addElement(new Attribute(attributeName.toString(), vals));	    }	  }	}      }    }    outputFormat = new Instances(getInputFormat().relationName(),				 newAtts, 0);    outputFormat.setClassIndex(newClassIndex);    setOutputFormat(outputFormat);  }  /**   * Set the output format if the class is numeric.   */  private void setOutputFormatNumeric() {    if (m_Indices == null) {      setOutputFormat(null);      return;    }    FastVector newAtts;    int newClassIndex;    StringBuffer attributeName;    Instances outputFormat;    FastVector vals;    // Compute new attributes    newClassIndex = getInputFormat().classIndex();    newAtts = new FastVector();    for (int j = 0; j < getInputFormat().numAttributes(); j++) {      Attribute att = getInputFormat().attribute(j);      if ((!att.isNominal()) || 	  (j == getInputFormat().classIndex())) {	newAtts.addElement(att.copy());      } else {	if (j < getInputFormat().classIndex())	  newClassIndex += att.numValues() - 2;	  	// Compute values for new attributes	  	for (int k = 1; k < att.numValues(); k++) {	  attributeName = 	    new StringBuffer(att.name() + "=");	  for (int l = k; l < att.numValues(); l++) {	    if (l > k) {	      attributeName.append(',');	    }	    attributeName.append(att.value(m_Indices[j][l]));	  }	  if (m_Numeric) {	    newAtts.	      addElement(new Attribute(attributeName.toString()));	  } else {	    vals = new FastVector(2);	    vals.addElement("f"); vals.addElement("t");	    newAtts.	      addElement(new Attribute(attributeName.toString(), vals));	  }	}      }    }    outputFormat = new Instances(getInputFormat().relationName(),				 newAtts, 0);    outputFormat.setClassIndex(newClassIndex);    setOutputFormat(outputFormat);  }  /**   * Convert a single instance over if the class is nominal. The converted    * instance is added to the end of the output queue.   *   * @param instance the instance to convert   */  private void convertInstanceNominal(Instance instance) {    double [] vals = new double [outputFormatPeek().numAttributes()];    int attSoFar = 0;    for(int j = 0; j < getInputFormat().numAttributes(); j++) {      Attribute att = getInputFormat().attribute(j);      if ((!att.isNominal()) || (j == getInputFormat().classIndex())) {	vals[attSoFar] = instance.value(j);	attSoFar++;      } else {	if ( (att.numValues() <= 2) && (!m_TransformAll) ) {	  vals[attSoFar] = instance.value(j);	  attSoFar++;	} else {	  if (instance.isMissing(j)) {	    for (int k = 0; k < att.numValues(); k++) {              vals[attSoFar + k] = instance.value(j);	    }	  } else {	    for (int k = 0; k < att.numValues(); k++) {	      if (k == (int)instance.value(j)) {                vals[attSoFar + k] = 1;	      } else {                vals[attSoFar + k] = 0;	      }	    }	  }	  attSoFar += att.numValues();	}      }    }    Instance inst = null;    if (instance instanceof SparseInstance) {      inst = new SparseInstance(instance.weight(), vals);    } else {      inst = new Instance(instance.weight(), vals);    }    inst.setDataset(getOutputFormat());    copyValues(inst, false, instance.dataset(), getOutputFormat());    inst.setDataset(getOutputFormat());    push(inst);  }  /**   * Convert a single instance over if the class is numeric. The converted    * instance is added to the end of the output queue.   *   * @param instance the instance to convert   */  private void convertInstanceNumeric(Instance instance) {    double [] vals = new double [outputFormatPeek().numAttributes()];    int attSoFar = 0;    for(int j = 0; j < getInputFormat().numAttributes(); j++) {      Attribute att = getInputFormat().attribute(j);      if ((!att.isNominal()) || (j == getInputFormat().classIndex())) {	vals[attSoFar] = instance.value(j);	attSoFar++;      } else {	if (instance.isMissing(j)) {	  for (int k = 0; k < att.numValues() - 1; k++) {            vals[attSoFar + k] = instance.value(j);	  }	} else {	  int k = 0;	  while ((int)instance.value(j) != m_Indices[j][k]) {            vals[attSoFar + k] = 1;	    k++;	  }	  while (k < att.numValues() - 1) {            vals[attSoFar + k] = 0;	    k++;	  }	}	attSoFar += att.numValues() - 1;      }    }    Instance inst = null;    if (instance instanceof SparseInstance) {      inst = new SparseInstance(instance.weight(), vals);    } else {      inst = new Instance(instance.weight(), vals);    }    inst.setDataset(getOutputFormat());    copyValues(inst, false, instance.dataset(), getOutputFormat());    inst.setDataset(getOutputFormat());    push(inst);  }  /**   * Main method for testing this class.   *   * @param argv should contain arguments to the filter:    * use -h for help   */  public static void main(String [] argv) {    runFilter(new NominalToBinary(), argv);  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -