OpenMS
SimpleSVM.h
Go to the documentation of this file.
1 // Copyright (c) 2002-present, OpenMS Inc. -- 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 // Remove libSVM header include
14 // #include <svm.h>
15 
16 #include <map>
17 #include <vector>
18 #include <utility> // for "pair"
19 #include <tuple>
20 #include <memory> // for std::unique_ptr
21 
22 namespace OpenMS
23 {
51  class OPENMS_DLLAPI SimpleSVM :
52  public DefaultParamHandler
53  {
54 
55  public:
57  typedef std::map<String, std::vector<double> > PredictorMap;
58 
60  typedef std::map<String, std::pair<double, double> > ScaleMap;
61 
63  struct Prediction
64  {
66  double outcome;
67 
69  std::map<double, double> probabilities;
70  };
71 
74 
76  ~SimpleSVM() override;
77 
78  // Prevent copy and assignment
79  SimpleSVM(const SimpleSVM&) = delete;
80  SimpleSVM& operator=(const SimpleSVM&) = delete;
81 
93  void setup(PredictorMap& predictors, const std::map<Size, double>& outcomes, bool classification = true);
94 
104  void predict(std::vector<Prediction>& predictions,
105  std::vector<Size> indexes = std::vector<Size>()) const;
106 
116  void predict(PredictorMap& predictors, std::vector<Prediction>& predictions) const;
117 
126  void getFeatureWeights(std::map<String, double>& feature_weights) const;
127 
129  void writeXvalResults(const String& path) const;
130 
132  const ScaleMap& getScaling() const;
133 
134  protected:
135  // Forward declaration of implementation class
136  class Impl;
137 
138  // Pointer to implementation (Pimpl pattern)
139  std::unique_ptr<Impl> pimpl_;
140  };
141 }
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:53
~SimpleSVM() override
Destructor.
SimpleSVM & operator=(const SimpleSVM &)=delete
void predict(PredictorMap &predictors, std::vector< Prediction > &predictions) const
Predict class labels or regression values (and probabilities).
std::map< double, double > probabilities
Class label (or regression value) and their predicted probabilities.
Definition: SimpleSVM.h:69
void predict(std::vector< Prediction > &predictions, std::vector< Size > indexes=std::vector< Size >()) const
Predict class labels or regression values (and probabilities).
std::unique_ptr< Impl > pimpl_
Definition: SimpleSVM.h:136
void writeXvalResults(const String &path) const
Write cross-validation (parameter optimization) results to a CSV file.
std::map< String, std::vector< double > > PredictorMap
Mapping from predictor name to vector of predictor values.
Definition: SimpleSVM.h:57
double outcome
Predicted class label (or regression value)
Definition: SimpleSVM.h:66
const ScaleMap & getScaling() const
Get data range of predictors before scaling to [0, 1].
SimpleSVM()
Default constructor.
SimpleSVM(const SimpleSVM &)=delete
std::map< String, std::pair< double, double > > ScaleMap
Mapping from predictor name to predictor min and max.
Definition: SimpleSVM.h:60
void setup(PredictorMap &predictors, const std::map< Size, double > &outcomes, bool classification=true)
Load data and train a model.
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:64
A more convenient string class.
Definition: String.h:34
Main OpenMS namespace.
Definition: openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19