示例#1
0
void read_data(const char * fname, SGMatrix<int32_t>& labels, SGMatrix<float64_t>& feats)
{
	// sparse data from matrix
	CLibSVMFile * svmfile = new CLibSVMFile(fname);

	SGSparseVector<float64_t>* spv_feats;
	SGVector<float64_t>* pv_labels;
	int32_t dim_feat;
	int32_t num_samples;
	int32_t num_classes;

	svmfile->get_sparse_matrix(spv_feats, dim_feat, num_samples, pv_labels, num_classes);

	SG_SPRINT("Number of the samples: %d\n", num_samples);
	SG_SPRINT("Dimention of the feature: %d\n", dim_feat+1);
	SG_SPRINT("Number of classes: %d\n", num_classes);

	feats  = SGMatrix<float64_t>(dim_feat+1, num_samples);
	labels = SGMatrix<int32_t>(num_classes, num_samples);
	feats.zero();
	labels.zero();

	for (int32_t i = 0; i < num_samples; i++)
	{
		SGVector<float64_t> v_feat = spv_feats[i].get_dense();
		SGVector<float64_t> v_labels = pv_labels[i];

		for (int32_t f = 0; f < v_feat.size(); f++)
			feats(f, i) = v_feat[f];
		
		feats(dim_feat, i) = 1.0; // bias

		for (int32_t l = 0; l < v_labels.size(); l++)
			labels((int32_t)v_labels[l], i) = 1;
	}

	SG_UNREF(svmfile);
	SG_FREE(spv_feats);
	SG_FREE(pv_labels);
}
void test_libsvmfile_multilabel(const char* fname)
{
	FILE* pfile = fopen(fname, "r");

    if (pfile == NULL)
	{
		SG_SPRINT("Unable to open file: %s\n", fname);
		return;
	}

    fclose(pfile);

	/* sparse data from matrix*/
	CLibSVMFile* svmfile = new CLibSVMFile(fname);

	SGSparseVector<float64_t>* feats;
	SGVector<float64_t>* labels;
	int32_t dim_feat;
	int32_t num_samples;
	int32_t num_classes;

	svmfile->get_sparse_matrix(feats, dim_feat, num_samples, labels, num_classes);

#ifdef SHOW_DATA
	// Display the labels
	for (int32_t i = 0; i < num_samples; i++)
	{
		labels[i].display_vector();
	}
#endif

	SG_SPRINT("Number of the samples: %d\n", num_samples);
	SG_SPRINT("Dimention of the feature: %d\n", dim_feat);
	SG_SPRINT("Number of classes: %d\n", num_classes);

	SG_UNREF(svmfile);
	SG_FREE(feats);
	SG_FREE(labels);
}