kernelfilter.java

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

JAVA
877
字号
   *    * <pre> -E &lt;num&gt;   *  The Exponent to use.   *  (default: 1.0)</pre>   *    * <pre> -L   *  Use lower-order terms.   *  (default: no)</pre>   *    * <pre>    * Options specific to preprocessing filter weka.filters.unsupervised.attribute.Center:   * </pre>   *    * <pre> -unset-class-temporarily   *  Unsets the class index temporarily before the filter is   *  applied to the data.   *  (default: no)</pre>   *    <!-- options-end -->   *    * @param options the list of options as an array of strings   * @throws Exception if an option is not supported    */  public void setOptions(String[] options) throws Exception {    String	tmpStr;    String[]	tmpOptions;        setChecksTurnedOff(Utils.getFlag("no-checks", options));    tmpStr = Utils.getOption('F', options);    if (tmpStr.length() != 0)      setInitFile(new File(tmpStr));    else       setInitFile(null);    tmpStr = Utils.getOption('C', options);    if (tmpStr.length() != 0)      setInitFileClassIndex(tmpStr);    else       setInitFileClassIndex("last");    tmpStr     = Utils.getOption('K', options);    tmpOptions = Utils.splitOptions(tmpStr);    if (tmpOptions.length != 0) {      tmpStr        = tmpOptions[0];      tmpOptions[0] = "";      setKernel(Kernel.forName(tmpStr, tmpOptions));    }        tmpStr = Utils.getOption("kernel-factor", options);    if (tmpStr.length() != 0)      setKernelFactorExpression(tmpStr);    else       setKernelFactorExpression("1");        tmpStr = Utils.getOption("P", options);    tmpOptions = Utils.splitOptions(tmpStr);    if (tmpOptions.length != 0) {      tmpStr        = tmpOptions[0];      tmpOptions[0] = "";      setPreprocessing((Filter) Utils.forName(Filter.class, tmpStr, tmpOptions));    }    else {      setPreprocessing(new Center());    }    super.setOptions(options);  }	      /**   * Returns the tip text for this property   *    * @return 		tip text for this property suitable for   * 			displaying in the explorer/experimenter gui   */  public String initFileTipText() {    return "The dataset to initialize the filter with.";  }  /**   * Gets the file to initialize the filter with, can be null.   *   * @return 		the file   */  public File getInitFile() {    return m_InitFile;  }      /**   * Sets the file to initialize the filter with, can be null.   *   * @param value	the file   */  public void setInitFile(File value) {    m_InitFile = value;  }    /**   * Returns the tip text for this property   *    * @return 		tip text for this property suitable for   * 			displaying in the explorer/experimenter gui   */  public String initFileClassIndexTipText() {    return "The class index of the dataset to initialize the filter with (first and last are valid).";  }  /**   * Gets the class index of the file to initialize the filter with.   *   * @return 		the class index   */  public String getInitFileClassIndex() {    return m_InitFileClassIndex.getSingleIndex();  }      /**   * Sets class index of the file to initialize the filter with.   *   * @param value	the class index   */  public void setInitFileClassIndex(String value) {    m_InitFileClassIndex.setSingleIndex(value);  }    /**   * Returns the tip text for this property   *    * @return 		tip text for this property suitable for   * 			displaying in the explorer/experimenter gui   */  public String kernelTipText() {    return "The kernel to use.";  }  /**   * Gets the kernel to use.   *   * @return 		the kernel   */  public Kernel getKernel() {    return m_Kernel;  }      /**   * Sets the kernel to use.   *   * @param value	the kernel   */  public void setKernel(Kernel value) {    m_Kernel = value;  }  /**   * Disables or enables the checks (which could be time-consuming). Use with   * caution!   *    * @param value	if true turns off all checks   */  public void setChecksTurnedOff(boolean value) {    m_checksTurnedOff = value;  }    /**   * Returns whether the checks are turned off or not.   *    * @return		true if the checks are turned off   */  public boolean getChecksTurnedOff() {    return m_checksTurnedOff;  }  /**   * Returns the tip text for this property   *    * @return 		tip text for this property suitable for   * 			displaying in the explorer/experimenter gui   */  public String checksTurnedOffTipText() {    return "Turns time-consuming checks off - use with caution.";  }    /**   * Returns the tip text for this property   *    * @return 		tip text for this property suitable for   * 			displaying in the explorer/experimenter gui   */  public String kernelFactorExpressionTipText() {    return "The factor for the kernel, with A = # of attributes and N = # of instances.";  }  /**   * Gets the expression for the kernel.   *   * @return 		the expression   */  public String getKernelFactorExpression() {    return m_KernelFactorExpression;  }      /**   * Sets the expression for the kernel.   *   * @param value	the file   */  public void setKernelFactorExpression(String value) {    m_KernelFactorExpression = value;  }  /**   * Returns the tip text for this property   *    * @return 		tip text for this property suitable for   * 			displaying in the explorer/experimenter gui   */  public String preprocessingTipText() {    return "Sets the filter to use for preprocessing (use the AllFilter for no preprocessing).";  }  /**   * Sets the filter to use for preprocessing (use the AllFilter for no    * preprocessing)   *   * @param value 	the preprocessing filter   */  public void setPreprocessing(Filter value) {    m_Filter       = value;    m_ActualFilter = null;  }  /**   * Gets the filter used for preprocessing   *   * @return 		the current preprocessing filter.   */  public Filter getPreprocessing() {    return m_Filter;  }  /**   * resets the filter, i.e., m_NewBatch to true and m_FirstBatchDone to   * false.   */  protected void reset() {    super.reset();        m_Initialized = false;  }  /**   * 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 {    return new Instances(inputFormat);  }    /**   * initializes the filter with the given dataset, i.e., the kernel gets   * built. Needs to be called before the first call of Filter.useFilter or   * batchFinished(), if not the -F option (or setInitFile(File) is used).   *    * @param instances	the data to initialize with   * @throws Exception	if building of kernel fails   */  public void initFilter(Instances instances) throws Exception {    HashMap	symbols;        // determine kernel factor    symbols = new HashMap();    symbols.put("A", new Double(instances.numAttributes()));    symbols.put("N", new Double(instances.numInstances()));    m_KernelFactor = MathematicalExpression.evaluate(getKernelFactorExpression(), symbols);        // init filters    if (!m_checksTurnedOff) {      m_Missing = new ReplaceMissingValues();      m_Missing.setInputFormat(instances);      instances = Filter.useFilter(instances, m_Missing);     }     else {      m_Missing = null;    }    if (getKernel().getCapabilities().handles(Capability.NUMERIC_ATTRIBUTES)) {	boolean onlyNumeric = true;	if (!m_checksTurnedOff) {	  for (int i = 0; i < instances.numAttributes(); i++) {	    if (i != instances.classIndex()) {	      if (!instances.attribute(i).isNumeric()) {		onlyNumeric = false;		break;	      }	    }	  }	}		if (!onlyNumeric) {	  m_NominalToBinary = new NominalToBinary();	  m_NominalToBinary.setInputFormat(instances);	  instances = Filter.useFilter(instances, m_NominalToBinary);	} 	else {	  m_NominalToBinary = null;	}    }    else {      m_NominalToBinary = null;    }    if ((m_Filter != null) && (m_Filter.getClass() != AllFilter.class)) {      m_ActualFilter = Filter.makeCopy(m_Filter);      m_ActualFilter.setInputFormat(instances);      instances = Filter.useFilter(instances, m_ActualFilter);    }    else {      m_ActualFilter = null;    }    m_NumTrainInstances = instances.numInstances();    // set factor for kernel    m_ActualKernel = Kernel.makeCopy(m_Kernel);    if (m_ActualKernel instanceof RBFKernel)      ((RBFKernel) m_ActualKernel).setGamma(	  m_KernelFactor * ((RBFKernel) m_ActualKernel).getGamma());    // build kernel    m_ActualKernel.buildKernel(instances);    m_Initialized = true;  }  /**    * Returns the Capabilities of this filter.   *   * @return            the capabilities of this object   * @see               Capabilities   */  public Capabilities getCapabilities() {    Capabilities 	result;        if (getKernel() == null)      result = super.getCapabilities();    else      result = getKernel().getCapabilities();    result.setMinimumNumberInstances(0);        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 {    // initializing necessary?    if (!m_Initialized) {      // do we have a file to initialize with?      if ((getInitFile() != null) && getInitFile().isFile()) {	DataSource source = new DataSource(getInitFile().getAbsolutePath());	Instances data = source.getDataSet();	m_InitFileClassIndex.setUpper(data.numAttributes() - 1);	data.setClassIndex(m_InitFileClassIndex.getIndex());	initFilter(data);      }      else {	initFilter(instances);      }    }    // apply filters    if (m_Missing != null)      instances = Filter.useFilter(instances, m_Missing);     if (m_NominalToBinary != null)      instances = Filter.useFilter(instances, m_NominalToBinary);     if (m_ActualFilter != null)      instances = Filter.useFilter(instances, m_ActualFilter);    // backup class attribute and remove it    double[] classes = instances.attributeToDoubleArray(instances.classIndex());    int classIndex = instances.classIndex();    instances.setClassIndex(-1);    instances.deleteAttributeAt(classIndex);    // generate new header    FastVector atts = new FastVector();    for (int j = 0; j < m_NumTrainInstances; j++)      atts.addElement(new Attribute("Kernel " + j));    atts.addElement(new Attribute("Class"));    Instances result = new Instances("Kernel", atts, 0);    result.setClassIndex(result.numAttributes() - 1);    // compute matrix    for (int i = 0; i < instances.numInstances(); i++) {      double[] k = new double[m_NumTrainInstances + 1];            for (int j = 0; j < m_NumTrainInstances; j++) {	double v = m_ActualKernel.eval(-1, j, instances.instance(i));	k[j] = v;      }      k[k.length - 1] = classes[i];      // create new instance      Instance in = new Instance(1.0, k);      result.add(in);        }    if (!isFirstBatchDone())      setOutputFormat(result);        return result;  }  /**   * runs the filter with the given arguments   *   * @param args      the commandline arguments   */  public static void main(String[] args) {    runFilter(new KernelFilter(), args);  }}

⌨️ 快捷键说明

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