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

📄 datarowfactory.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.example;

import edu.udo.cs.yale.tools.Ontology;
import edu.udo.cs.yale.tools.LogService;

/** Factory class for DataRow objects. One factory should be used for
 *  one ExampleTable only. This class is necessary to customize implementations
 *  of DataRowReader to create DataRows of arbitrary type.
 *
 *  @version $Id: DataRowFactory.java,v 2.5 2004/08/27 11:57:31 ingomierswa Exp $ 
 */
public class DataRowFactory {

    public static final String[] TYPE_NAMES = { "double_array", "sparse_map" };

    public static final int FIRST_TYPE_INDEX  = 0;
    public static final int TYPE_DOUBLE_ARRAY = 0;
    public static final int TYPE_SPARSE_MAP   = 1;
    public static final int LAST_TYPE_INDEX   = 1;

    /** The type can be one out of TYPE_DOUBLE_ARRAY and TYPE_SPARSE_MAP. */
    private int type;

    /** @param type must be one out of TYPE_DOUBLE_ARRAY and TYPE_SPARSE_MAP. */
    public DataRowFactory(int type) {
	if ((type < FIRST_TYPE_INDEX) || (type > LAST_TYPE_INDEX))
	    throw new IllegalArgumentException("Illegal data row type: "+type);
	this.type = type;
    }

    /** Creates a new DataRow with the given initial capacity. */
    public DataRow create(int size) {
	DataRow row = null;
	switch (type) {
	case TYPE_DOUBLE_ARRAY:
	    row = new DoubleArrayDataRow(new double[size]);
	    break;
	case TYPE_SPARSE_MAP:
	    row = new SparseMapDataRow();
	    break;
	default:
	}
	return row;
    }

    /** Creates a data row from an array of Strings. If the corresponding attribute is nominal,
     *  the string is mapped to its index, otherwise it is parsed using 
     *  <code>Double.parseDouble(String)</code> .
     *  @see FileDataRowReader */
    public DataRow create(String[] strings, Attribute[] attributes) {
	DataRow dataRow = create(strings.length);
	for (int i = 0; i < strings.length; i++) {
	    if (strings[i] != null) strings[i] = strings[i].trim();
	    if ((strings[i] != null) && (strings[i].length() > 0) && (!strings[i].equals("?"))) {
		if (attributes[i].isNominal()) {
		    dataRow.set(attributes[i], attributes[i].mapString(strings[i]));
		} else {
		    dataRow.set(attributes[i], string2Double(strings[i]));
		}
	    } else {
		dataRow.set(attributes[i], Double.NaN);
	    }
	}
	return dataRow;
    }

    /** Creates a data row from an Object array. The classes of the object must match the
     *  value type of the corresponding {@link Attribute}. If the corresponding attribute
     *  is nominal, <code>data[i]</code> will be cast to String. If it is numerical,
     *  it will be cast to Number.
     *  @throws ClassCastException if data class does not match attribute type
     *  @see DatabaseDataRowReader */
    public DataRow create(Object[] data, Attribute[] attributes) {
	DataRow dataRow = create(data.length);
	for (int i = 0; i < data.length; i++) {
	    if (data[i] != null) {
		if (attributes[i].isNominal()) {
		    dataRow.set(attributes[i], attributes[i].mapString(((String)data[i]).trim()));
		} else {
		    dataRow.set(attributes[i], ((Number)data[i]).doubleValue());
		}
	    } else {
		dataRow.set(attributes[i], Double.NaN);
	    }
	}
	return dataRow;
    }


    /** Returns the type of the created data rows. */
    public int getType() {
	return type;
    }

    // --------------------------------------------------------------------------------

    public static final double string2Double(String str) {
	if (str == null) return Double.NaN;
	try {
	    return Double.parseDouble(str);
	} catch (NumberFormatException e) {
	    LogService.logMessage("DataRowFactory.string2Double(String): '"+str+"' is not a valid number!", 
				  LogService.ERROR);
	    return Double.NaN;
	}
    }

}

⌨️ 快捷键说明

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