philsupertramp/game-math
Loading...
Searching...
No Matches
PCA.h
Go to the documentation of this file.
1#pragma once
2
3#include "../../numerics/lin_alg/svd.h"
4#include "../Predictor.h"
5
6class PCA : public Transformer
7{
8public:
12
13public:
14 PCA(int k_components = 0)
15 : Transformer()
16 , keep_components(k_components) { }
17
18 void fit(const Matrix<double>& X, const Matrix<double>& y) override {
19 auto res = svd(X, keep_components);
20 PCs = res[0] * res[1];
21 right = res[2];
22 }
23 Matrix<double> transform(const Matrix<double>& in) override { return in * PCs; }
24};
Definition: Matrix.h:42
Definition: PCA.h:7
Matrix< double > PCs
Definition: PCA.h:9
int keep_components
Definition: PCA.h:11
void fit(const Matrix< double > &X, const Matrix< double > &y) override
Definition: PCA.h:18
PCA(int k_components=0)
Definition: PCA.h:14
Matrix< double > right
Definition: PCA.h:10
Matrix< double > transform(const Matrix< double > &in) override
Definition: PCA.h:23
Definition: Predictor.h:30
std::vector< Matrix< double > > svd(const Matrix< double > &A, const size_t &k, const double epsilon=0.1e-4)
Definition: svd.h:14