user1725062
user1725062

Reputation: 11

how to load data set into libsvm in C#

I have downloaded SVM in C# from Libsvm, but I don't know where to load my data to perform classification, I would like to know how to load the data set into SVM and run it.

Upvotes: 1

Views: 3096

Answers (1)

nicolaspanel
nicolaspanel

Reputation: 973

I assume you are using libsvm.net

You have to ways to load your data :


First way - Using a file data set, called "TRAINING_FILE.txt"

Note : This method assume your file is correctly formated. See here for some good samples.

var TRAINING_FILE = "C:\\[your_local_path]\\TRAINING_FILE.txt";
var data_set = ProblemHelper.ReadAndScaleProblem(TRAINING_FILE);

If you don't want to scale, just use following instruction :

var data_set = ProblemHelper.ReadProblem(TRAINING_FILE);

Then, you finally have to create your SVM

var svm = new C_SVC(data_set, [most_appropriate_Kernel], c_parameter);

Second way - Formating the dataset yourself

Note : This method use Linq, make sure you added the System.Ling reference.

You can build your data_set line by line using the following code :

var vy = new List<double>();
var vx = new List<svm_node[]>();
foreach (var line_i in your_data_source)
{
   vy.Add(line_i.Y); // double value representing the class for the given experience

   List<svm_node> x = new List<svm_node>();
   for(int j = 0 ; j < NB_ATTRIBUTES ; j++) // Save values for each attributes
   {
      var attributeValue = line_i.X[j]; // value of the corresponding attribute
      x.Add( new svm_node() { index = j, value = attributeValue });
   }
   vx.Add(x.ToArray());
}
var data_set= new svm_problem();
data_set.l = vy.Count;
data_set.x = vx.ToArray();
data_set.y = vy.ToArray();
var svm = new C_SVC(data_set, [most_appropriate_Kernel], c_parameter);

Upvotes: 2

Related Questions