예제 #1
0
int main()
{

	const int32_t feature_cache=0;
	const int32_t kernel_cache=0;
	const float64_t rbf_width=10;
	const float64_t svm_C=10;
	const float64_t svm_eps=0.001;

	init_shogun();

	gen_rand_data();

	// create train labels
	CLabels* labels=new CLabels(SGVector<float64_t>(lab, NUM));
	SG_REF(labels);

	// create train features
	CSimpleFeatures<float64_t>* features = new CSimpleFeatures<float64_t>(feature_cache);
	SG_REF(features);
	features->set_feature_matrix(feat, DIMS, NUM);

	// create gaussian kernel
	CGaussianKernel* kernel = new CGaussianKernel(kernel_cache, rbf_width);
	SG_REF(kernel);
	kernel->init(features, features);

	// create svm via libsvm and train
	CLibSVM* svm = new CLibSVM(svm_C, kernel, labels);
	SG_REF(svm);
	svm->set_epsilon(svm_eps);
	svm->train();

	printf("num_sv:%d b:%f\n", svm->get_num_support_vectors(), svm->get_bias());

	// classify + display output
	CLabels* out_labels=svm->apply();

	for (int32_t i=0; i<NUM; i++)
		printf("out[%d]=%f\n", i, out_labels->get_label(i));

	SG_UNREF(labels);
	SG_UNREF(out_labels);
	SG_UNREF(kernel);
	SG_UNREF(features);
	SG_UNREF(svm);

	exit_shogun();
	return 0;
}
예제 #2
0
CLabels* CAUCKernel::setup_auc_maximization(CLabels* labels)
{
	SG_INFO( "setting up AUC maximization\n") ;
	ASSERT(labels);
	ASSERT(labels->is_two_class_labeling());

	// get the original labels
	int32_t num=0;
	ASSERT(labels);
	int32_t* int_labels=labels->get_int_labels(num);
	ASSERT(subkernel->get_num_vec_rhs()==num);

	// count positive and negative
	int32_t num_pos=0;
	int32_t num_neg=0;

	for (int32_t i=0; i<num; i++)
	{
		if (int_labels[i]==1)
			num_pos++;
		else 
			num_neg++;
	}

	// create AUC features and labels (alternate labels)
	int32_t num_auc = num_pos*num_neg;
	SG_INFO("num_pos: %i  num_neg: %i  num_auc: %i\n", num_pos, num_neg, num_auc);

	uint16_t* features_auc = new uint16_t[num_auc*2];
	int32_t* labels_auc = new int32_t[num_auc];
	int32_t n=0 ;

	for (int32_t i=0; i<num; i++)
	{
		if (int_labels[i]!=1)
			continue;

		for (int32_t j=0; j<num; j++)
		{
			if (int_labels[j]!=-1)
				continue;

			// create about as many positively as negatively labeled examples
			if (n%2==0)
			{
				features_auc[n*2]=i;
				features_auc[n*2+1]=j;
				labels_auc[n]=1;
			}
			else
			{
				features_auc[n*2]=j;
				features_auc[n*2+1]=i;
				labels_auc[n]=-1;
			}

			n++;
			ASSERT(n<=num_auc);
		}
	}

	// create label object and attach it to svm
	CLabels* lab_auc = new CLabels(num_auc);
	lab_auc->set_int_labels(labels_auc, num_auc);
	SG_REF(lab_auc);

	// create feature object
	CSimpleFeatures<uint16_t>* f = new CSimpleFeatures<uint16_t>(0);
	f->set_feature_matrix(features_auc, 2, num_auc);

	// create AUC kernel and attach the features
	init(f,f);

	delete[] int_labels;
	delete[] labels_auc;

	return lab_auc;
}
예제 #3
0
int main()
{
	init_shogun_with_defaults();

	// Generate random data, features and labels
	gen_rand_data();

	// Create features
	CSimpleFeatures<float64_t>* features = new CSimpleFeatures<float64_t>();
	SG_REF(features);
	features->set_feature_matrix(feat, DIMS, NUM);

	// Create a StreamingSimpleFeatures object which uses the above as input; labels (float64_t*) are optional
	CStreamingSimpleFeatures<float64_t>* streaming_simple = new CStreamingSimpleFeatures<float64_t>(features, lab);
	SG_REF(streaming_simple);

	// Start parsing of the examples; in this case, it is trivial - returns each vector from the SimpleFeatures object
	streaming_simple->start_parser();

	int32_t counter=0;
	SG_SPRINT("Processing examples...\n\n");

	// Run a while loop over all the examples.  Note that since
	// features are "streaming", there is no predefined
	// number_of_vectors known to the StreamingFeatures object.
	// Thus, this loop must be used to iterate over all the
	// features
	while (streaming_simple->get_next_example())
	{
		counter++;
		// Get the current vector; no other vector is accessible
		SGVector<float64_t> vec = streaming_simple->get_vector();
		float64_t label = streaming_simple->get_label();

		SG_SPRINT("Vector %d: [\t", counter);
		for (int32_t i=0; i<vec.vlen; i++)
		{
			SG_SPRINT("%f\t", vec.vector[i]);
		}
		SG_SPRINT("Label=%f\t", label);

		// Calculate dot product of the current vector (from
		// the StreamingFeatures object) with itself (the
		// vector passed as argument)
		float64_t dot_prod = streaming_simple->dense_dot(vec.vector, vec.vlen);

		SG_SPRINT("]\nDot product of the vector with itself: %f", dot_prod);
		SG_SPRINT("\n\n");

		// Free the example, since we are done with processing it.
		streaming_simple->release_example();
	}

	// Now that all examples are used, end the parser.
	streaming_simple->end_parser();

	SG_FREE(lab);

	SG_UNREF(streaming_simple);
	SG_UNREF(features);

	exit_shogun();
	return 0;
}
예제 #4
0
int main(int argc,char *argv[])
{
	init_shogun(&print_message,&print_message,&print_message);//initialising shogun without giving arguments shogun wont be able to print
	int32_t x_n=4,x_d=2;//X dimensions : x_n for no of datapoints and x_d for dimensionality of data
	SGMatrix<float64_t> fmatrix(x_d,x_n);


	SG_SPRINT("\nTEST 1:\n\n");

/*Initialising Feature Matrix */

	for (int i=0; i<x_n*x_d; i++)
		fmatrix.matrix[i] = i+1;
	SG_SPRINT("FEATURE MATRIX :\n");	
	CMath::display_matrix(fmatrix.matrix,x_d,x_n);

	CSimpleFeatures<float64_t>* features = new CSimpleFeatures<float64_t>(fmatrix);
	SG_REF(features);
	
/*Creating random labels */
	CLabels* labels=new CLabels(x_n);
	
	// create labels, two classes 
	labels->set_label(0,1);
	labels->set_label(1,-1);
	labels->set_label(2,1);
	labels->set_label(3,1);
	SG_REF(labels);
	
/*Working with Newton SVM */

	float64_t lambda=1.0;
	int32_t iter=20;	

	CNewtonSVM *nsvm = new CNewtonSVM(lambda,features,labels,iter);
	SG_REF(nsvm);
	nsvm->train();
	SG_UNREF(labels);
	SG_UNREF(nsvm);

	SG_SPRINT("TEST 2:\n\n");

	
	x_n=5;
	x_d=3;
	SGMatrix<float64_t> fmatrix2(x_d,x_n);	
	for (int i=0; i<x_n*x_d; i++)
		fmatrix2.matrix[i] = i+1;
	SG_SPRINT("FEATURE MATRIX :\n");	
	CMath::display_matrix(fmatrix2.matrix,x_d,x_n);
	features->set_feature_matrix(fmatrix2);
	SG_REF(features);
	
/*Creating random labels */
	CLabels* labels2=new CLabels(x_n);
	
	// create labels, two classes 
	labels2->set_label(0,1);
	labels2->set_label(1,-1);
	labels2->set_label(2,1);
	labels2->set_label(3,1);
	labels2->set_label(4,-1);
	SG_REF(labels2);
	
/*Working with Newton SVM */

	lambda=1.0;
	iter=20;	

	CNewtonSVM *nsvm2 = new CNewtonSVM(lambda,features,labels2,iter);
	SG_REF(nsvm2);
	nsvm2->train();


	SG_UNREF(labels2);
	SG_UNREF(nsvm2);
	SG_UNREF(features);
	exit_shogun();
	return 0;
}