Deep Learning Algorithm Implementations 1.0.0
C++ implementations of fundamental deep learning algorithms
Loading...
Searching...
No Matches
pca.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <cmath>
5#include <stdexcept>
6#include <vector>
7#include "../utils/matrix.hpp"
8
16namespace ml {
17 using namespace utils;
40 template<typename T>
41 class PCA {
42 public:
46 PCA() = default;
47
55 void fit(const Matrix<T> &data, bool center = true, bool scale = false);
56
64 Matrix<T> transform(const Matrix<T> &data, size_t n_components = 0) const;
65
75 Matrix<T> fit_transform(const Matrix<T> &data, size_t n_components = 0, bool center = true, bool scale = false);
76
82 std::vector<T> explained_variance_ratio() const;
83
89 Matrix<T> components() const;
90
96 std::vector<T> singular_values() const;
97
98 private:
99 Matrix<T> components_;
100 std::vector<T> singular_values_;
101 std::vector<T> explained_variance_;
102 std::vector<T> explained_variance_ratio_;
103 std::vector<T> mean_;
104 std::vector<T> scale_;
105 bool is_fitted_ = false;
106 };
107
108 // Type aliases for common use cases
111
112} // namespace ml
void fit(const Matrix< T > &data, bool center=true, bool scale=false)
Fit the PCA model to the data.
Definition pca.cpp:10
PCA()=default
Default constructor.
std::vector< T > singular_values() const
Get the singular values (square roots of eigenvalues)
Definition pca.cpp:179
Matrix< T > components() const
Get the principal components (eigenvectors)
Definition pca.cpp:171
std::vector< T > explained_variance_ratio() const
Get the explained variance ratio for each component.
Definition pca.cpp:163
Matrix< T > transform(const Matrix< T > &data, size_t n_components=0) const
Transform data to the principal component space.
Definition pca.cpp:117
Matrix< T > fit_transform(const Matrix< T > &data, size_t n_components=0, bool center=true, bool scale=false)
Fit the model and transform the data in one step.
Definition pca.cpp:157
Namespace containing traditional machine learning algorithms.
Definition kmeans.hpp:15