Loading [MathJax]/extensions/tex2jax.js
slope 0.29.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
cv.cpp
1#include "cv.h"
2
3namespace slope {
4
5std::vector<std::map<std::string, double>>
6createGrid(const std::map<std::string, std::vector<double>>& param_values)
7{
8 std::vector<std::map<std::string, double>> grid;
9
10 if (param_values.empty()) {
11 return grid;
12 }
13
14 // Start with first parameter
15 auto it = param_values.begin();
16 for (double value : it->second) {
17 std::map<std::string, double> point;
18 point[it->first] = value;
19 grid.push_back(point);
20 }
21
22 // Add remaining parameters
23 for (++it; it != param_values.end(); ++it) {
24 std::vector<std::map<std::string, double>> new_grid;
25 for (const auto& existing_point : grid) {
26 for (double value : it->second) {
27 auto new_point = existing_point;
28 new_point[it->first] = value;
29 new_grid.push_back(new_point);
30 }
31 }
32 grid = std::move(new_grid);
33 }
34
35 return grid;
36}
37
38void
39findBestParameters(CvResult& cv_result, const std::unique_ptr<Score>& scorer)
40{
41 cv_result.best_score = scorer->initValue();
42 auto comp = scorer->getComparator();
43
44 for (size_t i = 0; i < cv_result.results.size(); ++i) {
45 auto result = cv_result.results[i];
46 int best_alpha_ind = whichBest(result.mean_scores, comp);
47 double current_score = result.mean_scores(best_alpha_ind);
48
49 assert(result.alphas(best_alpha_ind) > 0);
50
51 if (scorer->isWorse(cv_result.best_score, current_score)) {
52 cv_result.best_ind = i;
53 cv_result.best_score = current_score;
54 cv_result.best_params = result.params;
55
56 cv_result.best_params["alpha"] = result.alphas(best_alpha_ind);
57 }
58 }
59}
60
61} // namespace slope
Cross-validation functionality for SLOPE models.
Namespace containing SLOPE regression implementation.
Definition clusters.cpp:5
void findBestParameters(CvResult &cv_result, const std::unique_ptr< Score > &scorer)
Identifies the best parameters from cross-validation results.
Definition cv.cpp:39
std::vector< std::map< std::string, double > > createGrid(const std::map< std::string, std::vector< double > > &param_values)
Creates a grid of parameter combinations from parameter value ranges.
Definition cv.cpp:6
int whichBest(const T &x, const Comparator &comp)
Returns the index of the minimum element in a container.
Definition math.h:403
Contains overall results from a cross-validation process.
Definition cv.h:58
double best_score
The score achieved by the optimal hyperparameter configuration.
Definition cv.h:68
std::map< std::string, double > best_params
Definition cv.h:65
std::vector< GridResult > results
Definition cv.h:61
int best_ind
Index of the best performing configuration in the results vector.
Definition cv.h:71