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

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

import edu.udo.cs.yale.operator.performance.*;
import edu.udo.cs.yale.example.test.*;
import edu.udo.cs.yale.example.*;
import edu.udo.cs.yale.tools.att.*;
import java.util.*;

/** Tests classification criteria.
 *
 *  @version $Id: ClassificationCriterionTest.java,v 1.11 2004/08/27 11:57:43 ingomierswa Exp $
 */
public class ClassificationCriterionTest extends CriterionTestCase {


    public void testClassificationError() throws Exception {
	Attribute label = ExampleTestTools.attributeYesNo();
	label.setIndex(0);
 	List attributeList = new LinkedList();
	attributeList.add(label);
	MemoryExampleTable exampleTable
	    = new MemoryExampleTable(attributeList, 
				     ExampleTestTools.createDataRowReader(new DataRowFactory(DataRowFactory.TYPE_DOUBLE_ARRAY),
									  new Attribute[] {label},
									  new String[][] { {"yes"},
											   {"no"},
											   {"yes"},
											   {"no"},
											   {"yes"},
											   {"no"},
											   {"yes"},
											   {"yes"},
											   {"yes"},
											   {"no"},
											   {"no"},
											   {"yes"}}
											   ));


	AttributeSet attributeSet = new AttributeSet();
	attributeSet.setSpecialAttribute("label", label);
	
	ExampleSet eSet = exampleTable.createExampleSet(attributeSet);
	ExampleTestTools.createPredictedLabel(eSet);
	
	//eSet.createPredictedLabel();
	ExampleReader r = eSet.getExampleReader();
	Example e;
	e = r.next(); e.setPredictedLabel("yes"); // yy
	e = r.next(); e.setPredictedLabel("no");  // nn
	e = r.next(); e.setPredictedLabel("no");  // yn
	e = r.next(); e.setPredictedLabel("yes"); // ny
	e = r.next(); e.setPredictedLabel("yes"); // yy
	e = r.next(); e.setPredictedLabel("no");  // nn
	e = r.next(); e.setPredictedLabel("yes"); // yy
	e = r.next(); e.setPredictedLabel("no");  // yn
	e = r.next(); e.setPredictedLabel("no");  // yn
	e = r.next(); e.setPredictedLabel("no");  // nn
	e = r.next(); e.setPredictedLabel("yes"); // ny
	e = r.next(); e.setPredictedLabel("yes"); // yy
	// 4x yy
	// 3x nn
	// 3x yn
	// 2x ny

	PerformanceVector pv = new PerformanceVector();
	for (int i = 0; i < MultiClassificationPerformance.NAME.length; i++)
	    pv.addCriterion(new MultiClassificationPerformance(i));
	for (int i = 0; i < BinaryClassificationPerformance.NAME.length; i++)
	    pv.addCriterion(new BinaryClassificationPerformance(i));
	PerformanceEvaluator.evaluate(null, eSet, pv, false);
	
	assertEquals("accuracy", 7.0 / 12.0, pv.getCriterion(MultiClassificationPerformance.NAME[MultiClassificationPerformance.ACCURACY]).getValue(), 0.00000001);
	assertEquals("classification_error", 5.0 / 12.0, pv.getCriterion(MultiClassificationPerformance.NAME[MultiClassificationPerformance.ERROR]).getValue(), 0.00000001);
	assertEquals("precision", 4.0 /  6.0, pv.getCriterion(BinaryClassificationPerformance.NAME[BinaryClassificationPerformance.PRECISION]).getValue(), 0.00000001);
	assertEquals("recall", 4.0 /  7.0, pv.getCriterion(BinaryClassificationPerformance.NAME[BinaryClassificationPerformance.RECALL]).getValue(), 0.00000001);
	assertEquals("fallout", 2.0 /  5.0, pv.getCriterion(BinaryClassificationPerformance.NAME[BinaryClassificationPerformance.FALLOUT]).getValue(), 0.00000001);
	assertEquals("true_pos", 4, pv.getCriterion(BinaryClassificationPerformance.NAME[BinaryClassificationPerformance.TRUE_POSITIVE]).getValue(), 0.00000001);
	assertEquals("true_neg", 3, pv.getCriterion(BinaryClassificationPerformance.NAME[BinaryClassificationPerformance.TRUE_NEGATIVE]).getValue(), 0.00000001);
	assertEquals("false_pos", 2, pv.getCriterion(BinaryClassificationPerformance.NAME[BinaryClassificationPerformance.FALSE_POSITIVE]).getValue(), 0.00000001);
	assertEquals("false_neg", 3, pv.getCriterion(BinaryClassificationPerformance.NAME[BinaryClassificationPerformance.FALSE_NEGATIVE]).getValue(), 0.00000001);
    }

