몰래 쓴 svm 라 이브 러 리
4003 단어 알고리즘
#include
#include
#include
#include "dlib/rand/rand_kernel_abstract.h"
using namespace std;
using namespace dlib;
//svm , nFeatures ;
namespace SVM{
#define nFeatures 2
typedef matrix sample_type;// ;
typedef radial_basis_kernel kernel_type;// ;
typedef probabilistic_decision_function probabilistic_funct_type;
typedef normalized_function pfunct_type;
enum Trainer{CTrainer = 1, NUTrainer = 2};
enum LoadType {LoadSamples = 1, LoadTestData = 2};
class SVMClassification{
public:
SVMClassification(){
samples.clear();
labels.clear();
}
~SVMClassification(){}
bool loadData(const char* fn, int opt = LoadSamples)
{
if(! QFile::exists(fn))
{
cout << fn << "does not exist!
";
return false;
}
QFile infile(fn);
if (!infile.open(QIODevice::ReadOnly))
{
cout << fn << "open error!
";
return false;
}
QTextStream _in(&infile);
QString smsg = _in.readLine();
QStringList slist;
if(opt == LoadSamples)
{
samples.clear();
labels.clear();
}
else
testData.clear();
while(! _in.atEnd())
{
sample_type samp;
smsg = _in.readLine();
slist = smsg.split(",");
for (int i = 0; i < nFeatures; i ++)
{
samp(i) = slist[i+1].trimmed().toDouble();
//cout << samp(i)< best_result(1, 2);
best_result = 0;
best_gamma = 0.0001, best_nu = 0.0001, best_c = 5;
switch(opt)
{
case NUTrainer:
for (double gamma = 0.00001; gamma <= 1; gamma *= 5)
{
for (double nu = 0.00001; nu < max_nu; nu *= 5)
{
trainer.set_kernel(kernel_type(gamma));
trainer.set_nu(nu);
cout << "gamma: " << gamma << " nu: " << nu;
matrix result = cross_validate_trainer(trainer, samples, labels, 10);
cout << " cross validation accuracy: " << result;
if (sum(result) > sum(best_result))
{
best_result = result;
best_gamma = gamma;
best_nu = nu;
}
}
}
cout << "
best gamma: " << best_gamma < result = cross_validate_trainer(c_trainer, samples, labels, 10);
cout << " cross validation accuracy: " << result;
if (sum(result) > sum(best_result))
{
best_result = result;
best_gamma = gamma;
best_c = _c;
}
}
}
cout << "
best gamma: " << best_gamma <> learned_pfunct;
cout < samples;
std::vector labels;
std::vector testData;
svm_nu_trainer trainer;
svm_c_trainer c_trainer;
vector_normalizer normalizer;
double best_gamma;
double best_nu;
double best_c;
pfunct_type learned_pfunct;
protected:
};
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
【Codility Lesson3】FrogJmpA small frog wants to get to the other side of the road. The frog is currently located at position X and wants to get to...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.