pca 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/pca.rb +20 -6
  3. metadata +4 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8dc374097222849bba01f272a8df8d019bdefad9
4
- data.tar.gz: 70af4ff18c1cb5796b54308ea43f1b202fcca8ed
3
+ metadata.gz: 437676549bb33931b4565d105e4a73d5acfc1f35
4
+ data.tar.gz: da5c93ae3bcee85fa79a7aab62dc14d6da729856
5
5
  SHA512:
6
- metadata.gz: 6c2508f2c7a86e677330736ea733e8fedb525301ae3d1c1e6ad00e95dc7be07ba50aa4a4ef5e27cde958bb0091fd0310e8d8ae44ec2cc0987928c26da7c0bd3d
7
- data.tar.gz: f708f797cdb4811680ed4a4390db9b918555fec1c8033c44356ebe64950697b6d09855cb93fec8fcb56dab3930e66a17171fcf8e0953f7518b5259f9a87bcbc2
6
+ metadata.gz: bce79847afa2af0cba8305a5eeeccaae29ae651dd9c26a9cef9154971e0680225e61801e179b769dd893544bc4b145b0fea8456cc159984ad882690665a08b13
7
+ data.tar.gz: a5b103e1813788e5235c04b7cc90cbee3141d9ec49bfb342a2ba0a7e1019307464b1ea65914b1254b70f274d9021aeb0980b7933323f0f9bf6ec4e2e163b4f2f
data/lib/pca.rb CHANGED
@@ -1,10 +1,11 @@
1
1
  require 'gsl'
2
2
 
3
3
  class PCA
4
- attr_reader :components, :singular_values, :mean, :explained_variance, :explained_variance_ratio
4
+ attr_reader :components, :singular_values, :mean, :std, :explained_variance, :explained_variance_ratio
5
5
 
6
6
  def initialize opts = {}
7
7
  @n_components = opts[:components]
8
+ @scale_data = opts[:scale_data]
8
9
  end
9
10
 
10
11
  def fit x
@@ -14,7 +15,7 @@ class PCA
14
15
  end
15
16
 
16
17
  def transform x
17
- x = prepare_data x, use_saved_mean: true
18
+ x = prepare_data x, use_saved_mean_and_std: true
18
19
  _transform x
19
20
  end
20
21
 
@@ -26,7 +27,8 @@ class PCA
26
27
 
27
28
  def inverse_transform x
28
29
  x = ensure_matrix x
29
- out = x * @components.transpose
30
+ out = x * @components
31
+ out.size2.times {|col| out.col(col).mul! @std[col] } if @scale_data
30
32
  out.size2.times {|col| out.col(col).add! @mean[col] }
31
33
  out
32
34
  end
@@ -34,22 +36,26 @@ class PCA
34
36
  private
35
37
  def prepare_data x, opts = {}
36
38
  x = ensure_matrix x
37
- @mean = calculate_mean(x) unless opts[:use_saved_mean]
39
+ @mean = calculate_mean(x) unless opts[:use_saved_mean_and_std]
38
40
  mean_normalize x
41
+ if @scale_data
42
+ @std = calculate_std(x) unless opts[:use_saved_mean_and_std]
43
+ scale(x)
44
+ end
39
45
  x
40
46
  end
41
47
 
42
48
  def _fit x
43
49
  covariance_matrix = (x.transpose * x) / x.size1
44
50
  u, v, s = covariance_matrix.SV_decomp
45
- @components = slice_n u
51
+ @components = slice_n(u).transpose
46
52
  @singular_values = slice_n s
47
53
  @explained_variance = @singular_values**2 / x.size1
48
54
  @explained_variance_ratio = @explained_variance / @explained_variance.sum
49
55
  end
50
56
 
51
57
  def _transform x
52
- x * @components
58
+ x * @components.transpose
53
59
  end
54
60
 
55
61
  def ensure_matrix x
@@ -71,6 +77,14 @@ class PCA
71
77
  x.size2.times {|col| x.col(col).sub! @mean[col] }
72
78
  end
73
79
 
80
+ def calculate_std x
81
+ x.size2.times.map {|col| x.col(col).sd }
82
+ end
83
+
84
+ def scale x
85
+ x.size2.times {|col| x.col(col).div! @std[col] }
86
+ end
87
+
74
88
  def slice_n x
75
89
  return x unless @n_components
76
90
  case x
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pca
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Geoff Buesing
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-31 00:00:00.000000000 Z
11
+ date: 2015-04-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rb-gsl
@@ -24,7 +24,7 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
- description: Principal Component Analysis
27
+ description: Principal Component Analysis (PCA). Uses GSL for calculations.
28
28
  email: gbuesing@gmail.com
29
29
  executables: []
30
30
  extensions: []
@@ -54,6 +54,6 @@ rubyforge_project:
54
54
  rubygems_version: 2.4.5
55
55
  signing_key:
56
56
  specification_version: 4
57
- summary: Principal Component Analysis
57
+ summary: Principal Component Analysis (PCA)
58
58
  test_files: []
59
59
  has_rdoc: