using System;
namespace svmdemo
{
using netsvm;
///
/// Zusammenfassung für Class1.
///
class svmdemo
{
///
/// Der Haupteinstiegspunkt für die Anwendung.
///
[STAThread]
static void Main(string[] args)
{
Lib.minitialize();
ClassificationProblem prob = new ClassificationProblem();
int count=0;
uint NUMTRAIN = 1000;
Console.WriteLine("Generate {0} training examples for circle problem", NUMTRAIN);
Random rg = new Random( );
while (count < NUMTRAIN)
{
// random ppoint in [-2,2] x [-2,2]
float x = (float)rg.NextDouble()*4.0f - 2.0f;
float y = (float)rg.NextDouble()*4.0f - 2.0f;
float dist = x*x+y*y-1;
// point to near to decision line ?
// --> retry
if (Math.Abs(dist)<0.2f) continue;
bool label = dist > 0.0f; // point inside circle
SparseVector sv = new SparseVector();
sv.put(0,x); sv.put(1,y);
prob.push_back(sv, label);
count += 1;
}
Console.WriteLine("\nRUN CSVM TRAINING");
ClassifierCSVM csvm = new ClassifierCSVM(prob, new PolynomialKernel());
CSVMTrainInfo res = csvm.train();
Console.WriteLine("FINISHED:");
Console.WriteLine(" Number of iterations: {0}\n Number of SVs: {1}", res.numIter, res.numSV);
int ok = 0, notok = 0;
Console.WriteLine("\nTEST SVM");
for (int i=0; i<100; ++i)
{
float x = (float)rg.NextDouble()*4.0f - 2.0f;
float y = (float)rg.NextDouble()*4.0f - 2.0f;
float dist = x*x+y*y-1;
SparseVector sv = new SparseVector();
sv.put(0,x); sv.put(1,y);
float svm_label = csvm.classify(sv);
if (svm_label * dist > 0)
ok ++;
else
{
Console.WriteLine(" FALSE: {0}", sv.to_str());
notok ++;
}
}
Console.WriteLine("Test showed: {0} right classifications {1} false classifications\n", ok, notok);
Lib.mterminate();
}
}
}