Class: PCA

Inherits:
Object
  • Object
show all
Defined in:
lib/pca.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ PCA

Returns a new instance of PCA.



7
8
9
10
# File 'lib/pca.rb', line 7

def initialize opts = {}
  @n_components = opts[:components]
  @scale_data = opts[:scale_data]
end

Instance Attribute Details

#componentsObject

Returns the value of attribute components.



4
5
6
# File 'lib/pca.rb', line 4

def components
  @components
end

#explained_varianceObject (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_ratioObject (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

#meanObject

Returns the value of attribute mean.



5
6
7
# File 'lib/pca.rb', line 5

def mean
  @mean
end

#singular_valuesObject (readonly)

Returns the value of attribute singular_values.



4
5
6
# File 'lib/pca.rb', line 4

def singular_values
  @singular_values
end

#stdObject

Returns the value of attribute std.



5
6
7
# File 'lib/pca.rb', line 5

def std
  @std
end

Instance Method Details

#fit(x) ⇒ Object



12
13
14
15
16
# File 'lib/pca.rb', line 12

def fit x
  x = prepare_data x
  _fit x
  self
end

#fit_transform(x) ⇒ Object



23
24
25
26
27
# File 'lib/pca.rb', line 23

def fit_transform x
  x = prepare_data x
  _fit x
  _transform x
end

#inverse_transform(x) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/pca.rb', line 29

def inverse_transform x
  x = ensure_matrix x
  xit = x * @components
  undo_scale(xit) if @scale_data
  undo_mean_normalize xit
  xit
end

#transform(x) ⇒ Object



18
19
20
21
# File 'lib/pca.rb', line 18

def transform x
  x = prepare_data x, use_saved_mean_and_std: true
  _transform x
end