📄 costmatrix.java
字号:
/*
* 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/**
* Title: XELOPES Data Mining Library
* Description: The XELOPES library is an open platform-independent and data-source-independent library for Embedded Data Mining.
* Copyright: Copyright (c) 2002 Prudential Systems Software GmbH
* Company: ZSoft (www.zsoft.ru), Prudsys (www.prudsys.com)
* @author Valentine Stepanenko (valentine.stepanenko@zsoft.ru)
* @version 1.0
*/
package com.prudsys.pdm.Models.Classification;
import java.util.Collection;
import com.prudsys.pdm.Core.CategoricalAttribute;
import com.prudsys.pdm.Core.Category;
import com.prudsys.pdm.Core.MiningException;
/**
* Defines cost of misclassifications. <p>
*
* A CostMatrix is used to measure the cost of predictions made by a
* supervised model. The value at entry (j, k) of a cost matrix specifies
* the user valuation of predicting target class j when target class k is
* the truth. <p>
*
* By default, returns 0 on the diagonal and 1 on the off-diagonal
* if no other value has been set.
*
* From CWM Data Mining. <p>
*
* Superclasses:
* <ul>
* <li> Class
* </ul>
*/
public class CostMatrix extends com.prudsys.pdm.Cwm.Core.Class
{
// -----------------------------------------------------------------------
// Variables declarations
// -----------------------------------------------------------------------
/** Reference to classification settings. Not used. */
public ClassificationSettings settings[];
/** Number of rows (=columns) of the matrix. */
private int size = 0;
/** Categorical attribute used as container for categories. */
private CategoricalAttribute categories = new CategoricalAttribute();
/** Cost values of the matrix. */
private double[][] values = null;
// -----------------------------------------------------------------------
// Constructors
// -----------------------------------------------------------------------
/**
* Empty constructor.
*/
public CostMatrix()
{
this(0);
}
/**
* Constructor with given number of rows.
*
* @param size number of rows (= #coumns) of cost matrix
*/
public CostMatrix(int size) {
this.size = size;
this.values = new double[size][size];
}
// -----------------------------------------------------------------------
// Getter and setter methods
// -----------------------------------------------------------------------
/**
* Returns size of cost matrix, i.e. the maximum number of categories
* ( = #rows = #columns) that build up the matrix.
*
* @return size of cost matrix
*/
public int getSize() {
return size;
}
/**
* Set size of cost matrix, i.e. the maximum number of categories
* ( = #rows = #columns) that build up the matrix.
*
* @param size new size of cost matrix
*/
public void setSize(int size) {
this.size = size;
this.values = new double[size][size];
categories.removeValues();
}
/**
* Returns collection of all categories of the cost matrix.
*
* @return categories of cost matrix
*/
public Collection getCategories() {
return categories.getValues();
}
/**
* Sets new cost value for category pair. If a category is used for
* the first time, it is dynamically added - supposed the number of
* categories does not exceed the size. In the latter case an exception
* is thrown.
*
* @param actualTarget actual target value
* @param predictedTarget predicted target value
* @param cost costs of misprediction
* @throws MiningException unknown category that can't be used because
* already size of matrix is reached
*/
public void setValue(Category actualTarget, Category predictedTarget, double cost)
throws MiningException {
if ( categories.getCategoriesNumber() == 0 )
throw new MiningException("size of matrix is zero");
int ncatt = categories.getCategoriesNumber();
double kact = categories.getKey(actualTarget);
double kpred = categories.getKey(predictedTarget);
if ( (Category.isMissingValue(kact) || Category.isMissingValue(kpred)) && ncatt == size)
throw new MiningException("can't set value for unknown category since alraedy at bounds");
if ( (Category.isMissingValue(kact) && Category.isMissingValue(kpred)) && ncatt == size-1)
throw new MiningException("can't set value for unknown categories since alraedy at bounds");
if ( Category.isMissingValue(kact) ) kact = categories.addCategory(actualTarget);
if ( Category.isMissingValue(kpred) ) kpred = categories.addCategory(predictedTarget);
values[ (int) kact ][ (int) kpred ] = cost;
}
/**
* Returns cost value for category pair.
*
* @param actualTarget actual target value
* @param predictedTarget predicted target value
* @return cost costs of misprediction
* @throws MiningException unknown category doesn't belong to cost matrix
*/
public double getValue(Category actualTarget, Category predictedTarget)
throws MiningException {
double kact = categories.getKey(actualTarget);
double kpred = categories.getKey(predictedTarget);
if ( Category.isMissingValue(kact) || Category.isMissingValue(kpred) )
throw new MiningException("category doesn't belong to cost matrix");
return values[ (int) kact ][ (int) kpred ];
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -