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

📄 glearner.cpp

📁 一个非常有用的开源代码
💻 CPP
字号:
/*	Copyright (C) 2006, Mike Gashler	This library is free software; you can redistribute it and/or	modify it under the terms of the GNU Lesser General Public	License as published by the Free Software Foundation; either	version 2.1 of the License, or (at your option) any later version.	see http://www.gnu.org/copyleft/lesser.html*/#include "GLearner.h"#include "GArff.h"#include <stdlib.h>#include <string.h>#include "GMacros.h"GSupervisedLearner::GSupervisedLearner(GArffRelation* pRelation){	m_pRelation = pRelation;}GSupervisedLearner::~GSupervisedLearner(){}double GSupervisedLearner::MeasurePredictiveAccuracy(GArffData* pData){	int nInputCount = m_pRelation->GetInputCount();	int nOutputCount = m_pRelation->GetOutputCount();	int nAttributes = m_pRelation->GetAttributeCount();	int nIndex;	GTEMPBUF(double, pVector, nAttributes);	double* pRow;	int nRowCount = pData->GetSize();	double dCorrectCount = 0;	double d;	int nTotalCount = 0;	int n, i;	for(n = 0; n < nRowCount; n++)	{		pRow = pData->GetVector(n);		// Copy the input values into the vector		for(i = 0; i < nInputCount; i++)		{			nIndex = m_pRelation->GetInputIndex(i);			pVector[nIndex] = pRow[nIndex];		}		// Mess up the output values just to be safe		for(i = 0; i < nOutputCount; i++)		{			nIndex = m_pRelation->GetOutputIndex(i);			pVector[nIndex] = 1e100;		}		// Evaluate		Eval(pVector);		// Check the answer		for(i = 0; i < nOutputCount; i++)		{			nIndex = m_pRelation->GetOutputIndex(i);			if(m_pRelation->GetAttribute(nIndex)->IsContinuous())			{				// Predictive accuracy doesn't really make sense for real values,				// so we'll just use a squashed squared error for an estimate				d = pRow[nIndex] - pVector[nIndex];				dCorrectCount += (1.0 - (1.0 / (1.0 + (d * d))));			}			else			{				if((int)pVector[nIndex] == (int)pRow[nIndex])					dCorrectCount++;			}			nTotalCount++;		}	}	return dCorrectCount / nTotalCount;}double GSupervisedLearner::MeasureMeanSquaredError(GArffData* pData){	int nInputCount = m_pRelation->GetInputCount();	int nOutputCount = m_pRelation->GetOutputCount();	int nAttributes = m_pRelation->GetAttributeCount();	int nIndex;	GTEMPBUF(double, pVector, nAttributes);	double* pRow;	int nRowCount = pData->GetSize();	double dError = 0;	double d;	int n, i;	for(n = 0; n < nRowCount; n++)	{		pRow = pData->GetVector(n);		// Copy the input values into the sample		for(i = 0; i < nInputCount; i++)		{			nIndex = m_pRelation->GetInputIndex(i);			pVector[nIndex] = pRow[nIndex];		}		// Mess up the output values just to be safe		for(i = 0; i < nOutputCount; i++)		{			nIndex = m_pRelation->GetOutputIndex(i);			pVector[nIndex] = 1e100;		}		// Evaluate		Eval(pVector);		// Check the answer		for(i = 0; i < nOutputCount; i++)		{			nIndex = m_pRelation->GetOutputIndex(i);			if(m_pRelation->GetAttribute(nIndex)->IsContinuous())			{				d = pRow[nIndex] - pVector[nIndex];				dError += (d * d);			}			else			{				// Squared error doesn't really make sense for discreet				// values, so we'll just say an incorrect classification				// corresponds to an error of 1.				if((int)pVector[nIndex] != (int)pRow[nIndex])					dError += 1;			}		}	}	return dError / (nRowCount * nOutputCount);}

⌨️ 快捷键说明

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