    public void testUCCClone() {
	int counter[][] = {{3, 5},{4, 6}};
	cloneTest("", new BinaryClassificationPerformance(BinaryClassificationPerformance.TRUE_POSITIVE, counter));
	cloneTest("", new BinaryClassificationPerformance(BinaryClassificationPerformance.TRUE_NEGATIVE, counter));
	cloneTest("", new BinaryClassificationPerformance(BinaryClassificationPerformance.FALSE_POSITIVE, counter));
	cloneTest("", new BinaryClassificationPerformance(BinaryClassificationPerformance.FALSE_NEGATIVE, counter));

    }

    public void testUCCAverage() {
	int counter1[][] = {{3, 5}, {4, 6}};
	int counter2[][] = {{5, 8}, {2, 9}};
	int sum[][]      = {{8, 13},{6, 15}};
	BinaryClassificationPerformance[] ucc1 = new BinaryClassificationPerformance[4];
	ucc1[0] = new BinaryClassificationPerformance(BinaryClassificationPerformance.TRUE_POSITIVE, counter1);
	ucc1[1] = new BinaryClassificationPerformance(BinaryClassificationPerformance.TRUE_NEGATIVE, counter1);
	ucc1[2] = new BinaryClassificationPerformance(BinaryClassificationPerformance.FALSE_POSITIVE, counter1);
	ucc1[3] = new BinaryClassificationPerformance(BinaryClassificationPerformance.FALSE_NEGATIVE, counter1);

	BinaryClassificationPerformance[] ucc2 = new BinaryClassificationPerformance[4];
	ucc2[0] = new BinaryClassificationPerformance(BinaryClassificationPerformance.TRUE_POSITIVE, counter2);
	ucc2[1] = new BinaryClassificationPerformance(BinaryClassificationPerformance.TRUE_NEGATIVE, counter2);
	ucc2[2] = new BinaryClassificationPerformance(BinaryClassificationPerformance.FALSE_POSITIVE, counter2);
	ucc2[3] = new BinaryClassificationPerformance(BinaryClassificationPerformance.FALSE_NEGATIVE, counter2);
	
	BinaryClassificationPerformance[] avg = new BinaryClassificationPerformance[4];
	avg[0] = new BinaryClassificationPerformance(BinaryClassificationPerformance.TRUE_POSITIVE, sum);
	avg[1] = new BinaryClassificationPerformance(BinaryClassificationPerformance.TRUE_NEGATIVE, sum);
	avg[2] = new BinaryClassificationPerformance(BinaryClassificationPerformance.FALSE_POSITIVE, sum);
	avg[3] = new BinaryClassificationPerformance(BinaryClassificationPerformance.FALSE_NEGATIVE, sum);

	for (int i = 0; i < ucc1.length; i++) {
	    ucc1[i].buildAverage(ucc2[i]);
	    assertEquals(ucc1[i].getName(), avg[i].getValue(), ucc1[i].getValue(), 0.0000001);
	}
    }
}

⌨️ 快捷键说明

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