몰래 쓴 svm 라 이브 러 리

4003 단어 알고리즘
오늘 아침 에 새로운 라 이브 러 리 를 만 났 습 니 다.dlib(http://dlib.net),정말 잘 썼어 요.공식 설명 에 따 르 면 These wrappers provide a portable object oriented interface for networking,multithreading,GUI development,and file browsing.프로그램 written using them can be compiled under POSIX or MS Windows platforms without change the code.즉,DLIB 는 C++라 이브 러 리 입 니 다.이식 가능 한 응용 프로그램 과 네트워크 처리,스 레 드,그래 픽 인터페이스,데이터 구조,선형 대수,기계 학습,XML 과 텍스트 분석,수치 최적화,베 이 루스 망 과 많은 다른 임 무 를 개발 하 는 데 사용 된다.거의 데이터 분석의 여러 방면 에 관련 되 었 다.더 중요 한 것 은 openCV 와 같이 상세 한 example 를 많이 제공 하기 때문에 공부 하기 어렵 지 않 을 것 이다.오늘 첫 접촉 으로 svm 분류의 example 에 따라 두 가지 분류 라 이브 러 리 로 바 꾸 었 고 여러 가지 분류 기 를 나중에 천천히 넣 었 습 니 다.하지만 기능 이 완벽 하지 않 을 것 으로 보인다.한 마디 로 하면 먼저 올 려 놓 고 나중에 천천히 고치 세 요.현 재 는 nu 와 C 매개 변수 에 대한 조정 입 니 다.기본 값 은 ganmma 와 C 조 참 입 니 다.이 두 가지 가 결과 에 가장 큰 영향 을 미 치기 때 문 입 니 다.
#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: }; }

좋은 웹페이지 즐겨찾기