addclassification.java

来自「Weka」· Java 代码 · 共 711 行 · 第 1/2 页

JAVA
711
字号
   * @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 removeOldClassTipText() {    return "Whether to remove the old class attribute.";  }  /**   * Get whether the old class attribute is removed.   *   * @return 		true if the old class attribute is removed.   */  public boolean getRemoveOldClass() {    return m_RemoveOldClass;  }    /**   * Set whether the old class attribute is removed.   *   * @param value 	whether the old class attribute is removed.   */  public void setRemoveOldClass(boolean value) {    m_RemoveOldClass = 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;    int		classindex;        classindex = -1;        // copy old attributes    atts = new FastVector();    for (i = 0; i < inputFormat.numAttributes(); i++) {      // remove class?      if ((i == inputFormat.classIndex()) && (getRemoveOldClass()) )	continue;      // record class index      if (i == inputFormat.classIndex())	classindex = i;      atts.addElement(inputFormat.attribute(i).copy());    }        // add new attributes    // 1. classification?    if (getOutputClassification()) {      // if old class got removed, use this one      if (classindex == -1)	classindex = atts.size();      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(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()];            start = oldValues.length;      if (getRemoveOldClass())	start--;      // copy old values      System.arraycopy(oldValues, 0, newValues, 0, start);            // add new values:      // 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 + =
减小字号Ctrl + -
显示快捷键?