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

📄 attribute2realvaluemapping.java

📁 一个很好的LIBSVM的JAVA源码。对于要研究和改进SVM算法的学者。可以参考。来自数据挖掘工具YALE工具包。
💻 JAVA
字号:
/*
 *  YALE - Yet Another Learning Environment
 *  Copyright (C) 2001-2004
 *      Simon Fischer, Ralf Klinkenberg, Ingo Mierswa, 
 *          Katharina Morik, Oliver Ritthoff
 *      Artificial Intelligence Unit
 *      Computer Science Department
 *      University of Dortmund
 *      44221 Dortmund,  Germany
 *  email: yale-team@lists.sourceforge.net
 *  web:   http://yale.cs.uni-dortmund.de/
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License as 
 *  published by the Free Software Foundation; either version 2 of the
 *  License, or (at your option) any later version. 
 *
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 *  USA.
 */
package edu.udo.cs.yale.operator.preprocessing;

import edu.udo.cs.yale.operator.Operator;
import edu.udo.cs.yale.operator.IOObject;
import edu.udo.cs.yale.operator.OperatorException;
import edu.udo.cs.yale.operator.parameter.*;
import edu.udo.cs.yale.example.Attribute;
import edu.udo.cs.yale.example.DataRow;
import edu.udo.cs.yale.example.DataRowReader;
import edu.udo.cs.yale.example.DoubleArrayDataRow;
import edu.udo.cs.yale.example.ListDataRowReader;
import edu.udo.cs.yale.example.Example;
import edu.udo.cs.yale.example.ExampleSet;
import edu.udo.cs.yale.example.ExampleReader;
import edu.udo.cs.yale.example.MemoryExampleTable;
import edu.udo.cs.yale.tools.Ontology;

import java.util.*;

/** This operator maps all non numeric attributes to real valued attributes. Nothing is done for 
 *  numeric attributes, binary attributes are mapped to 0 and 1.
 *
 *  For nominal attributes one of the following calculations will be done:
 *  <ul>
 *  <li>Dichotomization, i.e. one new attribute for each value of the nominal attribute. 
 *      The new attribute of the current gets value 1 and all other attributes gets value 0. The
 *      operator constructs a new example table which may increase the used memory dramatically.</li>
 *  <li>Alternatively the values of nominal attributes can be seen as equally ranked, therefore 
 *      the nominal attribute will simply be turned into a real valued attribute, the old values
 *      results in equidistant real values.</li>
 *  </ul>
 *
 *  At this moment the same applies for ordinal attributes, in a future release more appropriate values based
 *  on the ranking between the ordinal values may be included.
 *
 *  @yale.todo ordinal attributes are mapped in accordance to their ranks
 *  @yale.todo non numeric value series ?
 *
 *  @version $Id: Attribute2RealValueMapping.java,v 1.4 2004/09/17 12:57:42 ingomierswa Exp $
 */
public class Attribute2RealValueMapping extends Operator {

    public IOObject[] apply() throws OperatorException {
	ExampleSet eSet = (ExampleSet)getInput(ExampleSet.class);

	ExampleSet result = null;
	if (getParameterAsBoolean("dichotomization")) {
	    List attributes = new LinkedList();
	    // regular attributes
	    for (int i = 0; i < eSet.getNumberOfAttributes(); i++) {
		Attribute attribute = eSet.getAttribute(i);
		if (attribute.isNominal()) {  // nominal
		    handleNominalAttribute(attribute, attributes);
		} else { // numeric
		    Attribute newAttribute = new Attribute(attribute.getName(),
							   Ontology.REAL, Ontology.SINGLE_VALUE, Attribute.UNDEFINED_BLOCK_NR, null);
		    attributes.add(newAttribute);
		}
	    }
	    
	    // add all special attributes
	    Map specialMap = new HashMap();
	    Iterator i = eSet.getSpecialAttributeNames().iterator();
	    while (i.hasNext()) {
		String name = (String)i.next();
		Attribute attribute = (Attribute)eSet.getAttribute(name).clone(); 
		attributes.add(attribute);
		specialMap.put(name, attribute);
	    }

	    // create new example table and fill with data
	    MemoryExampleTable exampleTable = new MemoryExampleTable(attributes);

	    List dataRows = new LinkedList();
	    ExampleReader exampleReader = eSet.getExampleReader();
	    while (exampleReader.hasNext()) {
		Example example = exampleReader.next();
		dataRows.add(createDataRow(example, eSet, specialMap, attributes.size()));
	    }

	    DataRowReader reader = new ListDataRowReader(dataRows.iterator());
	    exampleTable.readExamples(reader);

	    // create a new example set with the correct special attributes
	    result = exampleTable.createCompleteExampleSet(specialMap);

	} else {
	    // simply set values types on real for non-dichotomization
	    for (int i = 0; i < eSet.getNumberOfAttributes(); i++) {
		Attribute attribute = eSet.getAttribute(i);
		if (attribute.isNominal())
		    attribute.clearMaps();
		attribute.setValueType(Ontology.REAL);
	    }
	    result = eSet;
	}

	result.recalculateAllAttributeStatistics();

	return new IOObject[] { result };
    }

