Commit c87e5d53 authored by FPoupon's avatar FPoupon
Browse files

add SVM cross-validation

parent 73302c88
......@@ -346,6 +346,7 @@ gkg-processing-io/VectorBinaryItemWriter.h
gkg-processing-io/VectorBSwapItemReader.h
gkg-processing-io/VectorBSwapItemWriter.h
gkg-processing-io/VectorItemIOFactory.h
gkg-processing-machinelearning/MachineLearningCrossValidation.h
gkg-processing-machinelearning/MachineLearningData.h
gkg-processing-machinelearning/MachineLearningData_i.h
gkg-processing-machinelearning/MachineLearningDataImplementation.h
......@@ -833,6 +834,7 @@ gkg-processing-io/VectorBSwapItemReader.cxx
gkg-processing-io/VectorBSwapItemWriter.cxx
gkg-processing-io/VectorItemIOFactory.cxx
gkg-processing-io/VolumeDiskFormatFactory.cxx
gkg-processing-machinelearning/MachineLearningCrossValidation.cxx
gkg-processing-machinelearning/MachineLearningData.cxx
gkg-processing-machinelearning/MachineLearningDataImplementation.cxx
gkg-processing-machinelearning/MachineLearningImplementationFactory.cxx
......
#include <gkg-processing-machinelearning/MachineLearningCrossValidation.h>
gkg::MachineLearningCrossValidation::MachineLearningCrossValidation(
double theMeanSquareError,
double theSquaredCorrelationCoefficient,
double theAccuracy )
: meanSquareError( theMeanSquareError ),
squaredCorrelationCoefficient(
theSquaredCorrelationCoefficient ),
accuracy( theAccuracy )
{
}
gkg::MachineLearningCrossValidation::MachineLearningCrossValidation(
const MachineLearningCrossValidation& other )
: meanSquareError( other.meanSquareError ),
squaredCorrelationCoefficient(
other.squaredCorrelationCoefficient ),
accuracy( other.accuracy )
{
}
#ifndef _gkg_processing_machinelearning_MachineLearningCrossValidation_h_
#define _gkg_processing_machinelearning_MachineLearningCrossValidation_h_
namespace gkg
{
struct MachineLearningCrossValidation
{
MachineLearningCrossValidation( double theMeanSquareError = 0.0,
double theSquaredCorrelationCoefficient = 0.0,
double theAccuracy = 0.0 );
MachineLearningCrossValidation( const MachineLearningCrossValidation& other );
double meanSquareError;
double squaredCorrelationCoefficient;
double accuracy;
};
}
#endif
......@@ -97,6 +97,26 @@ void gkg::SupportVectorMachine::train(
}
gkg::MachineLearningCrossValidation gkg::SupportVectorMachine::crossValidation(
const gkg::MachineLearningProblem& problem,
int32_t foldCount )
{
try
{
return _supportVectorMachineImplementation->crossValidation( problem,
foldCount );
}
GKG_CATCH( "gkg::MachineLearningCrossValidation "
"gkg::SupportVectorMachine::crossValidation( "
"const gkg::MachineLearningProblem& problem, "
"int32_t foldCount )" );
}
void gkg::SupportVectorMachine::predict( const gkg::MachineLearningData& data,
gkg::Vector& labels,
gkg::Matrix* probabilities ) const
......
......@@ -2,7 +2,9 @@
#define _gkg_processing_machinelearning_SupportVectorMachine_h_
#include <gkg-processing-machinelearning/MachineLearningCrossValidation.h>
#include <gkg-core-cppext/StdInt.h>
#include <vector>
#include <string>
......@@ -61,6 +63,9 @@ class SupportVectorMachine
const std::vector< double >& weights );
void train( const MachineLearningProblem& problem );
MachineLearningCrossValidation crossValidation(
const MachineLearningProblem& problem,
int32_t foldCount );
void predict( const MachineLearningData& data,
Vector& labels,
Matrix* probabilities = 0 ) const;
......
......@@ -26,6 +26,9 @@ class SupportVectorMachineImplementation
const std::vector< double >& weights ) = 0;
virtual void train( const MachineLearningProblem& problem ) = 0;
virtual MachineLearningCrossValidation crossValidation(
const MachineLearningProblem& problem,
int32_t foldCount ) = 0;
virtual void predict( const MachineLearningData& data,
Vector& labels,
Matrix* probabilities ) const = 0;
......
......@@ -200,6 +200,91 @@ void gkg::LibsvmSupportVectorMachineImplementation::train(
}
gkg::MachineLearningCrossValidation
gkg::LibsvmSupportVectorMachineImplementation::crossValidation(
const gkg::MachineLearningProblem& machineLearningProblem,
int32_t foldCount )
{
try
{
gkg::MachineLearningCrossValidation result;
const svm_problem& problem =
static_cast< gkg::LibsvmMachineLearningProblemImplementation* >(
machineLearningProblem.getImplementation() )->getLibsvmProblem();
double* target = new double[ problem.l ];
if ( target )
{
int32_t i, correct = 0;
double l = double( problem.l );
svm_cross_validation( &problem, &_parameters, foldCount, target );
if ( ( _parameters.svm_type == EPSILON_SVR ) ||
( _parameters.svm_type == NU_SVR ) )
{
double error = 0.0;
double sumv = 0.0, sumy = 0.0, sumvv = 0.0, sumyy = 0.0, sumvy = 0.0;
for ( i = 0; i < problem.l; i++ )
{
double v = target[ i ];
double y = problem.y[ i ];
error += ( v - y ) * ( v - y );
sumv += v;
sumy += y;
sumvv += v * v;
sumyy += y * y;
sumvy += v * y;
}
result.meanSquareError = error / l;
result.squaredCorrelationCoefficient =
( ( l * sumvy - sumv * sumy ) * ( l * sumvy - sumv * sumy ) ) /
( ( l * sumvv - sumv * sumv ) * ( l * sumyy - sumy * sumy ) );
}
else
{
for ( i = 0; i < problem.l; i++ )
{
if ( target[ i ] == problem.y[ i ] )
{
correct++;
}
}
result.accuracy = 100.0 * double( correct ) / l;
}
delete target;
}
return result;
}
GKG_CATCH( "gkg::MachineLearningCrossValidation "
"gkg::LibsvmSupportVectorMachineImplementation::crossValidation( "
"const gkg::MachineLearningProblem& problem, "
"int32_t foldCount )" );
}
void gkg::LibsvmSupportVectorMachineImplementation::predict(
const gkg::MachineLearningData& data,
gkg::Vector& labels,
......
......@@ -36,6 +36,9 @@ class LibsvmSupportVectorMachineImplementation :
const std::vector< double >& weights );
void train( const MachineLearningProblem& machineLearningProblem );
MachineLearningCrossValidation crossValidation(
const MachineLearningProblem& machineLearningProblem,
int32_t foldCount );
void predict( const MachineLearningData& data,
Vector& labels,
Matrix* probabilities ) const;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment