-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
KernelPCA_unittest.cc
96 lines (73 loc) · 2.61 KB
/
KernelPCA_unittest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <shogun/preprocessor/KernelPCA.h>
#include <shogun/kernel/GaussianKernel.h>
#include <shogun/lib/SGMatrix.h>
#include <gtest/gtest.h>
using ::testing::Test;
using namespace shogun;
// Results compared with sklearn
// https://gist.github.com/micmn/f93f723b74db2a1eb5875f63d841bdc1
const index_t num_vectors = 5;
const index_t num_features = 3;
const index_t target_dim = 2;
const float64_t train_data[] = {1, 1, 1, 1, 2, 3, 5, 6, 1, 2, 2, 2, 1, 1, 1};
const float64_t test_data[] = {3, 3, 3, 7, 4, 1};
const float64_t resdata[] = {-0.17645841, 0.013962, -0.16082441, 0.03640145};
template <template <typename> class Container>
void load_data(SGMatrix<float64_t>& train, Container<float64_t>& test)
{
for (auto i = 0; i < train.size(); ++i)
train[i] = train_data[i];
for (auto i = 0; i < test.size(); ++i)
test[i] = test_data[i];
}
TEST(KernelPCA, apply_to_feature_matrix)
{
index_t num_test_vectors = 2;
SGMatrix<float64_t> train_matrix(num_features, num_vectors);
SGMatrix<float64_t> test_matrix(num_features, num_test_vectors);
load_data(train_matrix, test_matrix);
CDenseFeatures<float64_t>* train_feats =
new CDenseFeatures<float64_t>(train_matrix);
CDenseFeatures<float64_t>* test_feats =
new CDenseFeatures<float64_t>(test_matrix);
SG_REF(train_feats)
SG_REF(test_feats)
CGaussianKernel* kernel = new CGaussianKernel();
SG_REF(kernel)
kernel->set_width(1);
CKernelPCA* kpca = new CKernelPCA(kernel);
SG_REF(kpca)
kpca->set_target_dim(target_dim);
kpca->fit(train_feats);
SGMatrix<float64_t> embedding = kpca->apply_to_feature_matrix(test_feats);
// allow embedding with opposite sign
for (index_t i = 0; i < num_test_vectors * target_dim; ++i)
EXPECT_NEAR(CMath::abs(embedding[i]), CMath::abs(resdata[i]), 1E-6);
SG_UNREF(train_feats)
SG_UNREF(test_feats)
SG_UNREF(kpca);
SG_UNREF(kernel);
}
TEST(KernelPCA, apply_to_feature_vector)
{
SGMatrix<float64_t> train_matrix(num_features, num_vectors);
SGVector<float64_t> test_vector(num_features);
load_data(train_matrix, test_vector);
CDenseFeatures<float64_t>* train_feats =
new CDenseFeatures<float64_t>(train_matrix);
SG_REF(train_feats)
CGaussianKernel* kernel = new CGaussianKernel();
SG_REF(kernel)
kernel->set_width(1);
CKernelPCA* kpca = new CKernelPCA(kernel);
SG_REF(kpca)
kpca->set_target_dim(target_dim);
kpca->fit(train_feats);
SGVector<float64_t> embedding = kpca->apply_to_feature_vector(test_vector);
// allow embedding with opposite sign
for (index_t i = 0; i < target_dim; ++i)
EXPECT_NEAR(CMath::abs(embedding[i]), CMath::abs(resdata[i]), 1E-6);
SG_UNREF(train_feats)
SG_UNREF(kpca);
SG_UNREF(kernel);
}