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

📄 addclassification.java

📁 代码是一个分类器的实现,其中使用了部分weka的源代码。可以将项目导入eclipse运行
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
   * Sets the classifier to classify instances with.   *   * @param value 	The classifier to be used (with its options set).   */  public void setClassifier(Classifier value) {    m_Classifier = value;  }    /**   * Gets the classifier used by the filter.   *   * @return 		The classifier to be used.   */  public Classifier getClassifier() {    return m_Classifier;  }  /**   * Gets the classifier specification string, which contains the class name of   * the classifier and any options to the classifier.   *   * @return 		the classifier string.   */  protected String getClassifierSpec() {    String	result;    Classifier 	c;        c      = getClassifier();    result = c.getClass().getName();    if (c instanceof OptionHandler)      result += " " + Utils.joinOptions(((OptionHandler) c).getOptions());        return result;  }    /**   * Returns the tip text for this property   *    * @return 		tip text for this property suitable for   * 			displaying in the explorer/experimenter gui   */  public String serializedClassifierFileTipText() {    return "A file containing the serialized model of a trained classifier.";  }  /**   * Gets the file pointing to a serialized, trained classifier. If it is   * null or pointing to a directory it will not be used.   *    * @return		the file the serialized, trained classifier is located    * 			in   */  public File getSerializedClassifierFile() {    return m_SerializedClassifierFile;  }  /**   * Sets the file pointing to a serialized, trained classifier. If the   * argument is null, doesn't exist or pointing to a directory, then the    * value is ignored.   *    * @param value	the file pointing to the serialized, trained classifier   */  public void setSerializedClassifierFile(File value) {    if ((value == null) || (!value.exists()))      value = new File(System.getProperty("user.dir"));    m_SerializedClassifierFile = value;  }    /**   * Returns the tip text for this property   *    * @return 		tip text for this property suitable for   * 			displaying in the explorer/experimenter gui   */  public String outputClassificationTipText() {    return "Whether to add an attribute with the actual classification.";  }  /**   * Get whether the classifiction of the classifier is output.   *   * @return 		true if the classification of the classifier is output.   */  public boolean getOutputClassification() {    return m_OutputClassification;  }    /**   * Set whether the classification of the classifier is output.   *   * @param value 	whether the classification of the classifier is output.   */  public void setOutputClassification(boolean value) {    m_OutputClassification = value;  }    /**   * Returns the tip text for this property   *    * @return 		tip text for this property suitable for   * 			displaying in the explorer/experimenter gui   */  public String outputDistributionTipText() {    return         "Whether to add attributes with the distribution for all classes "      + "(for numeric classes this will be identical to the attribute output "      + "with 'outputClassification').";  }  /**   * Get whether the classifiction of the classifier is output.   *   * @return 		true if the distribution of the classifier is output.   */  public boolean getOutputDistribution() {    return m_OutputDistribution;  }    /**   * Set whether the Distribution of the classifier is output.   *   * @param value 	whether the distribution of the classifier is output.   */  public void setOutputDistribution(boolean value) {    m_OutputDistribution = value;  }    /**   * Returns the tip text for this property   *    * @return 		tip text for this property suitable for   * 			displaying in the explorer/experimenter gui   */  public String outputErrorFlagTipText() {    return         "Whether to add an attribute indicating whether the classifier output "      + "a wrong classification (for numeric classes this is the numeric "      + "difference).";  }  /**   * Get whether the classifiction of the classifier is output.   *   * @return 		true if the classification of the classifier is output.   */  public boolean getOutputErrorFlag() {    return m_OutputErrorFlag;  }    /**   * Set whether the classification of the classifier is output.   *   * @param value 	whether the classification of the classifier is output.   */  public void setOutputErrorFlag(boolean value) {    m_OutputErrorFlag = value;  }  /**   * Determines the output format based on the input format and returns    * this. In case the output format cannot be returned immediately, i.e.,   * immediateOutputFormat() returns false, then this method will be called   * from batchFinished().   *   * @param inputFormat     the input format to base the output format on   * @return                the output format   * @throws Exception      in case the determination goes wrong   * @see   #hasImmediateOutputFormat()   * @see   #batchFinished()   */  protected Instances determineOutputFormat(Instances inputFormat)      throws Exception {        Instances	result;    FastVector	atts;    int		i;    FastVector	values;        // copy old attributes    atts = new FastVector();    for (i = 0; i < inputFormat.numAttributes(); i++)      atts.addElement(inputFormat.attribute(i).copy());        // add new attributes    // 1. classification?    if (getOutputClassification()) {      atts.addElement(inputFormat.classAttribute().copy("classification"));    }        // 2. distribution?    if (getOutputDistribution()) {      if (inputFormat.classAttribute().isNominal()) {	for (i = 0; i < inputFormat.classAttribute().numValues(); i++) {	  atts.addElement(new Attribute("distribution_" + inputFormat.classAttribute().value(i)));	}      }      else {	atts.addElement(new Attribute("distribution"));      }    }        // 2. error flag?    if (getOutputErrorFlag()) {      if (inputFormat.classAttribute().isNominal()) {	values = new FastVector();	values.addElement("no");	values.addElement("yes");	atts.addElement(new Attribute("error", values));      }      else {	atts.addElement(new Attribute("error"));      }    }        // generate new header    result = new Instances(inputFormat.relationName(), atts, 0);    result.setClassIndex(inputFormat.classIndex());        return result;  }  /**   * Processes the given data (may change the provided dataset) and returns   * the modified version. This method is called in batchFinished().   *   * @param instances   the data to process   * @return            the modified data   * @throws Exception  in case the processing goes wrong   * @see               #batchFinished()   */  protected Instances process(Instances instances) throws Exception {    Instances		result;    double[]		newValues;    double[]		oldValues;    int			i;    int			start;    int			n;    Instance		newInstance;    Instance		oldInstance;    double[]		distribution;    File		file;    ObjectInputStream 	ois;        // load or train classifier    if (!isFirstBatchDone()) {      file = getSerializedClassifierFile();      if (!file.isDirectory()) {	ois = new ObjectInputStream(new FileInputStream(file));	m_ActualClassifier = (Classifier) ois.readObject();	ois.close();      }      else {	m_ActualClassifier = Classifier.makeCopy(m_Classifier);	m_ActualClassifier.buildClassifier(instances);      }    }        result = getOutputFormat();        // traverse all instances    for (i = 0; i < instances.numInstances(); i++) {      oldInstance = instances.instance(i);      oldValues   = oldInstance.toDoubleArray();      newValues   = new double[result.numAttributes()];            // copy old values      System.arraycopy(oldValues, 0, newValues, 0, oldValues.length);            // add new values:      start = oldValues.length;            // 1. classification?      if (getOutputClassification()) {	newValues[start] = m_ActualClassifier.classifyInstance(oldInstance);	start++;      }            // 2. distribution?      if (getOutputDistribution()) {	distribution = m_ActualClassifier.distributionForInstance(oldInstance);	for (n = 0; n < distribution.length; n++) {	  newValues[start] = distribution[n];	  start++;	}      }            // 3. error flag?      if (getOutputErrorFlag()) {	if (result.classAttribute().isNominal()) {	  if (oldInstance.classValue() == m_ActualClassifier.classifyInstance(oldInstance))	    newValues[start] = 0;	  else	    newValues[start] = 1;	}	else {	  newValues[start] = m_ActualClassifier.classifyInstance(oldInstance) - oldInstance.classValue();	}	start++;      }            // create new instance      if (oldInstance instanceof SparseInstance)	newInstance = new SparseInstance(oldInstance.weight(), newValues);      else	newInstance = new Instance(oldInstance.weight(), newValues);      result.add(newInstance);    }        return result;  }  /**   * runs the filter with the given arguments   *   * @param args      the commandline arguments   */  public static void main(String[] args) {    runFilter(new AddClassification(), args);  }}

⌨️ 快捷键说明

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