#include <iostream>
#include <vector>
#include <Eigen/Dense>
#include <gsl/gsl>
#include <numeric>
#include <random>
Go to the source code of this file.
|
| template<typename Matrix > |
| void | removeRow (Matrix &matrix, gsl::index rowToRemove) |
| |
| template<typename Matrix > |
| void | removeColumn (Matrix &matrix, gsl::index colToRemove) |
| |
| template<typename T > |
| void | select (std::vector< T > &result, const std::vector< T > &in, const std::vector< typename std::vector< T >::size_type > &s) |
| |
| std::tuple< std::vector< gsl::index >, std::vector< gsl::index > > | TrainTestSplit (gsl::index nrows, double testRatio=0.3) |
| |
| std::tuple< std::vector< gsl::index >, std::vector< gsl::index > > | TrainTestSplit (std::vector< gsl::index > inputIdx, double testRatio=0.3) |
| |
| template<typename T > |
| std::vector< T > | linspace (T start_in, T end_in, int num_in) |
| |
| template<typename T > |
| void | print_vector (std::vector< T > vec) |
| |
◆ linspace()
template<typename T >
| std::vector<T> linspace |
( |
T |
start_in, |
|
|
T |
end_in, |
|
|
int |
num_in |
|
) |
| |
Definition at line 81 of file utils.h.
84 std::vector<T> linspaced;
86 double start =
static_cast<double>(start_in);
87 double end =
static_cast<double>(end_in);
88 double num =
static_cast<double>(num_in);
90 if (num == 0) {
return linspaced; }
93 linspaced.push_back(start);
97 double delta = (end - start) / (num - 1);
98 for(
int i=0; i < num-1; ++i)
100 linspaced.push_back(start + delta * i);
102 linspaced.push_back(end);
◆ print_vector()
template<typename T >
| void print_vector |
( |
std::vector< T > |
vec | ) |
|
Definition at line 107 of file utils.h.
109 std::cout <<
"size: " <<
vec.size() << std::endl;
111 std::cout << d <<
" ";
112 std::cout << std::endl;
◆ removeColumn()
template<typename Matrix >
| void removeColumn |
( |
Matrix & |
matrix, |
|
|
gsl::index |
colToRemove |
|
) |
| |
Definition at line 22 of file utils.h.
24 gsl::index numRows = matrix.rows();
25 gsl::index numCols = matrix.cols()-1;
27 if( colToRemove < numCols )
28 matrix.block(0,colToRemove,numRows,numCols-colToRemove) = matrix.block(0,colToRemove+1,numRows,numCols-colToRemove).eval();
30 matrix.conservativeResize(numRows,numCols);
◆ removeRow()
template<typename Matrix >
| void removeRow |
( |
Matrix & |
matrix, |
|
|
gsl::index |
rowToRemove |
|
) |
| |
Definition at line 11 of file utils.h.
13 gsl::index numRows = matrix.rows()-1;
14 gsl::index numCols = matrix.cols();
16 if( rowToRemove < numRows )
17 matrix.block(rowToRemove,0,numRows-rowToRemove,numCols) = matrix.block(rowToRemove+1,0,numRows-rowToRemove,numCols).eval();
19 matrix.conservativeResize(numRows,numCols);
◆ select()
template<typename T >
| void select |
( |
std::vector< T > & |
result, |
|
|
const std::vector< T > & |
in, |
|
|
const std::vector< typename std::vector< T >::size_type > & |
s |
|
) |
| |
Definition at line 34 of file utils.h.
35 result.reserve(s.size());
37 [&in](
typename std::vector<T>::size_type idx) {
◆ TrainTestSplit() [1/2]
| std::tuple<std::vector<gsl::index>,std::vector<gsl::index> > TrainTestSplit |
( |
gsl::index |
nrows, |
|
|
double |
testRatio = 0.3 |
|
) |
| |
Definition at line 42 of file utils.h.
43 assert(testRatio>=0 && testRatio<=1);
44 gsl::index ntest = (gsl::index) nrows*(testRatio);
45 gsl::index ntrain = nrows - ntest;
46 std::vector<int> idx(nrows);
47 std::iota(idx.begin(), idx.end(), 0);
49 std::vector<gsl::index> trainIdx, testIdx;
50 trainIdx.resize(ntrain);
51 testIdx.resize(ntest);
52 std::sample(idx.begin(), idx.end(), std::back_inserter(testIdx),
53 ntest, std::mt19937{std::random_device{}()});
54 std::sort(testIdx.begin(),testIdx.end());
55 std::set_difference(idx.begin(), idx.end(), trainIdx.begin(), trainIdx.end(),
56 std::inserter(testIdx, testIdx.begin()));
57 return {trainIdx,testIdx};
◆ TrainTestSplit() [2/2]
| std::tuple<std::vector<gsl::index>,std::vector<gsl::index> > TrainTestSplit |
( |
std::vector< gsl::index > |
inputIdx, |
|
|
double |
testRatio = 0.3 |
|
) |
| |
Definition at line 60 of file utils.h.
61 assert(testRatio>=0 && testRatio<=1);
62 gsl::index nrows = inputIdx.size();
63 gsl::index ntest = (gsl::index) nrows*(testRatio);
64 gsl::index ntrain = nrows - ntest;
65 std::vector<int> idx(nrows);
66 std::iota(idx.begin(), idx.end(), 0);
68 std::vector<gsl::index> trainIdx, testIdx;
69 trainIdx.resize(ntrain);
70 testIdx.resize(ntest);
71 std::sample(inputIdx.begin(), inputIdx.end(), std::back_inserter(testIdx),
72 ntest, std::mt19937{std::random_device{}()});
73 std::sort(testIdx.begin(),testIdx.end());
74 std::sort(inputIdx.begin(),inputIdx.end());
75 std::set_difference(inputIdx.begin(), inputIdx.end(), trainIdx.begin(), trainIdx.end(),
76 std::inserter(testIdx, testIdx.begin()));
77 return {trainIdx,testIdx};