int
main(int, char *[])
{
constexpr unsigned int measurementVectorLength = 1;
sample->SetMeasurementVectorSize(measurementVectorLength);
std::vector<ClassSampleType::Pointer> classSamples;
for (unsigned int i = 0; i < 2; ++i)
{
classSamples[i]->SetSample(sample);
}
normalGenerator->Initialize(101);
MeasurementVectorType mv;
double mean = 100;
double standardDeviation = 30;
SampleType::InstanceIdentifier id = 0UL;
for (unsigned int i = 0; i < 100; ++i)
{
mv.Fill((normalGenerator->GetVariate() * standardDeviation) + mean);
sample->PushBack(mv);
classSamples[0]->AddInstance(id);
++id;
}
normalGenerator->Initialize(3024);
mean = 200;
standardDeviation = 30;
for (unsigned int i = 0; i < 100; ++i)
{
mv.Fill((normalGenerator->GetVariate() * standardDeviation) + mean);
sample->PushBack(mv);
classSamples[1]->AddInstance(id);
++id;
}
using CovarianceEstimatorType =
std::vector<CovarianceEstimatorType::Pointer> covarianceEstimators;
for (unsigned int i = 0; i < 2; ++i)
{
covarianceEstimators[i]->SetInput(classSamples[i]);
covarianceEstimators[i]->Update();
}
for (unsigned int i = 0; i < 2; ++i)
{
std::cout << "class[" << i << "] " << std::endl;
std::cout << " estimated mean : " << covarianceEstimators[i]->GetMean()
<< " covariance matrix : "
<< covarianceEstimators[i]->GetCovarianceMatrix() << std::endl;
}
using MembershipFunctionType =
DecisionRuleType::PriorProbabilityVectorType aPrioris;
aPrioris.push_back(
static_cast<double>(classSamples[0]->GetTotalFrequency()) /
static_cast<double>(sample->GetTotalFrequency()));
aPrioris.push_back(
static_cast<double>(classSamples[1]->GetTotalFrequency()) /
static_cast<double>(sample->GetTotalFrequency()));
decisionRule->SetPriorProbabilities(aPrioris);
classifier->SetDecisionRule(decisionRule);
classifier->SetInput(sample);
classifier->SetNumberOfClasses(2);
using ClassLabelVectorObjectType =
ClassifierType::ClassLabelVectorObjectType;
using ClassLabelVectorType = ClassifierType::ClassLabelVectorType;
ClassLabelVectorType classLabelVector = classLabelVectorObject->Get();
constexpr ClassifierType::ClassLabelType class1 = 100;
classLabelVector.push_back(class1);
constexpr ClassifierType::ClassLabelType class2 = 200;
classLabelVector.push_back(class2);
classLabelVectorObject->Set(classLabelVector);
classifier->SetClassLabels(classLabelVectorObject);
using MembershipFunctionVectorObjectType =
ClassifierType::MembershipFunctionVectorObjectType;
using MembershipFunctionVectorType =
ClassifierType::MembershipFunctionVectorType;
auto membershipFunctionVectorObject =
MembershipFunctionVectorType membershipFunctionVector =
membershipFunctionVectorObject->Get();
for (unsigned int i = 0; i < 2; ++i)
{
membershipFunction->SetMean(covarianceEstimators[i]->GetMean());
membershipFunction->SetCovariance(
covarianceEstimators[i]->GetCovarianceMatrix());
membershipFunctionVector.push_back(membershipFunction);
}
membershipFunctionVectorObject->Set(membershipFunctionVector);
classifier->SetMembershipFunctions(membershipFunctionVectorObject);
classifier->Update();
const ClassifierType::MembershipSampleType * membershipSample =
classifier->GetOutput();
ClassifierType::MembershipSampleType::ConstIterator iter =
membershipSample->Begin();
while (iter != membershipSample->End())
{
std::cout << "measurement vector = " << iter.GetMeasurementVector()
<< " class label = " << iter.GetClassLabel() << std::endl;
++iter;
}
return EXIT_SUCCESS;
}