📄 attributeselection.java
字号:
/** * Gets the current settings for the attribute selection (search, evaluator) * etc. * * @return an array of strings suitable for passing to setOptions() */ public String [] getOptions() { String [] EvaluatorOptions = new String[0]; String [] SearchOptions = new String[0]; int current = 0; if (m_ASEvaluator instanceof OptionHandler) { EvaluatorOptions = ((OptionHandler)m_ASEvaluator).getOptions(); } if (m_ASSearch instanceof OptionHandler) { SearchOptions = ((OptionHandler)m_ASSearch).getOptions(); } String [] setOptions = new String [10]; setOptions[current++]="-E"; setOptions[current++]= getEvaluator().getClass().getName() +" "+Utils.joinOptions(EvaluatorOptions); setOptions[current++]="-S"; setOptions[current++]=getSearch().getClass().getName() + " "+Utils.joinOptions(SearchOptions); while (current < setOptions.length) { setOptions[current++] = ""; } return setOptions; } /** * Returns the tip text for this property * * @return tip text for this property suitable for * displaying in the explorer/experimenter gui */ public String evaluatorTipText() { return "Determines how attributes/attribute subsets are evaluated."; } /** * set attribute/subset evaluator * * @param evaluator the evaluator to use */ public void setEvaluator(ASEvaluation evaluator) { m_ASEvaluator = evaluator; } /** * Returns the tip text for this property * * @return tip text for this property suitable for * displaying in the explorer/experimenter gui */ public String searchTipText() { return "Determines the search method."; } /** * Set search class * * @param search the search class to use */ public void setSearch(ASSearch search) { m_ASSearch = search; } /** * Get the name of the attribute/subset evaluator * * @return the name of the attribute/subset evaluator as a string */ public ASEvaluation getEvaluator() { return m_ASEvaluator; } /** * Get the name of the search method * * @return the name of the search method as a string */ public ASSearch getSearch() { return m_ASSearch; } /** * Returns the Capabilities of this filter. * * @return the capabilities of this object * @see Capabilities */ public Capabilities getCapabilities() { Capabilities result; if (m_ASEvaluator == null) { result = super.getCapabilities(); } else { result = m_ASEvaluator.getCapabilities(); // class index will be set if necessary, so we always allow the dataset // to have no class attribute set. see the following method: // weka.attributeSelection.AttributeSelection.SelectAttributes(Instances) result.enable(Capability.NO_CLASS); } result.setMinimumNumberInstances(0); return result; } /** * Input an instance for filtering. Ordinarily the instance is processed * and made available for output immediately. Some filters require all * instances be read before producing output. * * @param instance the input instance * @return true if the filtered instance may now be * collected with output(). * @throws IllegalStateException if no input format has been defined. * @throws Exception if the input instance was not of the correct format * or if there was a problem with the filtering. */ public boolean input(Instance instance) throws Exception { if (getInputFormat() == null) { throw new IllegalStateException("No input instance format defined"); } if (m_NewBatch) { resetQueue(); m_NewBatch = false; } if (isOutputFormatDefined()) { convertInstance(instance); return true; } bufferInput(instance); return false; } /** * Signify that this batch of input to the filter is finished. If the filter * requires all instances prior to filtering, output() may now be called * to retrieve the filtered instances. * * @return true if there are instances pending output. * @throws IllegalStateException if no input structure has been defined. * @throws Exception if there is a problem during the attribute selection. */ public boolean batchFinished() throws Exception { if (getInputFormat() == null) { throw new IllegalStateException("No input instance format defined"); } if (!isOutputFormatDefined()) { m_trainSelector.setEvaluator(m_ASEvaluator); m_trainSelector.setSearch(m_ASSearch); m_trainSelector.SelectAttributes(getInputFormat()); // System.out.println(m_trainSelector.toResultsString()); m_SelectedAttributes = m_trainSelector.selectedAttributes(); if (m_SelectedAttributes == null) { throw new Exception("No selected attributes\n"); } setOutputFormat(); // Convert pending input instances for (int i = 0; i < getInputFormat().numInstances(); i++) { convertInstance(getInputFormat().instance(i)); } flushInput(); } m_NewBatch = true; return (numPendingOutput() != 0); } /** * Set the output format. Takes the currently defined attribute set * m_InputFormat and calls setOutputFormat(Instances) appropriately. * * @throws Exception if something goes wrong */ protected void setOutputFormat() throws Exception { Instances informat; if (m_SelectedAttributes == null) { setOutputFormat(null); return; } FastVector attributes = new FastVector(m_SelectedAttributes.length); int i; if (m_ASEvaluator instanceof AttributeTransformer) { informat = ((AttributeTransformer)m_ASEvaluator).transformedData(); } else { informat = getInputFormat(); } for (i=0;i < m_SelectedAttributes.length;i++) { attributes. addElement(informat.attribute(m_SelectedAttributes[i]).copy()); } Instances outputFormat = new Instances(getInputFormat().relationName(), attributes, 0); if (!(m_ASEvaluator instanceof UnsupervisedSubsetEvaluator) && !(m_ASEvaluator instanceof UnsupervisedAttributeEvaluator)) { outputFormat.setClassIndex(m_SelectedAttributes.length - 1); } setOutputFormat(outputFormat); } /** * Convert a single instance over. Selected attributes only are transfered. * The converted instance is added to the end of * the output queue. * * @param instance the instance to convert * @throws Exception if something goes wrong */ protected void convertInstance(Instance instance) throws Exception { double[] newVals = new double[getOutputFormat().numAttributes()]; if (m_ASEvaluator instanceof AttributeTransformer) { Instance tempInstance = ((AttributeTransformer)m_ASEvaluator). convertInstance(instance); for (int i = 0; i < m_SelectedAttributes.length; i++) { int current = m_SelectedAttributes[i]; newVals[i] = tempInstance.value(current); } } else { for (int i = 0; i < m_SelectedAttributes.length; i++) { int current = m_SelectedAttributes[i]; newVals[i] = instance.value(current); } } if (instance instanceof SparseInstance) { push(new SparseInstance(instance.weight(), newVals)); } else { push(new Instance(instance.weight(), newVals)); } } /** * set options to their default values */ protected void resetOptions() { m_trainSelector = new weka.attributeSelection.AttributeSelection(); setEvaluator(new CfsSubsetEval()); setSearch(new BestFirst()); m_SelectedAttributes = null; m_FilterOptions = null; } /** * 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 AttributeSelection(), argv); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -