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.



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

#componentsObject (readonly)

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 (readonly)

Returns the value of attribute mean.



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

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 (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