    private void handleNominalAttribute(Attribute attribute, List attributes) {
  	if ((Ontology.ATTRIBUTE_VALUE_TYPE.isA(attribute.getValueType(), Ontology.CLASSIFICATION)) ||
  	    (Ontology.ATTRIBUTE_VALUE_TYPE.isA(attribute.getValueType(), Ontology.BOOLEAN))) {
	    Attribute newAttribute = new Attribute(attribute.getName(),
						   Ontology.REAL, Ontology.SINGLE_VALUE, Attribute.UNDEFINED_BLOCK_NR, null);
  	    attributes.add(newAttribute);

//  	    //} else if (Ontology.ATTRIBUTE_VALUE_TYPE.isA(attribute.getValueType(), Ontology.ORDERED)) {
	    
  	} else { // all nominal values which are not one of the above (including ordered attributes for now)...
	    Iterator i = attribute.getValues().iterator();
	    while (i.hasNext()) {
		Attribute newAttribute = new Attribute(attribute.getName() + "_" + (String)i.next(),
						       Ontology.REAL, Ontology.SINGLE_VALUE, 
						       Attribute.UNDEFINED_BLOCK_NR, null);
		attributes.add(newAttribute);
	    }
  	}
    }


    private DataRow createDataRow(Example example, ExampleSet eSet, Map specialAttributes, int size) {
	double[] result = new double[size];
	// regular data
	int currentResultIndex = 0;
	for (int i = 0; i < eSet.getNumberOfAttributes(); i++) {
	    Attribute oldAttribute = eSet.getAttribute(i);
	    if (oldAttribute.isNominal()) {
		if ((Ontology.ATTRIBUTE_VALUE_TYPE.isA(oldAttribute.getValueType(), Ontology.CLASSIFICATION)) ||
		    (Ontology.ATTRIBUTE_VALUE_TYPE.isA(oldAttribute.getValueType(), Ontology.BOOLEAN))) {
		    result[currentResultIndex++] = example.getValue(oldAttribute);
		} else { // other nominal and ordered
		    int index = (int)example.getValue(oldAttribute) - Attribute.FIRST_CLASS_INDEX;
		    result[currentResultIndex+index] = 1.0d;
		    currentResultIndex += oldAttribute.getValues().size();
		}
	    } else {
		result[currentResultIndex++] = example.getValue(oldAttribute);
	    }
	}

	// special data
	Iterator i = specialAttributes.keySet().iterator();
	while (i.hasNext()) {
	    Attribute oldAttribute = eSet.getAttribute((String)i.next());
	    result[currentResultIndex++] = example.getValue(oldAttribute);
	}
	return new DoubleArrayDataRow(result);
    }

    public Class[] getInputClasses() {
	return new Class[] { ExampleSet.class };
    }

    public Class[] getOutputClasses() {
	return new Class[] { ExampleSet.class }; 
    }

    public List getParameterTypes() {
	List types = super.getParameterTypes();
	types.add(new ParameterTypeBoolean("dichotomization", "Uses one new attribute for each possible value of nominal attributes (new example table increasing used memory)", false));
	return types;
    }
}


⌨️ 快捷键说明

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