Cosan  1.0
Data Analytics Library
utils.h File Reference
#include <iostream>
#include <vector>
#include <Eigen/Dense>
#include <gsl/gsl>
#include <numeric>
#include <random>

Go to the source code of this file.

Functions

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)
 

Function Documentation

◆ linspace()

template<typename T >
std::vector<T> linspace ( start_in,
end_in,
int  num_in 
)

Definition at line 81 of file utils.h.

82 {
83 
84  std::vector<T> linspaced;
85 
86  double start = static_cast<double>(start_in);
87  double end = static_cast<double>(end_in);
88  double num = static_cast<double>(num_in);
89 
90  if (num == 0) { return linspaced; }
91  if (num == 1)
92  {
93  linspaced.push_back(start);
94  return linspaced;
95  }
96 
97  double delta = (end - start) / (num - 1);
98  for(int i=0; i < num-1; ++i)
99  {
100  linspaced.push_back(start + delta * i);
101  }
102  linspaced.push_back(end);
103  return linspaced;
104 }

◆ print_vector()

template<typename T >
void print_vector ( std::vector< T >  vec)

Definition at line 107 of file utils.h.

108 {
109  std::cout << "size: " << vec.size() << std::endl;
110  for (T & d : vec)
111  std::cout << d << " ";
112  std::cout << std::endl;
113 }

◆ removeColumn()

template<typename Matrix >
void removeColumn ( Matrix &  matrix,
gsl::index  colToRemove 
)

Definition at line 22 of file utils.h.

23 {
24  gsl::index numRows = matrix.rows();
25  gsl::index numCols = matrix.cols()-1;
26 
27  if( colToRemove < numCols )
28  matrix.block(0,colToRemove,numRows,numCols-colToRemove) = matrix.block(0,colToRemove+1,numRows,numCols-colToRemove).eval();
29 
30  matrix.conservativeResize(numRows,numCols);
31 }

◆ removeRow()

template<typename Matrix >
void removeRow ( Matrix &  matrix,
gsl::index  rowToRemove 
)

Definition at line 11 of file utils.h.

12 {
13  gsl::index numRows = matrix.rows()-1;
14  gsl::index numCols = matrix.cols();
15 
16  if( rowToRemove < numRows )
17  matrix.block(rowToRemove,0,numRows-rowToRemove,numCols) = matrix.block(rowToRemove+1,0,numRows-rowToRemove,numCols).eval();
18 
19  matrix.conservativeResize(numRows,numCols);
20 }

◆ 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.

34  {
35  result.reserve(s.size());
36  std::transform(s.begin(), s.end(), std::back_inserter(result),
37  [&in](typename std::vector<T>::size_type idx) {
38  return in.at(idx);
39  });
40 }

◆ 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.

42  {
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);
48 
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};
58 }

◆ 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.

60  {
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);
67 
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};
78 }
transform
void transform(CosanMatrix< double > func(CosanMatrix< double >))
Definition: CustomTransformTest.cpp:26
Cosan::vec
std::vector< NumericType > vec
Definition: templateTest.cpp:14