public static void Predict(params string[] args) { int i = 0; bool predictProbability = false; // parse options for (i = 0; i < args.Length; i++) { if (args[i][0] != '-') { break; } ++i; switch (args[i - 1][1]) { case 'b': predictProbability = int.Parse(args[i]) == 1; break; default: throw new ArgumentException("Unknown option"); } } if (i >= args.Length) { throw new ArgumentException("No input, model and output files provided"); } Problem problem = Problem.Read(args[i]); Model model = Model.Read(args[i + 1]); Predict(problem, args[i + 2], model, predictProbability); }
public static void Main(string[] args) { Problem train = Problem.Read("a1a.train.txt"); Problem test = Problem.Read("a1a.test.txt"); //For this example (and indeed, many scenarios), the default //parameters will suffice. Parameter parameters = new Parameter(); double C; double Gamma; //This will do a grid optimization to find the best parameters //and store them in C and Gamma, outputting the entire //search to params.txt. ParameterSelection.Grid(train, parameters, "params.txt", out C, out Gamma); parameters.C = C; parameters.Gamma = Gamma; //Train the model using the optimal parameters. Model model = Training.Train(train, parameters); //Perform classification on the test data, putting the //results in results.txt. Prediction.Predict(test, "results.txt", model, false); }
private static void parseCommandLine(string[] args, out Parameter parameters, out Problem problem, out bool crossValidation, out int nrfold, out string modelFilename) { int i; parameters = new Parameter(); // default values crossValidation = false; nrfold = 0; // parse options for (i = 0; i < args.Length; i++) { if (args[i][0] != '-') { break; } ++i; switch (args[i - 1][1]) { case 's': parameters.SvmType = (SvmType)int.Parse(args[i]); break; case 't': parameters.KernelType = (KernelType)int.Parse(args[i]); break; case 'd': parameters.Degree = int.Parse(args[i]); break; case 'g': parameters.Gamma = double.Parse(args[i]); break; case 'r': parameters.Coefficient0 = double.Parse(args[i]); break; case 'n': parameters.Nu = double.Parse(args[i]); break; case 'm': parameters.CacheSize = double.Parse(args[i]); break; case 'c': parameters.C = double.Parse(args[i]); break; case 'e': parameters.EPS = double.Parse(args[i]); break; case 'p': parameters.P = double.Parse(args[i]); break; case 'h': parameters.Shrinking = int.Parse(args[i]) == 1; break; case 'b': parameters.Probability = int.Parse(args[i]) == 1; break; case 'v': crossValidation = true; nrfold = int.Parse(args[i]); if (nrfold < 2) { throw new ArgumentException("n-fold cross validation: n must >= 2"); } break; case 'w': parameters.Weights[int.Parse(args[i - 1].Substring(2))] = double.Parse(args[1]); break; default: throw new ArgumentException("Unknown Parameter"); } } // determine filenames if (i >= args.Length) { throw new ArgumentException("No input file specified"); } problem = Problem.Read(args[i]); if (parameters.Gamma == 0) { parameters.Gamma = 1.0 / problem.MaxIndex; } if (i < args.Length - 1) { modelFilename = args[i + 1]; } else { int p = args[i].LastIndexOf('/') + 1; modelFilename = args[i].Substring(p) + ".model"; } }