OpenMS
SimpleSVM.h
Go to the documentation of this file.
1 // Copyright (c) 2002-2023, The OpenMS Team -- EKU Tuebingen, ETH Zurich, and FU Berlin
2 // SPDX-License-Identifier: BSD-3-Clause
3 //
4 // --------------------------------------------------------------------------
5 // $Maintainer: Hendrik Weisser $
6 // $Authors: Hendrik Weisser $
7 // --------------------------------------------------------------------------
8 
9 #pragma once
10 
12 
13 #include <svm.h>
14 
15 #include <map>
16 #include <vector>
17 #include <utility> // for "pair"
18 #include <tuple>
19 
20 namespace OpenMS
21 {
49  class OPENMS_DLLAPI SimpleSVM :
50  public DefaultParamHandler
51  {
52 
53  public:
55  typedef std::map<String, std::vector<double> > PredictorMap;
56 
58  typedef std::map<String, std::pair<double, double> > ScaleMap;
59 
61  struct Prediction
62  {
64  double outcome;
65 
67  std::map<double, double> probabilities;
68  };
69 
72 
74  ~SimpleSVM() override;
75 
87  void setup(PredictorMap& predictors, const std::map<Size, double>& outcomes, bool classification = true);
88 
98  void predict(std::vector<Prediction>& predictions,
99  std::vector<Size> indexes = std::vector<Size>()) const;
100 
110  void predict(PredictorMap& predictors, std::vector<Prediction>& predictions) const;
111 
120  void getFeatureWeights(std::map<String, double>& feature_weights) const;
121 
123  void writeXvalResults(const String& path) const;
124 
126  const ScaleMap& getScaling() const;
127 
128  protected:
129  void clear_();
130 
132  typedef std::vector<std::vector<std::vector<double>>> SVMPerformance;
133 
135  std::vector<std::vector<struct svm_node> > nodes_;
136 
138  struct svm_problem data_;
139 
141  struct svm_parameter svm_params_;
142 
144  struct svm_model* model_;
145 
147  std::vector<String> predictor_names_;
148 
151 
153  std::vector<double> log2_C_, log2_gamma_, log2_p_;
154 
157 
160 
162  static void printNull_(const char*) {}
163 
165  void scaleData_(PredictorMap& predictors);
166 
168  void convertData_(const PredictorMap& predictors);
169 
171  std::tuple<double, double, double> chooseBestParameters_(bool higher_better) const;
172 
174  void optimizeParameters_(bool classification);
175  };
176 }
A base class for all classes handling default parameters.
Definition: DefaultParamHandler.h:66
Simple interface to support vector machines for classification and regression (via LIBSVM).
Definition: SimpleSVM.h:51
void convertData_(const PredictorMap &predictors)
Convert predictors to LIBSVM format.
void optimizeParameters_(bool classification)
Run cross-validation to optimize SVM parameters.
~SimpleSVM() override
Destructor.
static void printNull_(const char *)
Dummy function to suppress LIBSVM output.
Definition: SimpleSVM.h:162
void predict(PredictorMap &predictors, std::vector< Prediction > &predictions) const
Predict class labels or regression values (and probabilities).
Size n_parts_
Number of partitions for cross-validation.
Definition: SimpleSVM.h:150
struct svm_model * model_
Pointer to SVM model (LIBSVM format)
Definition: SimpleSVM.h:144
std::vector< std::vector< std::vector< double > > > SVMPerformance
Classification (or regression) performance for different param. combinations (C/gamma/p):
Definition: SimpleSVM.h:132
std::map< double, double > probabilities
Class label (or regression value) and their predicted probabilities.
Definition: SimpleSVM.h:67
void predict(std::vector< Prediction > &predictions, std::vector< Size > indexes=std::vector< Size >()) const
Predict class labels or regression values (and probabilities).
void scaleData_(PredictorMap &predictors)
Scale predictor values to range 0-1.
void writeXvalResults(const String &path) const
Write cross-validation (parameter optimization) results to a CSV file.
std::tuple< double, double, double > chooseBestParameters_(bool higher_better) const
Choose best SVM parameters based on cross-validation results.
std::map< String, std::vector< double > > PredictorMap
Mapping from predictor name to vector of predictor values.
Definition: SimpleSVM.h:55
double outcome
Predicted class label (or regression value)
Definition: SimpleSVM.h:64
const ScaleMap & getScaling() const
Get data range of predictors before scaling to [0, 1].
SVMPerformance performance_
Cross-validation results.
Definition: SimpleSVM.h:159
std::vector< double > log2_C_
Parameter values to try during optimization.
Definition: SimpleSVM.h:153
SimpleSVM()
Default constructor.
std::map< String, std::pair< double, double > > ScaleMap
Mapping from predictor name to predictor min and max.
Definition: SimpleSVM.h:58
std::vector< String > predictor_names_
Names of predictors in the model (excluding uninformative ones)
Definition: SimpleSVM.h:147
void setup(PredictorMap &predictors, const std::map< Size, double > &outcomes, bool classification=true)
Load data and train a model.
ScaleMap scaling_
Mapping from predictor name to predictor min and max.
Definition: SimpleSVM.h:156
void getFeatureWeights(std::map< String, double > &feature_weights) const
Get the weights used for features (predictors) in the SVM model.
SVM/SVR prediction result.
Definition: SimpleSVM.h:62
A more convenient string class.
Definition: String.h:34
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:101
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:22