Changeset 8660 for branches/GP-MoveOperators/HeuristicLab.Algorithms.DataAnalysis/3.4/SupportVectorMachine/SupportVectorClassification.cs
- Timestamp:
- 09/14/12 18:58:15 (12 years ago)
- Location:
- branches/GP-MoveOperators
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/GP-MoveOperators
- Property svn:ignore
-
old new 21 21 protoc.exe 22 22 _ReSharper.HeuristicLab 3.3 Tests 23 Google.ProtocolBuffers-2.4.1.473.dll
-
- Property svn:mergeinfo changed
- Property svn:ignore
-
branches/GP-MoveOperators/HeuristicLab.Algorithms.DataAnalysis/3.4
- Property svn:ignore
-
old new 5 5 *.vs10x 6 6 Plugin.cs 7 *.user
-
- Property svn:ignore
-
branches/GP-MoveOperators/HeuristicLab.Algorithms.DataAnalysis/3.4/SupportVectorMachine/SupportVectorClassification.cs
r8206 r8660 30 30 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 31 31 using HeuristicLab.Problems.DataAnalysis; 32 using LibSVM; 32 33 33 34 namespace HeuristicLab.Algorithms.DataAnalysis { … … 44 45 private const string NuParameterName = "Nu"; 45 46 private const string GammaParameterName = "Gamma"; 47 private const string DegreeParameterName = "Degree"; 46 48 47 49 #region parameter properties … … 60 62 public IValueParameter<DoubleValue> GammaParameter { 61 63 get { return (IValueParameter<DoubleValue>)Parameters[GammaParameterName]; } 64 } 65 public IValueParameter<IntValue> DegreeParameter { 66 get { return (IValueParameter<IntValue>)Parameters[DegreeParameterName]; } 62 67 } 63 68 #endregion … … 79 84 public DoubleValue Gamma { 80 85 get { return GammaParameter.Value; } 86 } 87 public IntValue Degree { 88 get { return DegreeParameter.Value; } 81 89 } 82 90 #endregion … … 103 111 Parameters.Add(new ValueParameter<DoubleValue>(CostParameterName, "The value of the C (cost) parameter of C-SVC.", new DoubleValue(1.0))); 104 112 Parameters.Add(new ValueParameter<DoubleValue>(GammaParameterName, "The value of the gamma parameter in the kernel function.", new DoubleValue(1.0))); 113 Parameters.Add(new ValueParameter<IntValue>(DegreeParameterName, "The degree parameter for the polynomial kernel function.", new IntValue(3))); 105 114 } 106 115 [StorableHook(HookType.AfterDeserialization)] 107 private void AfterDeserialization() { } 116 private void AfterDeserialization() { 117 #region backwards compatibility (change with 3.4) 118 if (!Parameters.ContainsKey(DegreeParameterName)) 119 Parameters.Add(new ValueParameter<IntValue>(DegreeParameterName, "The degree parameter for the polynomial kernel function.", new IntValue(3))); 120 #endregion 121 } 108 122 109 123 public override IDeepCloneable Clone(Cloner cloner) { … … 118 132 int nSv; 119 133 var solution = CreateSupportVectorClassificationSolution(problemData, selectedInputVariables, 120 SvmType.Value, KernelType.Value, Cost.Value, Nu.Value, Gamma.Value, 134 SvmType.Value, KernelType.Value, Cost.Value, Nu.Value, Gamma.Value, Degree.Value, 121 135 out trainingAccuracy, out testAccuracy, out nSv); 122 136 123 137 Results.Add(new Result("Support vector classification solution", "The support vector classification solution.", solution)); 124 138 Results.Add(new Result("Training accuracy", "The accuracy of the SVR solution on the training partition.", new DoubleValue(trainingAccuracy))); 125 Results.Add(new Result("Test R²", "The accuracy of the SVR solution on the test partition.", new DoubleValue(testAccuracy)));139 Results.Add(new Result("Test accuracy", "The accuracy of the SVR solution on the test partition.", new DoubleValue(testAccuracy))); 126 140 Results.Add(new Result("Number of support vectors", "The number of support vectors of the SVR solution.", new IntValue(nSv))); 127 141 } 128 142 129 143 public static SupportVectorClassificationSolution CreateSupportVectorClassificationSolution(IClassificationProblemData problemData, IEnumerable<string> allowedInputVariables, 130 string svmType, string kernelType, double cost, double nu, double gamma, 144 string svmType, string kernelType, double cost, double nu, double gamma, int degree, 131 145 out double trainingAccuracy, out double testAccuracy, out int nSv) { 132 146 Dataset dataset = problemData.Dataset; … … 135 149 136 150 //extract SVM parameters from scope and set them 137 SVM.Parameter parameter = new SVM.Parameter();138 parameter. SvmType = (SVM.SvmType)Enum.Parse(typeof(SVM.SvmType), svmType, true);139 parameter. KernelType = (SVM.KernelType)Enum.Parse(typeof(SVM.KernelType), kernelType, true);151 svm_parameter parameter = new svm_parameter(); 152 parameter.svm_type = GetSvmType(svmType); 153 parameter.kernel_type = GetKernelType(kernelType); 140 154 parameter.C = cost; 141 parameter.Nu = nu; 142 parameter.Gamma = gamma; 143 parameter.CacheSize = 500; 144 parameter.Probability = false; 145 155 parameter.nu = nu; 156 parameter.gamma = gamma; 157 parameter.cache_size = 500; 158 parameter.probability = 0; 159 parameter.eps = 0.001; 160 parameter.degree = degree; 161 parameter.shrinking = 1; 162 parameter.coef0 = 0; 163 164 165 var weightLabels = new List<int>(); 166 var weights = new List<double>(); 146 167 foreach (double c in problemData.ClassValues) { 147 168 double wSum = 0.0; … … 151 172 } 152 173 } 153 parameter.Weights.Add((int)c, wSum); 174 weightLabels.Add((int)c); 175 weights.Add(wSum); 154 176 } 155 156 157 SVM.Problem problem = SupportVectorMachineUtil.CreateSvmProblem(dataset, targetVariable, allowedInputVariables, rows); 158 SVM.RangeTransform rangeTransform = SVM.RangeTransform.Compute(problem); 159 SVM.Problem scaledProblem = SVM.Scaling.Scale(rangeTransform, problem); 160 var svmModel = SVM.Training.Train(scaledProblem, parameter); 177 parameter.weight_label = weightLabels.ToArray(); 178 parameter.weight = weights.ToArray(); 179 180 181 svm_problem problem = SupportVectorMachineUtil.CreateSvmProblem(dataset, targetVariable, allowedInputVariables, rows); 182 RangeTransform rangeTransform = RangeTransform.Compute(problem); 183 svm_problem scaledProblem = rangeTransform.Scale(problem); 184 var svmModel = svm.svm_train(scaledProblem, parameter); 161 185 var model = new SupportVectorMachineModel(svmModel, rangeTransform, targetVariable, allowedInputVariables, problemData.ClassValues); 162 186 var solution = new SupportVectorClassificationSolution(model, (IClassificationProblemData)problemData.Clone()); 163 187 164 nSv = svmModel.S upportVectorCount;188 nSv = svmModel.SV.Length; 165 189 trainingAccuracy = solution.TrainingAccuracy; 166 190 testAccuracy = solution.TestAccuracy; 167 191 168 192 return solution; 193 } 194 195 private static int GetSvmType(string svmType) { 196 if (svmType == "NU_SVC") return svm_parameter.NU_SVC; 197 if (svmType == "C_SVC") return svm_parameter.C_SVC; 198 throw new ArgumentException("Unknown SVM type"); 199 } 200 201 private static int GetKernelType(string kernelType) { 202 if (kernelType == "LINEAR") return svm_parameter.LINEAR; 203 if (kernelType == "POLY") return svm_parameter.POLY; 204 if (kernelType == "SIGMOID") return svm_parameter.SIGMOID; 205 if (kernelType == "RBF") return svm_parameter.RBF; 206 throw new ArgumentException("Unknown kernel type"); 169 207 } 170 208 #endregion
Note: See TracChangeset
for help on using the changeset viewer.