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

📄 attribute.java

📁 一个很好的LIBSVM的JAVA源码。对于要研究和改进SVM算法的学者。可以参考。来自数据挖掘工具YALE工具包。
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		if (!generatingFunctionArguments[i].equals(a.generatingFunctionArguments[i])) return false;
	    return true;
	} else {
	    return false;
	}
    }

    /** Returns true if value and block types of this attribute are subtypes of value and block type of a. */
    public boolean compatible(Attribute a) {
	return  (Ontology.ATTRIBUTE_VALUE_TYPE.isA(this.valueType, a.valueType)) &&
	    (Ontology.ATTRIBUTE_BLOCK_TYPE.isA(this.blockType, a.blockType));
    }

    /** Returns true if the units are equal. */
    public boolean compatibleUnit(Attribute a) {
	for (int i = 0; i < unit.length; i++)
	    if (unit[i]!= a.unit[i]) return false;
	return true;
    }

    /** Returns true iff this attribute was generated. */
    public boolean isGenerated() {
	return generatingFunctionArguments != null;
    }

    /** Returns true if block type is a value series. */
    public boolean isSeries() {
	return Ontology.ATTRIBUTE_BLOCK_TYPE.isA(blockType, Ontology.VALUE_SERIES);
    }

    /** Returns true if block type is a value series. */
    public boolean isInterval() {
	return Ontology.ATTRIBUTE_VALUE_TYPE.isA(blockType, Ontology.INTERVAL);
    }
    
    /** Returns true if this attribute is nominal. */
    public boolean isNominal() {
	return Ontology.ATTRIBUTE_VALUE_TYPE.isA(getValueType(), Ontology.NOMINAL);
    }

    /** Returns true if this attribute is nominal. */
    public boolean isNumerical() {
	return Ontology.ATTRIBUTE_VALUE_TYPE.isA(getValueType(), Ontology.NUMERICAL);
    }

    /** Returns true if this attribute is nominal attribute with two different class values. */
    public boolean isBooleanClassification() {
	return isNominal() && (getNumberOfValues() == 2);
    }

    /** Returns true if this attribute is the start attribute of its block. */
    public boolean isBlockStart() {
	return (Ontology.ATTRIBUTE_BLOCK_TYPE.isA(blockType, Ontology.INTERVAL_START) ||
		Ontology.ATTRIBUTE_BLOCK_TYPE.isA(blockType, Ontology.VALUE_SERIES_START));
    }

    /** Clears all mappings for nominal values. */
    public void clearMaps() {
	ensureNominal();
	symbolToIndexMap.clear();
	indexToSymbolMap.clear();
    }

    /** Returns the index for the nominal attribute value <code>str</code>. If
     *  the string is unknown, a new index value is assigned. Returns -1,
     *  if str is null. */
    public int mapString(String str) {
	ensureNominal();
	if (str == null) return -1;
	// lookup string in hashtable
	Integer index = (Integer)symbolToIndexMap.get(str);
	// if string is not yet in the map, add it
	if (index == null) {
	    // new string -> insert
	    index = new Integer(nextFreeSymbolMapIndex++);
	    symbolToIndexMap.put(str, index);
	    indexToSymbolMap.put(index, str);
	}
	return index.intValue();	
    }

    /** Returns the attribute value, that is associated with this index. WARNING: index counting starts with 
     *  {@link #FIRST_CLASS_INDEX} and it is not assured that all indices are used. To iterate over all values please
     *  use the collection returned by {@link #getValues()}. */
    public String mapIndex(int index) {
	ensureNominal();
	Object value = indexToSymbolMap.get(new Integer(index));
	if (value != null) {
	    return ((String)value);
	} else return null;
    }

    /** Replaces the old value with the new one. Ensures that the new value gets the same value index. */
    public void replaceValue(String oldValue, String newValue) {
	ensureNominal();
	Integer index = (Integer)symbolToIndexMap.remove(oldValue);
	symbolToCounterMap.remove(oldValue);
	symbolToIndexMap.put(newValue, index);
	indexToSymbolMap.put(index, newValue);
    }

    /** Returns the number of occurences of the given nominal value. */
    public int getValueCount(String value) {
	ensureNominal();
	Integer counter = (Integer)symbolToCounterMap.get(value);
	if (counter != null) return counter.intValue();
	else return 0;
    }

    /** Sets the symbol to counter map, e.g. after recalculating the attribute statistics. */
    public void setSymbolToCounterMap(Map symbolToCounterMap) {
	this.symbolToCounterMap = symbolToCounterMap;
    }

    /** Returns a string representation of value if type is numerical or maps
     *  the value to a string if type is nominal. */
    public String getAsString(double value) {
	if (Double.isNaN(value))
	    return "?";
	if (Ontology.ATTRIBUTE_VALUE_TYPE.isA(getValueType(), Ontology.NUMERICAL)) {
	    return value + "";
	} else {
	    return mapIndex((int)value);
	}
    }

    /** Returns the index of the first value if this attribute is a classification attribute. */
    public int getNegativeIndex() {
	ensureClassification();
	if (mapIndex(FIRST_CLASS_INDEX) == null)
	    throw new RuntimeException("Attribute: Cannot use FIRST_CLASS_INDEX for negative class!");
	return FIRST_CLASS_INDEX;
    }

    /** Returns the index of the second value if this attribute is a classification attribute. Works for all nominal attributes. The method
     *  getNegativeIndex works only for boolean classification tasks. */
    public int getPositiveIndex() {
	ensureNominal();
	if (mapIndex(FIRST_CLASS_INDEX) == null)
	    throw new RuntimeException("Attribute: Cannot use FIRST_CLASS_INDEX for negative class!");
	Iterator i = symbolToIndexMap.values().iterator();
	while (i.hasNext()) {
	    int index = ((Integer)i.next()).intValue();
	    if (index != FIRST_CLASS_INDEX)
		return index;
	}
	throw new RuntimeException("Attribute: No other class than FIRST_CLASS_INDEX found!");
    }

    /** Returns the values of the attribute as an enumeration of strings. Attribute must have nominal value type. */
    public Collection getValues() {
 	ensureNominal();
 	//return values;
	return indexToSymbolMap.values();
    }

    public int getNumberOfValues() {
	//return values.size();
	return indexToSymbolMap.size();
    }

    /** Creates a new unsused attribute name with a given prefix. */
    public static String createName(String prefix) {
	return prefix + genSym++;
    }

    /** Creates a new unsused attribute name. */
    private static String createName() {
	return createName(GENSYM_PREFIX);
    }

    /** Returns the index in the example table. */
    public int getIndex() {
	return index;
    }

    /** Sets the index in the example table. */
    public void setIndex(int i) {
	this.index = i;
    }

    /** Returns the name of the function that generated this attribute. */
    public String getFunctionName() {
	return generatingFunctionName;
    }

    /** Returns the arguments that were used to generate this attribute. */
    public Attribute[] getArguments() {
	return generatingFunctionArguments;
    }

    /** Returns a string representation of the range (numerical attribute) or class values (nominal attribute). */
    private String getRangeString() {
	String str = "";
	if (isNominal()) {
	    Iterator i = getValues().iterator();
	    int n = 0;
	    while (i.hasNext()) {
		if (n>0) str +=",";
		n++;
		String value = (String)i.next();
		str += value + " (" + getValueCount(value) + ")";
	    }
	} else {
	    str += "range=" + minimum+"-"+maximum+"; avg="+average + " +/- " + Math.sqrt(variance);
	}
	return str;
    }

    /** Returns a human readable string that describes this attribute. */
    public String toString() {
	String str =
	    "#" + index + ": " + name + ":=" + getConstructionDescription() + 
	    " ["+unitToString()+"]: " + 
	    Ontology.ATTRIBUTE_VALUE_TYPE.mapIndex(valueType) + "/" + 
	    Ontology.ATTRIBUTE_BLOCK_TYPE.mapIndex(blockType) + "/" + 
	    (blockNr == -1 ? "no block; " : "block=" + blockNr + "; ");
	str += "[";
	str += getRangeString();
	str += "] ";
	return str;
    }

    /** Returns a html representation (table row) of this attribute. */
    public String toHTML() {
	String str = "<tr><td>"+index+"</td>"+
	    "<td>" + name + "</td>" +
	    "<td>" + getConstructionDescription() + "</td>" +
	    "<td>"+unitToString()+"</td>" + 
	    "<td>"+Ontology.ATTRIBUTE_VALUE_TYPE.mapIndex(valueType) + "</td>" + 
	    "<td>"+Ontology.ATTRIBUTE_BLOCK_TYPE.mapIndex(blockType) + "</td>" + 
	    "<td>"+((Ontology.ATTRIBUTE_BLOCK_TYPE.isA(blockType, Ontology.VALUE_SERIES) ||
		     Ontology.ATTRIBUTE_BLOCK_TYPE.isA(blockType, Ontology.INTERVAL)) ? "" + blockNr : "-") + "</td>" +
	    "<td>";
	str += getRangeString();
	str += "</td></tr>";
	return str;
    }

    public String unitToString() {
	String s = "";
	for (int i = 0; i < unit.length; i++)
	    if (unit[i] != 0) {
		s += UNIT_NAME[i];
		if (unit[i] != 1) s += unit[i];
	    }
	return s;
    }

    public static int[] multiplyUnits(int[] unit1, int[] unit2) {
	int unit[] = new int[unit1.length];
	for (int i = 0; i < unit1.length; i++)
	    unit[i] = unit1[i]+unit2[i];
	return unit;
    }

    public static int[] divideUnits(int[] unit1, int[] unit2) {
	int unit[] = new int[unit1.length];
	for (int i = 0; i < unit1.length; i++)
	    unit[i] = unit1[i]-unit2[i];
	return unit;
    }

    /** Reads the units from a string as generated by unitToString. */
    public void parseUnits(String unitString) {
	if (unitString == null) {
	    for (int i = 0;i < unit.length; i++)
		unit[i] = 0;
	    return;
	}
	for (int i = 0; i < UNIT_NAME.length; i++) {
	    int pos = unitString.indexOf(UNIT_NAME[i]);
	    if (pos != -1) {
		pos += UNIT_NAME[i].length();
		int endPos = pos;
		while ((endPos < unitString.length()) &&
		       (Character.isDigit(unitString.charAt(endPos)) ||
		       (unitString.charAt(endPos) == '-'))) {
		    endPos++;
		}
		if (endPos == pos) unit[i] = 1;
		else unit[i] = Integer.parseInt(unitString.substring(pos,endPos));
	    } else {
		unit[i] = 0;
	    }
	}
    }


    /** Throws a runtime exception if this attribute is not nominal. */
    private void ensureNominal() {
	if (!isNominal())
	    throw new RuntimeException("Attribute "+this.toString()+" is not a nominal attribute!");
    }

    /** Throws a runtime exception if this attribute is not a classification attribute. */
    private void ensureClassification() {
	if (!isBooleanClassification())
	    throw new RuntimeException("Attribute "+this.toString()+" is not a classification attribute!");
    }

    /** Returns true iff value is the default value for this attribute. The default for
     *  numerical attributes is 0.0, the default for nominal attributes is
     *  {@link Attribute#FIRST_CLASS_INDEX}. */
    public boolean isDefault(double value) {
	if (isNominal()) {
	    return value == FIRST_CLASS_INDEX;
	} else {
	    return value == 0.0;
	}
    }

    /** Writes the (non transient) attribute data to an output stream. */
    public void writeType(DataOutput out) throws IOException {
	out.writeUTF(name);
	out.writeInt(valueType);
	out.writeInt(blockType);
	out.writeInt(unit.length);
	for (int i = 0; i < unit.length; i++)
	    out.writeInt(unit[i]);
	if (isNominal()) {
	    out.writeInt(getNumberOfValues());
	    Iterator i = getValues().iterator();
	    while (i.hasNext()) {
		out.writeUTF((String)i.next());
	    }
	}
    }

    /** Writes the (non transient) attribute data to an output stream. */
    public static Attribute readType(DataInput in) throws IOException {
	Attribute attribute = new Attribute(in.readUTF());
	attribute.valueType = in.readInt();
	attribute.blockType = in.readInt();
	attribute.unit = new int[in.readInt()];
	for (int i = 0; i < attribute.unit.length; i++)
	    attribute.unit[i] = in.readInt();
	if (attribute.isNominal()) {
	    attribute.symbolToIndexMap = new HashMap();
	    attribute.indexToSymbolMap = new LinkedHashMap();
	    int num = in.readInt();
	    for (int i = 0; i < num; i++)
		attribute.mapString(in.readUTF());
	}
	return attribute;
    }
}

⌨️ 快捷键说明

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