This repository was archived by the owner on Jul 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 150
Expand file tree
/
Copy pathpca.rs
More file actions
127 lines (102 loc) · 5.08 KB
/
pca.rs
File metadata and controls
127 lines (102 loc) · 5.08 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
use rm::linalg::Matrix;
use rm::learning::UnSupModel;
use rm::learning::pca::PCA;
#[test]
fn test_default() {
let mut model = PCA::default();
let inputs = Matrix::new(7, 3, vec![8.3, 50., 23.,
10.2, 55., 21.,
11.1, 57., 22.,
12.5, 60., 15.,
11.3, 59., 20.,
12.4, 61., 11.,
11.2, 58., 23.]);
model.train(&inputs).unwrap();
let cexp = Matrix::new(3, 3, vec![0.2304196717022202, 0.2504639278931734, -0.9403055863478447,
0.5897383434061588, 0.7326863014098074, 0.3396755364211204,
-0.7740254913174374, 0.6328021843757651, -0.021117155112842168]);
let cmp = model.components().unwrap();
assert_matrix_eq!(cmp, cexp, comp=abs, tol=1e-8);
let new_data = Matrix::new(1, 3, vec![9., 45., 22.]);
let outputs = model.predict(&new_data).unwrap();
let exp = Matrix::new(1, 3, vec![-9.72287413262656, -7.680227015314077, -2.301338333438487]);
assert_matrix_eq!(outputs, exp, comp=abs, tol=1e-8);
}
#[test]
fn test_not_centering() {
let mut model = PCA::new(3, false);
let inputs = Matrix::new(7, 3, vec![8.3, 50., 23.,
10.2, 55., 21.,
11.1, 57., 22.,
12.5, 60., 15.,
11.3, 59., 20.,
12.4, 61., 11.,
11.2, 58., 23.]);
model.train(&inputs).unwrap();
let cexp = Matrix::new(3, 3, vec![0.17994480617740657, -0.16908609066166264, 0.9690354795746806,
0.9326216647416523, -0.2839205184846983, -0.2227239763426676,
0.3127885822473139, 0.9438215049087068, 0.10660332868901998]);
let cmp = model.components().unwrap();
assert_matrix_eq!(cmp, cexp, comp=abs, tol=1e-8);
let new_data = Matrix::new(1, 3, vec![9., 45., 22.]);
let outputs = model.predict(&new_data).unwrap();
let exp = Matrix::new(1, 3, vec![50.468826978411926, 6.465874960225161, 1.0440136119105228]);
assert_matrix_eq!(outputs, exp, comp=abs, tol=1e-8);
}
#[test]
fn test_filter_component() {
let mut model = PCA::new(2, false);
let inputs = Matrix::new(7, 3, vec![8.3, 50., 23.,
10.2, 55., 21.,
11.1, 57., 22.,
12.5, 60., 15.,
11.3, 59., 20.,
12.4, 61., 11.,
11.2, 58., 23.]);
model.train(&inputs).unwrap();
let cexp = Matrix::new(3, 2, vec![0.17994480617740657, -0.16908609066166264,
0.9326216647416523, -0.2839205184846983,
0.3127885822473139, 0.9438215049087068]);
let cmp = model.components().unwrap();
assert_matrix_eq!(cmp, cexp, comp=abs, tol=1e-8);
let new_data = Matrix::new(1, 3, vec![9., 45., 22.]);
let outputs = model.predict(&new_data).unwrap();
let exp = Matrix::new(1, 2, vec![50.468826978411926, 6.465874960225161]);
assert_matrix_eq!(outputs, exp, comp=abs, tol=1e-8);
}
#[test]
fn test_predict_different_dimension() {
let mut model = PCA::new(2, false);
let inputs = Matrix::new(7, 3, vec![8.3, 50., 23.,
10.2, 55., 21.,
11.1, 57., 22.,
12.5, 60., 15.,
11.3, 59., 20.,
12.4, 61., 11.,
11.2, 58., 23.]);
model.train(&inputs).unwrap();
let new_data = Matrix::new(1, 2, vec![1., 2.]);
let err = model.predict(&new_data);
assert!(err.is_err());
let new_data = Matrix::new(1, 4, vec![1., 2., 3., 4.]);
let err = model.predict(&new_data);
assert!(err.is_err());
let mut model = PCA::new(5, false);
let err = model.train(&inputs);
assert!(err.is_err());
}
#[test]
fn test_wide() {
let mut model = PCA::default();
let inputs = Matrix::new(2, 4, vec![8.3, 50., 23., 2.,
10.2, 55., 21., 3.]);
model.train(&inputs).unwrap();
let cexp = Matrix::new(2, 4, vec![0.3277323746171723, 0.8624536174136117, -0.3449814469654447, 0.17249072348272235,
0.933710591152088, -0.23345540994181946, 0.23959824886246414, -0.1275765757549414]);
let cmp = model.components().unwrap();
assert_matrix_eq!(cmp, cexp, comp=abs, tol=1e-8);
let new_data = Matrix::new(1, 4, vec![9., 45., 22., 2.5]);
let outputs = model.predict(&new_data).unwrap();
let exp = Matrix::new(1, 2, vec![-6.550335224256381, 1.517487926775624]);
assert_matrix_eq!(outputs, exp, comp=abs, tol=1e-8);
}