Changeset 8609 for trunk/sources/HeuristicLab.Algorithms.DataAnalysis
- Timestamp:
- 09/10/12 11:02:37 (12 years ago)
- Location:
- trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/HeuristicLab.Algorithms.DataAnalysis-3.4.csproj
r8600 r8609 105 105 <Private>False</Private> 106 106 </Reference> 107 <Reference Include="LibSVM- 1.6.3, Version=1.6.3.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL">108 <HintPath>..\..\bin\LibSVM- 1.6.3.dll</HintPath>107 <Reference Include="LibSVM-3.12, Version=3.12.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL"> 108 <HintPath>..\..\bin\LibSVM-3.12.dll</HintPath> 109 109 <Private>False</Private> 110 110 </Reference> … … 361 361 --> 362 362 <PropertyGroup> 363 <PreBuildEvent Condition=" '$(OS)' == 'Windows_NT' ">set Path=%25Path%25;$(ProjectDir);$(SolutionDir)363 <PreBuildEvent Condition=" '$(OS)' == 'Windows_NT' ">set Path=%25Path%25;$(ProjectDir);$(SolutionDir) 364 364 set ProjectDir=$(ProjectDir) 365 365 set SolutionDir=$(SolutionDir) … … 368 368 call PreBuildEvent.cmd 369 369 </PreBuildEvent> 370 <PreBuildEvent Condition=" '$(OS)' != 'Windows_NT' ">370 <PreBuildEvent Condition=" '$(OS)' != 'Windows_NT' "> 371 371 export ProjectDir=$(ProjectDir) 372 372 export SolutionDir=$(SolutionDir) -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/Interfaces/ISupportVectorMachineModel.cs
r7259 r8609 20 20 #endregion 21 21 22 using HeuristicLab.Optimization;23 22 using HeuristicLab.Problems.DataAnalysis; 24 using HeuristicLab.Core; 25 using System.Collections.Generic; 23 using LibSVM; 26 24 27 25 namespace HeuristicLab.Algorithms.DataAnalysis { … … 30 28 /// </summary> 31 29 public interface ISupportVectorMachineModel : IDataAnalysisModel, IRegressionModel, IClassificationModel { 32 SVM.Model Model { get; }33 SVM.RangeTransform RangeTransform { get; }30 svm_model Model { get; } 31 RangeTransform RangeTransform { get; } 34 32 Dataset SupportVectors { get; } 35 33 } -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/Plugin.cs.frame
r8452 r8609 46 46 [PluginDependency("HeuristicLab.Problems.DataAnalysis.Symbolic.Classification", "3.4")] 47 47 [PluginDependency("HeuristicLab.Problems.DataAnalysis.Symbolic.Regression", "3.4")] 48 [PluginDependency("HeuristicLab.LibSVM", " 1.6.3")]48 [PluginDependency("HeuristicLab.LibSVM", "3.12")] 49 49 [PluginDependency("HeuristicLab.Random", "3.3")] 50 50 public class HeuristicLabAlgorithmsDataAnalysisPlugin : PluginBase { -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/SupportVectorMachine/SupportVectorClassification.cs
r8139 r8609 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 { … … 123 124 Results.Add(new Result("Support vector classification solution", "The support vector classification solution.", solution)); 124 125 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)));126 Results.Add(new Result("Test accuracy", "The accuracy of the SVR solution on the test partition.", new DoubleValue(testAccuracy))); 126 127 Results.Add(new Result("Number of support vectors", "The number of support vectors of the SVR solution.", new IntValue(nSv))); 127 128 } … … 135 136 136 137 //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);138 svm_parameter parameter = new svm_parameter(); 139 parameter.svm_type = GetSvmType(svmType); 140 parameter.kernel_type = GetKernelType(kernelType); 140 141 parameter.C = cost; 141 parameter.Nu = nu; 142 parameter.Gamma = gamma; 143 parameter.CacheSize = 500; 144 parameter.Probability = false; 142 parameter.nu = nu; 143 parameter.gamma = gamma; 144 parameter.cache_size = 500; 145 parameter.probability = 0; 146 parameter.eps = 0.001; 147 parameter.degree = 3; 148 parameter.shrinking = 1; 149 parameter.coef0 = 0; 145 150 151 152 var weightLabels = new List<int>(); 153 var weights = new List<double>(); 146 154 foreach (double c in problemData.ClassValues) { 147 155 double wSum = 0.0; … … 151 159 } 152 160 } 153 parameter.Weights.Add((int)c, wSum); 161 weightLabels.Add((int)c); 162 weights.Add(wSum); 154 163 } 164 parameter.weight_label = weightLabels.ToArray(); 165 parameter.weight = weights.ToArray(); 155 166 156 167 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);168 svm_problem problem = SupportVectorMachineUtil.CreateSvmProblem(dataset, targetVariable, allowedInputVariables, rows); 169 RangeTransform rangeTransform = RangeTransform.Compute(problem); 170 svm_problem scaledProblem = rangeTransform.Scale(problem); 171 var svmModel = svm.svm_train(scaledProblem, parameter); 161 172 var model = new SupportVectorMachineModel(svmModel, rangeTransform, targetVariable, allowedInputVariables, problemData.ClassValues); 162 173 var solution = new SupportVectorClassificationSolution(model, (IClassificationProblemData)problemData.Clone()); 163 174 164 nSv = svmModel.S upportVectorCount;175 nSv = svmModel.SV.Length; 165 176 trainingAccuracy = solution.TrainingAccuracy; 166 177 testAccuracy = solution.TestAccuracy; … … 168 179 return solution; 169 180 } 181 182 private static int GetSvmType(string svmType) { 183 if (svmType == "NU_SVC") return svm_parameter.NU_SVC; 184 if (svmType == "C_SVC") return svm_parameter.C_SVC; 185 throw new ArgumentException("Unknown SVM type"); 186 } 187 188 private static int GetKernelType(string kernelType) { 189 if (kernelType == "LINEAR") return svm_parameter.LINEAR; 190 if (kernelType == "POLY") return svm_parameter.POLY; 191 if (kernelType == "SIGMOID") return svm_parameter.SIGMOID; 192 if (kernelType == "RBF") return svm_parameter.RBF; 193 throw new ArgumentException("Unknown kernel type"); 194 } 170 195 #endregion 171 196 } -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/SupportVectorMachine/SupportVectorMachineModel.cs
r8528 r8609 29 29 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 30 30 using HeuristicLab.Problems.DataAnalysis; 31 using SVM;31 using LibSVM; 32 32 33 33 namespace HeuristicLab.Algorithms.DataAnalysis { … … 39 39 public sealed class SupportVectorMachineModel : NamedItem, ISupportVectorMachineModel { 40 40 41 private SVM.Model model;41 private svm_model model; 42 42 /// <summary> 43 43 /// Gets or sets the SVM model. 44 44 /// </summary> 45 public SVM.Model Model {45 public svm_model Model { 46 46 get { return model; } 47 47 set { … … 57 57 /// Gets or sets the range transformation for the model. 58 58 /// </summary> 59 private SVM.RangeTransform rangeTransform;60 public SVM.RangeTransform RangeTransform {59 private RangeTransform rangeTransform; 60 public RangeTransform RangeTransform { 61 61 get { return rangeTransform; } 62 62 set { … … 71 71 public Dataset SupportVectors { 72 72 get { 73 var data = new double[Model. SupportVectorCount, allowedInputVariables.Count()];74 for (int i = 0; i < Model. SupportVectorCount; i++) {75 var sv = Model.S upportVectors[i];73 var data = new double[Model.sv_coef.Length, allowedInputVariables.Count()]; 74 for (int i = 0; i < Model.sv_coef.Length; i++) { 75 var sv = Model.SV[i]; 76 76 for (int j = 0; j < sv.Length; j++) { 77 data[i, j] = sv[j]. Value;77 data[i, j] = sv[j].value; 78 78 } 79 79 } … … 101 101 this.classValues = (double[])original.classValues.Clone(); 102 102 } 103 public SupportVectorMachineModel( SVM.Model model, SVM.RangeTransform rangeTransform, string targetVariable, IEnumerable<string> allowedInputVariables, IEnumerable<double> classValues)103 public SupportVectorMachineModel(svm_model model, RangeTransform rangeTransform, string targetVariable, IEnumerable<string> allowedInputVariables, IEnumerable<double> classValues) 104 104 : this(model, rangeTransform, targetVariable, allowedInputVariables) { 105 105 this.classValues = classValues.ToArray(); 106 106 } 107 public SupportVectorMachineModel( SVM.Model model, SVM.RangeTransform rangeTransform, string targetVariable, IEnumerable<string> allowedInputVariables)107 public SupportVectorMachineModel(svm_model model, RangeTransform rangeTransform, string targetVariable, IEnumerable<string> allowedInputVariables) 108 108 : base() { 109 109 this.name = ItemName; … … 161 161 private IEnumerable<double> GetEstimatedValuesHelper(Dataset dataset, IEnumerable<int> rows) { 162 162 // calculate predictions for the currently requested rows 163 SVM.Problem problem = SupportVectorMachineUtil.CreateSvmProblem(dataset, targetVariable, allowedInputVariables, rows);164 SVM.Problem scaledProblem = SVM.Scaling.Scale(RangeTransform,problem);165 166 for (int i = 0; i < scaledProblem.Count; i++) {167 yield return SVM.Prediction.Predict(Model, scaledProblem.X[i]);163 svm_problem problem = SupportVectorMachineUtil.CreateSvmProblem(dataset, targetVariable, allowedInputVariables, rows); 164 svm_problem scaledProblem = rangeTransform.Scale(problem); 165 166 for (int i = 0; i < problem.l; i++) { 167 yield return svm.svm_predict(Model, scaledProblem.x[i]); 168 168 } 169 169 } … … 183 183 get { 184 184 using (MemoryStream stream = new MemoryStream()) { 185 SVM.Model.Write(stream, Model);185 svm.svm_save_model(new StreamWriter(stream), Model); 186 186 stream.Seek(0, System.IO.SeekOrigin.Begin); 187 187 StreamReader reader = new StreamReader(stream); … … 191 191 set { 192 192 using (MemoryStream stream = new MemoryStream(Encoding.ASCII.GetBytes(value))) { 193 model = SVM.Model.Read(stream);193 model = svm.svm_load_model(new StreamReader(stream)); 194 194 } 195 195 } … … 199 199 get { 200 200 using (MemoryStream stream = new MemoryStream()) { 201 SVM.RangeTransform.Write(stream, RangeTransform);201 RangeTransform.Write(stream, RangeTransform); 202 202 stream.Seek(0, System.IO.SeekOrigin.Begin); 203 203 StreamReader reader = new StreamReader(stream); … … 207 207 set { 208 208 using (MemoryStream stream = new MemoryStream(Encoding.ASCII.GetBytes(value))) { 209 RangeTransform = SVM.RangeTransform.Read(stream);209 RangeTransform = RangeTransform.Read(stream); 210 210 } 211 211 } -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/SupportVectorMachine/SupportVectorMachineUtil.cs
r7259 r8609 23 23 using System.Linq; 24 24 using HeuristicLab.Problems.DataAnalysis; 25 using LibSVM; 25 26 26 27 namespace HeuristicLab.Algorithms.DataAnalysis { … … 32 33 /// <param name="rowIndices">The rows of the dataset that should be contained in the resulting SVM-problem</param> 33 34 /// <returns>A problem data type that can be used to train a support vector machine.</returns> 34 public static SVM.Problem CreateSvmProblem(Dataset dataset, string targetVariable, IEnumerable<string> inputVariables, IEnumerable<int> rowIndices) {35 public static svm_problem CreateSvmProblem(Dataset dataset, string targetVariable, IEnumerable<string> inputVariables, IEnumerable<int> rowIndices) { 35 36 double[] targetVector = 36 37 dataset.GetDoubleValues(targetVariable, rowIndices).ToArray(); 37 38 38 SVM.Node[][] nodes = new SVM.Node[targetVector.Length][];39 List< SVM.Node> tempRow;39 svm_node[][] nodes = new svm_node[targetVector.Length][]; 40 List<svm_node> tempRow; 40 41 int maxNodeIndex = 0; 41 42 int svmProblemRowIndex = 0; 42 43 List<string> inputVariablesList = inputVariables.ToList(); 43 44 foreach (int row in rowIndices) { 44 tempRow = new List< SVM.Node>();45 tempRow = new List<svm_node>(); 45 46 int colIndex = 1; // make sure the smallest node index for SVM = 1 46 47 foreach (var inputVariable in inputVariablesList) { … … 49 50 // => don't add NaN values in the dataset to the sparse SVM matrix representation 50 51 if (!double.IsNaN(value)) { 51 tempRow.Add(new SVM.Node(colIndex, value)); // nodes must be sorted in ascending ordered by column index52 tempRow.Add(new svm_node() { index = colIndex, value = value }); // nodes must be sorted in ascending ordered by column index 52 53 if (colIndex > maxNodeIndex) maxNodeIndex = colIndex; 53 54 } … … 57 58 } 58 59 59 return new SVM.Problem(targetVector.Length, targetVector, nodes, maxNodeIndex);60 return new svm_problem() { l = targetVector.Length, y = targetVector, x = nodes }; 60 61 } 61 62 } -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/SupportVectorMachine/SupportVectorRegression.cs
r8139 r8609 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 { … … 143 144 144 145 //extract SVM parameters from scope and set them 145 SVM.Parameter parameter = new SVM.Parameter();146 parameter. SvmType = (SVM.SvmType)Enum.Parse(typeof(SVM.SvmType), svmType, true);147 parameter. KernelType = (SVM.KernelType)Enum.Parse(typeof(SVM.KernelType), kernelType, true);146 svm_parameter parameter = new svm_parameter(); 147 parameter.svm_type = GetSvmType(svmType); 148 parameter.kernel_type = GetKernelType(kernelType); 148 149 parameter.C = cost; 149 parameter.Nu = nu; 150 parameter.Gamma = gamma; 151 parameter.P = epsilon; 152 parameter.CacheSize = 500; 153 parameter.Probability = false; 150 parameter.nu = nu; 151 parameter.gamma = gamma; 152 parameter.p = epsilon; 153 parameter.cache_size = 500; 154 parameter.probability = 0; 155 parameter.eps = 0.001; 156 parameter.degree = 3; 157 parameter.shrinking = 1; 158 parameter.coef0 = 0; 154 159 155 160 156 SVM.Problem problem = SupportVectorMachineUtil.CreateSvmProblem(dataset, targetVariable, allowedInputVariables, rows); 157 SVM.RangeTransform rangeTransform = SVM.RangeTransform.Compute(problem); 158 SVM.Problem scaledProblem = SVM.Scaling.Scale(rangeTransform, problem); 159 var svmModel = SVM.Training.Train(scaledProblem, parameter); 160 nSv = svmModel.SupportVectorCount; 161 162 svm_problem problem = SupportVectorMachineUtil.CreateSvmProblem(dataset, targetVariable, allowedInputVariables, rows); 163 RangeTransform rangeTransform = RangeTransform.Compute(problem); 164 svm_problem scaledProblem = rangeTransform.Scale(problem); 165 var svmModel = svm.svm_train(scaledProblem, parameter); 166 nSv = svmModel.SV.Length; 161 167 var model = new SupportVectorMachineModel(svmModel, rangeTransform, targetVariable, allowedInputVariables); 162 168 var solution = new SupportVectorRegressionSolution(model, (IRegressionProblemData)problemData.Clone()); … … 165 171 return solution; 166 172 } 173 174 private static int GetSvmType(string svmType) { 175 if (svmType == "NU_SVR") return svm_parameter.NU_SVR; 176 if (svmType == "EPSILON_SVR") return svm_parameter.EPSILON_SVR; 177 throw new ArgumentException("Unknown SVM type"); 178 } 179 180 private static int GetKernelType(string kernelType) { 181 if (kernelType == "LINEAR") return svm_parameter.LINEAR; 182 if (kernelType == "POLY") return svm_parameter.POLY; 183 if (kernelType == "SIGMOID") return svm_parameter.SIGMOID; 184 if (kernelType == "RBF") return svm_parameter.RBF; 185 throw new ArgumentException("Unknown kernel type"); 186 } 167 187 #endregion 168 188 }
Note: See TracChangeset
for help on using the changeset viewer.