Class: PCA
- Inherits:
-
Object
- Object
- PCA
- Defined in:
- lib/pca.rb
Instance Attribute Summary collapse
-
#components ⇒ Object
readonly
Returns the value of attribute components.
-
#explained_variance ⇒ Object
readonly
Returns the value of attribute explained_variance.
-
#explained_variance_ratio ⇒ Object
readonly
Returns the value of attribute explained_variance_ratio.
-
#mean ⇒ Object
readonly
Returns the value of attribute mean.
-
#singular_values ⇒ Object
readonly
Returns the value of attribute singular_values.
-
#std ⇒ Object
readonly
Returns the value of attribute std.
Instance Method Summary collapse
- #fit(x) ⇒ Object
- #fit_transform(x) ⇒ Object
-
#initialize(opts = {}) ⇒ PCA
constructor
A new instance of PCA.
- #inverse_transform(x) ⇒ Object
- #transform(x) ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ PCA
Returns a new instance of PCA.
6 7 8 9 |
# File 'lib/pca.rb', line 6 def initialize opts = {} @n_components = opts[:components] @scale_data = opts[:scale_data] end |
Instance Attribute Details
#components ⇒ Object (readonly)
Returns the value of attribute components.
4 5 6 |
# File 'lib/pca.rb', line 4 def components @components end |
#explained_variance ⇒ Object (readonly)
Returns the value of attribute explained_variance.
4 5 6 |
# File 'lib/pca.rb', line 4 def explained_variance @explained_variance end |
#explained_variance_ratio ⇒ Object (readonly)
Returns the value of attribute explained_variance_ratio.
4 5 6 |
# File 'lib/pca.rb', line 4 def explained_variance_ratio @explained_variance_ratio end |
#mean ⇒ Object (readonly)
Returns the value of attribute mean.
4 5 6 |
# File 'lib/pca.rb', line 4 def mean @mean end |
#singular_values ⇒ Object (readonly)
Returns the value of attribute singular_values.
4 5 6 |
# File 'lib/pca.rb', line 4 def singular_values @singular_values end |
#std ⇒ Object (readonly)
Returns the value of attribute std.
4 5 6 |
# File 'lib/pca.rb', line 4 def std @std end |
Instance Method Details
#fit(x) ⇒ Object
11 12 13 14 15 |
# File 'lib/pca.rb', line 11 def fit x x = prepare_data x _fit x self end |
#fit_transform(x) ⇒ Object
22 23 24 25 26 |
# File 'lib/pca.rb', line 22 def fit_transform x x = prepare_data x _fit x _transform x end |
#inverse_transform(x) ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/pca.rb', line 28 def inverse_transform x x = ensure_matrix x out = x * @components out.size2.times {|col| out.col(col).mul! @std[col] } if @scale_data out.size2.times {|col| out.col(col).add! @mean[col] } out end |
#transform(x) ⇒ Object
17 18 19 20 |
# File 'lib/pca.rb', line 17 def transform x x = prepare_data x, use_saved_mean_and_std: true _transform x end |