Changeset 4068 for trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.LibSVM/1.6.3/LibSVM-1.6.3/Prediction.cs
- Timestamp:
- 07/22/10 00:44:01 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.LibSVM/1.6.3/LibSVM-1.6.3/Prediction.cs
r2645 r4068 19 19 20 20 using System; 21 using System.Diagnostics; 21 22 using System.IO; 22 using System.Diagnostics;23 23 24 namespace SVM 25 { 24 namespace SVM { 25 /// <summary> 26 /// Class containing the routines to perform class membership prediction using a trained SVM. 27 /// </summary> 28 public static class Prediction { 26 29 /// <summary> 27 /// Class containing the routines to perform class membership prediction using a trained SVM.30 /// Predicts the class memberships of all the vectors in the problem. 28 31 /// </summary> 29 public static class Prediction 30 { 31 /// <summary> 32 /// Predicts the class memberships of all the vectors in the problem. 33 /// </summary> 34 /// <param name="problem">The SVM Problem to solve</param> 35 /// <param name="outputFile">File for result output</param> 36 /// <param name="model">The Model to use</param> 37 /// <param name="predict_probability">Whether to output a distribution over the classes</param> 38 /// <returns>Percentage correctly labelled</returns> 39 public static double Predict( 40 Problem problem, 41 string outputFile, 42 Model model, 43 bool predict_probability) 44 { 45 int correct = 0; 46 int total = 0; 47 double error = 0; 48 double sumv = 0, sumy = 0, sumvv = 0, sumyy = 0, sumvy = 0; 49 StreamWriter output = outputFile != null ? new StreamWriter(outputFile) : null; 32 /// <param name="problem">The SVM Problem to solve</param> 33 /// <param name="outputFile">File for result output</param> 34 /// <param name="model">The Model to use</param> 35 /// <param name="predict_probability">Whether to output a distribution over the classes</param> 36 /// <returns>Percentage correctly labelled</returns> 37 public static double Predict( 38 Problem problem, 39 string outputFile, 40 Model model, 41 bool predict_probability) { 42 int correct = 0; 43 int total = 0; 44 double error = 0; 45 double sumv = 0, sumy = 0, sumvv = 0, sumyy = 0, sumvy = 0; 46 StreamWriter output = outputFile != null ? new StreamWriter(outputFile) : null; 50 47 51 52 53 54 48 SvmType svm_type = Procedures.svm_get_svm_type(model); 49 int nr_class = Procedures.svm_get_nr_class(model); 50 int[] labels = new int[nr_class]; 51 double[] prob_estimates = null; 55 52 56 if (predict_probability) 57 { 58 if (svm_type == SvmType.EPSILON_SVR || svm_type == SvmType.NU_SVR) 59 { 60 Console.WriteLine("Prob. model for test data: target value = predicted value + z,\nz: Laplace distribution e^(-|z|/sigma)/(2sigma),sigma=" + Procedures.svm_get_svr_probability(model)); 61 } 62 else 63 { 64 Procedures.svm_get_labels(model, labels); 65 prob_estimates = new double[nr_class]; 66 if (output != null) 67 { 68 output.Write("labels"); 69 for (int j = 0; j < nr_class; j++) 70 { 71 output.Write(" " + labels[j]); 72 } 73 output.Write("\n"); 74 } 75 } 53 if (predict_probability) { 54 if (svm_type == SvmType.EPSILON_SVR || svm_type == SvmType.NU_SVR) { 55 Console.WriteLine("Prob. model for test data: target value = predicted value + z,\nz: Laplace distribution e^(-|z|/sigma)/(2sigma),sigma=" + Procedures.svm_get_svr_probability(model)); 56 } else { 57 Procedures.svm_get_labels(model, labels); 58 prob_estimates = new double[nr_class]; 59 if (output != null) { 60 output.Write("labels"); 61 for (int j = 0; j < nr_class; j++) { 62 output.Write(" " + labels[j]); 76 63 } 77 for (int i = 0; i < problem.Count; i++) 78 { 79 double target = problem.Y[i]; 80 Node[] x = problem.X[i]; 64 output.Write("\n"); 65 } 66 } 67 } 68 for (int i = 0; i < problem.Count; i++) { 69 double target = problem.Y[i]; 70 Node[] x = problem.X[i]; 81 71 82 double v; 83 if (predict_probability && (svm_type == SvmType.C_SVC || svm_type == SvmType.NU_SVC)) 84 { 85 v = Procedures.svm_predict_probability(model, x, prob_estimates); 86 if (output != null) 87 { 88 output.Write(v + " "); 89 for (int j = 0; j < nr_class; j++) 90 { 91 output.Write(prob_estimates[j] + " "); 92 } 93 output.Write("\n"); 94 } 95 } 96 else 97 { 98 v = Procedures.svm_predict(model, x); 99 if(output != null) 100 output.Write(v + "\n"); 101 } 102 103 if (v == target) 104 ++correct; 105 error += (v - target) * (v - target); 106 sumv += v; 107 sumy += target; 108 sumvv += v * v; 109 sumyy += target * target; 110 sumvy += v * target; 111 ++total; 72 double v; 73 if (predict_probability && (svm_type == SvmType.C_SVC || svm_type == SvmType.NU_SVC)) { 74 v = Procedures.svm_predict_probability(model, x, prob_estimates); 75 if (output != null) { 76 output.Write(v + " "); 77 for (int j = 0; j < nr_class; j++) { 78 output.Write(prob_estimates[j] + " "); 112 79 } 113 if(output != null) 114 output.Close(); 115 return (double)correct / total; 80 output.Write("\n"); 81 } 82 } else { 83 v = Procedures.svm_predict(model, x); 84 if (output != null) 85 output.Write(v + "\n"); 116 86 } 117 87 118 /// <summary> 119 /// Predict the class for a single input vector. 120 /// </summary> 121 /// <param name="model">The Model to use for prediction</param> 122 /// <param name="x">The vector for which to predict class</param> 123 /// <returns>The result</returns> 124 public static double Predict(Model model, Node[] x) 125 { 126 return Procedures.svm_predict(model, x); 88 if (v == target) 89 ++correct; 90 error += (v - target) * (v - target); 91 sumv += v; 92 sumy += target; 93 sumvv += v * v; 94 sumyy += target * target; 95 sumvy += v * target; 96 ++total; 97 } 98 if (output != null) 99 output.Close(); 100 return (double)correct / total; 101 } 102 103 /// <summary> 104 /// Predict the class for a single input vector. 105 /// </summary> 106 /// <param name="model">The Model to use for prediction</param> 107 /// <param name="x">The vector for which to predict class</param> 108 /// <returns>The result</returns> 109 public static double Predict(Model model, Node[] x) { 110 return Procedures.svm_predict(model, x); 111 } 112 113 /// <summary> 114 /// Predicts a class distribution for the single input vector. 115 /// </summary> 116 /// <param name="model">Model to use for prediction</param> 117 /// <param name="x">The vector for which to predict the class distribution</param> 118 /// <returns>A probability distribtion over classes</returns> 119 public static double[] PredictProbability(Model model, Node[] x) { 120 SvmType svm_type = Procedures.svm_get_svm_type(model); 121 if (svm_type != SvmType.C_SVC && svm_type != SvmType.NU_SVC) 122 throw new Exception("Model type " + svm_type + " unable to predict probabilities."); 123 int nr_class = Procedures.svm_get_nr_class(model); 124 double[] probEstimates = new double[nr_class]; 125 Procedures.svm_predict_probability(model, x, probEstimates); 126 return probEstimates; 127 } 128 129 private static void exit_with_help() { 130 Debug.Write("usage: svm_predict [options] test_file model_file output_file\n" + "options:\n" + "-b probability_estimates: whether to predict probability estimates, 0 or 1 (default 0); one-class SVM not supported yet\n"); 131 Environment.Exit(1); 132 } 133 134 /// <summary> 135 /// Legacy method, provided to allow usage as though this were the command line version of libsvm. 136 /// </summary> 137 /// <param name="args">Standard arguments passed to the svm_predict exectutable. See libsvm documentation for details.</param> 138 [Obsolete("Use the other version of Predict() instead")] 139 public static void Predict(params string[] args) { 140 int i = 0; 141 bool predictProbability = false; 142 143 // parse options 144 for (i = 0; i < args.Length; i++) { 145 if (args[i][0] != '-') 146 break; 147 ++i; 148 switch (args[i - 1][1]) { 149 150 case 'b': 151 predictProbability = int.Parse(args[i]) == 1; 152 break; 153 154 default: 155 throw new ArgumentException("Unknown option"); 156 127 157 } 158 } 159 if (i >= args.Length) 160 throw new ArgumentException("No input, model and output files provided"); 128 161 129 /// <summary> 130 /// Predicts a class distribution for the single input vector. 131 /// </summary> 132 /// <param name="model">Model to use for prediction</param> 133 /// <param name="x">The vector for which to predict the class distribution</param> 134 /// <returns>A probability distribtion over classes</returns> 135 public static double[] PredictProbability(Model model, Node[] x) 136 { 137 SvmType svm_type = Procedures.svm_get_svm_type(model); 138 if (svm_type != SvmType.C_SVC && svm_type != SvmType.NU_SVC) 139 throw new Exception("Model type " + svm_type + " unable to predict probabilities."); 140 int nr_class = Procedures.svm_get_nr_class(model); 141 double[] probEstimates = new double[nr_class]; 142 Procedures.svm_predict_probability(model, x, probEstimates); 143 return probEstimates; 144 } 145 146 private static void exit_with_help() 147 { 148 Debug.Write("usage: svm_predict [options] test_file model_file output_file\n" + "options:\n" + "-b probability_estimates: whether to predict probability estimates, 0 or 1 (default 0); one-class SVM not supported yet\n"); 149 Environment.Exit(1); 150 } 151 152 /// <summary> 153 /// Legacy method, provided to allow usage as though this were the command line version of libsvm. 154 /// </summary> 155 /// <param name="args">Standard arguments passed to the svm_predict exectutable. See libsvm documentation for details.</param> 156 [Obsolete("Use the other version of Predict() instead")] 157 public static void Predict(params string[] args) 158 { 159 int i = 0; 160 bool predictProbability = false; 161 162 // parse options 163 for (i = 0; i < args.Length; i++) 164 { 165 if (args[i][0] != '-') 166 break; 167 ++i; 168 switch (args[i - 1][1]) 169 { 170 171 case 'b': 172 predictProbability = int.Parse(args[i]) == 1; 173 break; 174 175 default: 176 throw new ArgumentException("Unknown option"); 177 178 } 179 } 180 if (i >= args.Length) 181 throw new ArgumentException("No input, model and output files provided"); 182 183 Problem problem = Problem.Read(args[i]); 184 Model model = Model.Read(args[i + 1]); 185 Predict(problem, args[i + 2], model, predictProbability); 186 } 162 Problem problem = Problem.Read(args[i]); 163 Model model = Model.Read(args[i + 1]); 164 Predict(problem, args[i + 2], model, predictProbability); 187 165 } 166 } 188 167 }
Note: See TracChangeset
for help on using the changeset viewer.