예제 #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));

	// create train features
	CDenseFeatures<float64_t>* features = new CDenseFeatures<float64_t>(feature_cache);
	SG_REF(features);
	features->set_feature_matrix(feat);

	// 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(out_labels);
	SG_UNREF(kernel);
	SG_UNREF(features);
	SG_UNREF(svm);

	exit_shogun();
	return 0;
}
예제 #2
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);

	CDenseFeatures<float64_t>* features = new CDenseFeatures<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;
}
예제 #3
0
CLabels* CAUCKernel::setup_auc_maximization(CLabels* labels)
{
	SG_INFO( "setting up AUC maximization\n") ;
	ASSERT(labels);
	ASSERT(labels->get_label_type() == LT_BINARY);
	labels->ensure_valid();

	// get the original labels
	SGVector<int32_t> int_labels=((CBinaryLabels*) labels)->get_int_labels();
	ASSERT(subkernel->get_num_vec_rhs()==int_labels.vlen);

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

	for (int32_t i=0; i<int_labels.vlen; i++)
	{
		if (int_labels.vector[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);

	SGMatrix<uint16_t> features_auc(2,num_auc);
	int32_t* labels_auc = SG_MALLOC(int32_t, num_auc);
	int32_t n=0 ;

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

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

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

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

	// create label object and attach it to svm
	CBinaryLabels* lab_auc = new CBinaryLabels(num_auc);
	lab_auc->set_int_labels(SGVector<int32_t>(labels_auc, num_auc, false));
	SG_REF(lab_auc);

	// create feature object
	CDenseFeatures<uint16_t>* f = new CDenseFeatures<uint16_t>(0);
	f->set_feature_matrix(features_auc);

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

	SG_FREE(labels_auc);

	return lab_auc;
}