pmlpp/pca/pca.cpp

146 lines
5.1 KiB
C++
Raw Normal View History

2023-12-30 00:41:59 +01:00
/*************************************************************************/
/* pca.cpp */
/*************************************************************************/
/* This file is part of: */
/* PMLPP Machine Learning Library */
/* https://github.com/Relintai/pmlpp */
/*************************************************************************/
2023-12-30 00:43:39 +01:00
/* Copyright (c) 2023-present Péter Magyar. */
2023-12-30 00:41:59 +01:00
/* Copyright (c) 2022-2023 Marc Melikyan */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
2023-01-24 18:12:23 +01:00
#include "pca.h"
#include "../data/data.h"
2023-02-08 01:26:37 +01:00
Ref<MLPPMatrix> MLPPPCA::get_input_set() {
return _input_set;
}
void MLPPPCA::set_input_set(const Ref<MLPPMatrix> &val) {
_input_set = val;
}
int MLPPPCA::get_k() {
return _k;
}
void MLPPPCA::set_k(const int val) {
_k = val;
}
Ref<MLPPMatrix> MLPPPCA::principal_components() {
ERR_FAIL_COND_V(!_input_set.is_valid() || _k == 0, Ref<MLPPMatrix>());
2023-01-25 00:21:31 +01:00
MLPPData data;
2023-01-24 19:00:54 +01:00
2023-04-29 20:10:49 +02:00
MLPPMatrix::SVDResult svr_res = _input_set->cov()->svd();
2023-02-08 01:26:37 +01:00
_x_normalized = data.mean_centering(_input_set);
Size2i svr_res_u_size = svr_res.U->size();
_u_reduce->resize(Size2i(_k, svr_res_u_size.y));
for (int i = 0; i < _k; ++i) {
for (int j = 0; j < svr_res_u_size.y; ++j) {
_u_reduce->element_set(j, i, svr_res.U->element_get(j, i));
2023-01-24 19:00:54 +01:00
}
}
2023-02-08 01:26:37 +01:00
2023-04-29 20:10:49 +02:00
_z = _u_reduce->transposen()->multn(_x_normalized);
2023-02-08 01:26:37 +01:00
return _z;
2023-01-24 19:00:54 +01:00
}
2023-02-08 01:26:37 +01:00
2023-01-24 19:00:54 +01:00
// Simply tells us the percentage of variance maintained.
2023-01-27 13:01:16 +01:00
real_t MLPPPCA::score() {
2023-02-08 01:26:37 +01:00
ERR_FAIL_COND_V(!_input_set.is_valid() || _k == 0, 0);
2023-04-29 20:10:49 +02:00
Ref<MLPPMatrix> x_approx = _u_reduce->multn(_z);
2023-02-08 01:26:37 +01:00
real_t num = 0;
real_t den = 0;
Size2i x_normalized_size = _x_normalized->size();
int x_normalized_size_y = x_normalized_size.y;
Ref<MLPPVector> x_approx_row_tmp;
x_approx_row_tmp.instance();
x_approx_row_tmp->resize(x_approx->size().x);
Ref<MLPPVector> x_normalized_row_tmp;
x_normalized_row_tmp.instance();
x_normalized_row_tmp->resize(x_normalized_size.x);
for (int i = 0; i < x_normalized_size_y; ++i) {
2023-04-29 15:07:30 +02:00
_x_normalized->row_get_into_mlpp_vector(i, x_normalized_row_tmp);
x_approx->row_get_into_mlpp_vector(i, x_approx_row_tmp);
2023-02-08 01:26:37 +01:00
2023-04-29 20:10:49 +02:00
num += x_normalized_row_tmp->subn(x_approx_row_tmp)->norm_sq();
2023-01-24 19:00:54 +01:00
}
2023-02-08 01:26:37 +01:00
num /= x_normalized_size_y;
for (int i = 0; i < x_normalized_size_y; ++i) {
2023-04-29 15:07:30 +02:00
_x_normalized->row_get_into_mlpp_vector(i, x_normalized_row_tmp);
2023-02-08 01:26:37 +01:00
2023-04-29 20:10:49 +02:00
den += x_normalized_row_tmp->norm_sq();
2023-01-24 19:00:54 +01:00
}
2023-02-08 01:26:37 +01:00
den /= x_normalized_size_y;
2023-01-24 19:00:54 +01:00
if (den == 0) {
den += 1e-10; // For numerical sanity as to not recieve a domain error
}
2023-02-08 01:26:37 +01:00
2023-01-24 19:00:54 +01:00
return 1 - num / den;
}
2023-01-24 19:20:18 +01:00
2023-02-08 01:26:37 +01:00
MLPPPCA::MLPPPCA(const Ref<MLPPMatrix> &p_input_set, int p_k) {
_k = p_k;
_input_set = p_input_set;
_x_normalized.instance();
_u_reduce.instance();
_z.instance();
}
MLPPPCA::MLPPPCA() {
_k = 0;
_x_normalized.instance();
_u_reduce.instance();
_z.instance();
}
MLPPPCA::~MLPPPCA() {
}
void MLPPPCA::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_input_set"), &MLPPPCA::get_input_set);
ClassDB::bind_method(D_METHOD("set_input_set", "val"), &MLPPPCA::set_input_set);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "get_input_set", PROPERTY_HINT_RESOURCE_TYPE, "MLPPMatrix"), "set_input_set", "get_input_set");
ClassDB::bind_method(D_METHOD("get_k"), &MLPPPCA::get_k);
ClassDB::bind_method(D_METHOD("set_k", "val"), &MLPPPCA::set_k);
ADD_PROPERTY(PropertyInfo(Variant::INT, "k"), "set_k", "get_k");
ClassDB::bind_method(D_METHOD("principal_components"), &MLPPPCA::principal_components);
ClassDB::bind_method(D_METHOD("score"), &MLPPPCA::score);
}