示例#1
0
void ex_kmeans () {

	SampleList samples;
	ssi_size_t n_classes = 4;
	ssi_size_t n_sampels = 200;
	ssi_size_t n_streams = 1;
	ssi_real_t distr[][3] = { 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f };
	ModelTools::CreateTestSamples (samples, n_classes, n_sampels, n_streams, distr);

	// training
	{
		KMeans *model = ssi_create (KMeans, "kmeans", true);
		model->getOptions()->k = n_classes;
		Trainer trainer (model);
		trainer.train (samples);
		trainer.save ("kmeans");
	}

	// evaluation
	{
		Trainer trainer;
		Trainer::Load (trainer, "kmeans");			
		trainer.cluster (samples);

		ModelTools::PlotSamples(samples, "kmeans", ssi_rect(650, 0, 400, 400));
	} 

	// split
	{
		KMeans *model = ssi_create (KMeans, "kmeans", true);
		model->load("kmeans.trainer.KMeans.model");

		ISSelectSample ss (&samples);
		ss.setSelection (model->getIndicesPerClusterSize(1), model->getIndicesPerCluster(1));

		ModelTools::PlotSamples (ss, "kmeans", ssi_rect(650,0,400,400));
		
	}
}
示例#2
0
void Evaluation::evalLOO (Trainer *trainer, ISamples &samples) {

	_trainer = trainer;
	destroy_conf_mat ();
	init_conf_mat (samples);
	ssi_size_t n_samples = samples.getSize ();

	_n_total = n_samples;
	_result_vec = new ssi_size_t[2*_n_total];
	_result_vec_ptr = _result_vec;

	ssi_size_t itest  = 0;
	ssi_size_t *itrain = new ssi_size_t[n_samples - 1];
	for (ssi_size_t nsample = 0; nsample < n_samples - 1; ++nsample) {
		itrain[nsample] = nsample+1;
	}
	
	ISSelectSample strain (&samples);
	ISSelectSample stest (&samples);

	strain.setSelection  (n_samples-1, itrain);
	stest.setSelection (1, &itest);

	_trainer->release ();
	if (_fselmethod) {
		_trainer->setSelection (strain, _fselmethod, _pre_fselmethod, _n_pre_feature);
	}
	if (_preproc_mode) {
		_trainer->setPreprocMode (_preproc_mode, _n_streams_refs, _stream_refs);
	}
	_trainer->train (strain);
	eval_h (stest);		

	for (ssi_size_t nsample = 1; nsample < n_samples; ++nsample) {
		
		itrain[nsample-1] = nsample-1;
		itest = nsample;

		strain.setSelection  (n_samples-1, itrain);
		stest.setSelection (1, &itest);

		_trainer->release ();	
		if (_fselmethod) {
			_trainer->setSelection (strain, _fselmethod, _pre_fselmethod, _n_pre_feature);
		}
		if (_preproc_mode) {
			_trainer->setPreprocMode (_preproc_mode, _n_streams_refs, _stream_refs);
		}
		_trainer->train (strain);

		eval_h (stest);		
	}

	delete [] itrain;

}