Class: SVMKit::Decomposition::PCA
- Inherits:
-
Object
- Object
- SVMKit::Decomposition::PCA
- Includes:
- Base::BaseEstimator, Base::Transformer
- Defined in:
- lib/svmkit/decomposition/pca.rb
Overview
PCA is a class that implements Principal Component Analysis.
Reference
-
Sharma and K K. Paliwal, “Fast principal component analysis using fixed-point algorithm,” Pattern Recognition Letters, 28, pp. 1151–1155, 2007.
-
Instance Attribute Summary collapse
-
#components ⇒ Numo::DFloat
readonly
Returns the principal components.
-
#mean ⇒ Numo::DFloat
readonly
Returns the mean vector.
-
#rng ⇒ Random
readonly
Return the random generator.
Attributes included from Base::BaseEstimator
Instance Method Summary collapse
-
#fit(x) ⇒ PCA
Fit the model with given training data.
-
#fit_transform(x) ⇒ Numo::DFloat
Fit the model with training data, and then transform them with the learned model.
-
#initialize(n_components: 2, max_iter: 100, tol: 1.0e-4, random_seed: nil) ⇒ PCA
constructor
Create a new transformer with PCA.
-
#inverse_transform(z) ⇒ Numo::DFloat
Inverse transform the given transformed data with the learned model.
-
#marshal_dump ⇒ Hash
Dump marshal data.
-
#marshal_load(obj) ⇒ nil
Load marshal data.
-
#transform(x) ⇒ Numo::DFloat
Transform the given data with the learned model.
Constructor Details
#initialize(n_components: 2, max_iter: 100, tol: 1.0e-4, random_seed: nil) ⇒ PCA
Create a new transformer with PCA.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/svmkit/decomposition/pca.rb', line 41 def initialize(n_components: 2, max_iter: 100, tol: 1.0e-4, random_seed: nil) check_params_integer(n_components: n_components, max_iter: max_iter) check_params_float(tol: tol) check_params_type_or_nil(Integer, random_seed: random_seed) check_params_positive(n_components: n_components, max_iter: max_iter, tol: tol) @params = {} @params[:n_components] = n_components @params[:max_iter] = max_iter @params[:tol] = tol @params[:random_seed] = random_seed @params[:random_seed] ||= srand @components = nil @mean = nil @rng = Random.new(@params[:random_seed]) end |
Instance Attribute Details
#components ⇒ Numo::DFloat (readonly)
Returns the principal components.
25 26 27 |
# File 'lib/svmkit/decomposition/pca.rb', line 25 def components @components end |
#mean ⇒ Numo::DFloat (readonly)
Returns the mean vector.
29 30 31 |
# File 'lib/svmkit/decomposition/pca.rb', line 29 def mean @mean end |
#rng ⇒ Random (readonly)
Return the random generator.
33 34 35 |
# File 'lib/svmkit/decomposition/pca.rb', line 33 def rng @rng end |
Instance Method Details
#fit(x) ⇒ PCA
Fit the model with given training data.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/svmkit/decomposition/pca.rb', line 63 def fit(x, _y = nil) check_sample_array(x) # initialize some variables. @components = nil n_samples, n_features = x.shape # centering. @mean = x.mean(0) centered_x = x - @mean # optimization. covariance_mat = centered_x.transpose.dot(centered_x) / (n_samples - 1) @params[:n_components].times do comp_vec = random_vec(n_features) @params[:max_iter].times do updated = orthogonalize(covariance_mat.dot(comp_vec)) break if (updated.dot(comp_vec) - 1).abs < @params[:tol] comp_vec = updated end @components = @components.nil? ? comp_vec : Numo::NArray.vstack([@components, comp_vec]) end self end |
#fit_transform(x) ⇒ Numo::DFloat
Fit the model with training data, and then transform them with the learned model.
91 92 93 94 |
# File 'lib/svmkit/decomposition/pca.rb', line 91 def fit_transform(x, _y = nil) check_sample_array(x) fit(x).transform(x) end |
#inverse_transform(z) ⇒ Numo::DFloat
Inverse transform the given transformed data with the learned model.
109 110 111 112 113 |
# File 'lib/svmkit/decomposition/pca.rb', line 109 def inverse_transform(z) check_sample_array(z) c = @components.shape[1].nil? ? @components.(0) : @components z.dot(c) + @mean end |
#marshal_dump ⇒ Hash
Dump marshal data.
117 118 119 120 121 122 |
# File 'lib/svmkit/decomposition/pca.rb', line 117 def marshal_dump { params: @params, components: @components, mean: @mean, rng: @rng } end |
#marshal_load(obj) ⇒ nil
Load marshal data.
126 127 128 129 130 131 132 |
# File 'lib/svmkit/decomposition/pca.rb', line 126 def marshal_load(obj) @params = obj[:params] @components = obj[:components] @mean = obj[:mean] @rng = obj[:rng] nil end |
#transform(x) ⇒ Numo::DFloat
Transform the given data with the learned model.
100 101 102 103 |
# File 'lib/svmkit/decomposition/pca.rb', line 100 def transform(x) check_sample_array(x) (x - @mean).dot(@components.transpose) end |