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

📄 sparsereadertest.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.test;

import edu.udo.cs.yale.test.TestCase;
import edu.udo.cs.yale.example.*;
import edu.udo.cs.yale.tools.att.*;

import java.io.*;
import java.util.*;

/** Tests all formats of the {@link SparseFormatDataRowReader}
 *
 *  @version $Id: SparseReaderTest.java,v 1.4 2004/08/27 11:57:32 ingomierswa Exp $
 */
public class SparseReaderTest extends TestCase {

    private static final String[] ATTRIBUTE_STRINGS = {
	"5:3.0 2:8.0",
	"1:cat 3:2.5 4:1.5e-1",
	"5:1.0",
	"1:dog 4:7.3e1" };

    private static final String[] LABEL = {"yes", "no", "no", "yes"};

    public void readerTest(int format, Reader input, Reader labelInput) throws Exception {
	AttributeSet attributeSet = new AttributeSet();
	attributeSet.addAttribute(ExampleTestTools.attributeDogCatMouse());
	attributeSet.addAttribute(ExampleTestTools.attributeReal(1));
	attributeSet.addAttribute(ExampleTestTools.attributeReal(2));
	attributeSet.addAttribute(ExampleTestTools.attributeReal(3));
	attributeSet.addAttribute(ExampleTestTools.attributeReal(4));
	attributeSet.setSpecialAttribute("label", ExampleTestTools.attributeYesNo());
	java.util.Map prefixMap = new java.util.HashMap();
	prefixMap.put("l", "label");
	SparseFormatDataRowReader reader = new SparseFormatDataRowReader(new DataRowFactory(DataRowFactory.TYPE_SPARSE_MAP),
									 format,
									 prefixMap,
									 attributeSet, 
									 input, labelInput,
									 -1);
	MemoryExampleTable table = new MemoryExampleTable(attributeSet.getAllAttributes());
	table.readExamples(reader);
	ExampleSet exampleSet =  table.createExampleSet(attributeSet);
	ExampleReader r = exampleSet.getExampleReader();
	Example e = r.next();
	assertEquals("example 1, column 1", "dog", e.getValueAsString(0));
	assertEquals("example 1, column 2", 8.0,   e.getValue(1), 0.00000001);
	assertEquals("example 1, column 3", 0.0,   e.getValue(2), 0.00000001);
	assertEquals("example 1, column 4", 0.0,   e.getValue(3), 0.00000001);
	assertEquals("example 1, column 5", 3.0,   e.getValue(4), 0.00000001);
	assertEquals("example 1, label", "yes", e.getLabelAsString());

	e = r.next();
	assertEquals("example 2, column 1", "cat", e.getValueAsString(0));
	assertEquals("example 2, column 2", 0.0,   e.getValue(1), 0.00000001);
	assertEquals("example 2, column 3", 2.5,   e.getValue(2), 0.00000001);
	assertEquals("example 2, column 4", 0.15,  e.getValue(3), 0.00000001);
	assertEquals("example 2, column 5", 0.0,   e.getValue(4), 0.00000001);
	assertEquals("example 2, label", "no", e.getLabelAsString());

	e = r.next();
	assertEquals("example 3, column 1", "dog", e.getValueAsString(0));
	assertEquals("example 3, column 2", 0.0,   e.getValue(1), 0.00000001);
	assertEquals("example 3, column 3", 0.0,   e.getValue(2), 0.00000001);
	assertEquals("example 3, column 4", 0.0,   e.getValue(3), 0.00000001);
	assertEquals("example 3, column 5", 1.0,   e.getValue(4), 0.00000001);
	assertEquals("example 3, label", "no", e.getLabelAsString());

	e = r.next();
	assertEquals("example 4, column 1", "dog", e.getValueAsString(0));
	assertEquals("example 4, column 2", 0.0,   e.getValue(1), 0.00000001);
	assertEquals("example 4, column 3", 0.0,   e.getValue(2), 0.00000001);
	assertEquals("example 4, column 4", 73,    e.getValue(3), 0.00000001);
	assertEquals("example 4, column 5", 0.0,   e.getValue(4), 0.00000001);
	assertEquals("example 4, label", "yes", e.getLabelAsString());
    }


    public void testFormatXY() throws Exception {
	String input = "# comment\n";
	for (int i = 0; i < ATTRIBUTE_STRINGS.length; i++) {
	    input += ATTRIBUTE_STRINGS[i] + " " + LABEL[i] + "\n";
	}
	readerTest(SparseFormatDataRowReader.FORMAT_XY, new StringReader(input), null);
    }


    public void testFormatYX() throws Exception {
	String input = "# comment\n";
	for (int i = 0; i < ATTRIBUTE_STRINGS.length; i++) {
	    input += LABEL[i] + " " + ATTRIBUTE_STRINGS[i] + "\n";
	}
	readerTest(SparseFormatDataRowReader.FORMAT_YX, new StringReader(input), null);
    }

    public void testFormatPrefix() throws Exception {
	String input = "# comment\n";
	for (int i = 0; i < ATTRIBUTE_STRINGS.length; i++) {
	    input += "l:"+LABEL[i] + " " + ATTRIBUTE_STRINGS[i] + "\n";
	}
	readerTest(SparseFormatDataRowReader.FORMAT_PREFIX, new StringReader(input), null);
    }

    public void testFormatSeparate() throws Exception {
	String input = "# comment\n";
	String label = "";
	for (int i = 0; i < ATTRIBUTE_STRINGS.length; i++) {
	    label += LABEL[i] + "\n";
	    input += ATTRIBUTE_STRINGS[i] + "\n";
	}
	readerTest(SparseFormatDataRowReader.FORMAT_SEPARATE_FILE, new StringReader(input), new StringReader(label));
    }
}

⌨️ 快捷键说明

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