Class: SVMKit::Preprocessing::StandardScaler

Inherits:
Object
  • Object
show all
Includes:
Base::BaseEstimator, Base::Transformer
Defined in:
lib/svmkit/preprocessing/standard_scaler.rb

Overview

Normalize samples by centering and scaling to unit variance.

Examples:

normalizer = SVMKit::Preprocessing::StandardScaler.new
new_training_samples = normalizer.fit_transform(training_samples)
new_testing_samples = normalizer.transform(testing_samples)

Instance Attribute Summary collapse

Attributes included from Base::BaseEstimator

#params

Instance Method Summary collapse

Constructor Details

#newStandardScaler

Create a new normalizer for centering and scaling to unit variance.



28
29
30
31
# File 'lib/svmkit/preprocessing/standard_scaler.rb', line 28

def initialize(_params = {})
  @mean_vec = nil
  @std_vec = nil
end

Instance Attribute Details

#mean_vecNMatrix (readonly)

Return the vector consists of the mean value for each feature.

Returns:

  • (NMatrix)

    (shape: [1, n_features])



19
20
21
# File 'lib/svmkit/preprocessing/standard_scaler.rb', line 19

def mean_vec
  @mean_vec
end

#std_vecNMatrix (readonly)

Return the vector consists of the standard deviation for each feature.

Returns:

  • (NMatrix)

    (shape: [1, n_features])



23
24
25
# File 'lib/svmkit/preprocessing/standard_scaler.rb', line 23

def std_vec
  @std_vec
end

Instance Method Details

#fit(x) ⇒ StandardScaler

Calculate the mean value and standard deviation of each feature for scaling.

Parameters:

  • x (NMatrix)

    (shape: [n_samples, n_features]) The samples to calculate the mean values and standard deviations.

Returns:



40
41
42
43
44
# File 'lib/svmkit/preprocessing/standard_scaler.rb', line 40

def fit(x, _y = nil)
  @mean_vec = x.mean(0)
  @std_vec = x.std(0)
  self
end

#fit_transform(x) ⇒ NMatrix

Calculate the mean values and standard deviations, and then normalize samples using them.

Parameters:

  • x (NMatrix)

    (shape: [n_samples, n_features]) The samples to calculate the mean values and standard deviations.

Returns:

  • (NMatrix)

    The scaled samples.



53
54
55
# File 'lib/svmkit/preprocessing/standard_scaler.rb', line 53

def fit_transform(x, _y = nil)
  fit(x).transform(x)
end

#marshal_dumpHash

Dump marshal data.

Returns:

  • (Hash)

    The marshal data about StandardScaler.



68
69
70
71
# File 'lib/svmkit/preprocessing/standard_scaler.rb', line 68

def marshal_dump
  { mean_vec: Utils.dump_nmatrix(@mean_vec),
    std_vec: Utils.dump_nmatrix(@std_vec) }
end

#marshal_load(obj) ⇒ nil

Load marshal data.

Returns:

  • (nil)


75
76
77
78
79
# File 'lib/svmkit/preprocessing/standard_scaler.rb', line 75

def marshal_load(obj)
  @mean_vec = Utils.restore_nmatrix(obj[:mean_vec])
  @std_vec = Utils.restore_nmatrix(obj[:std_vec])
  nil
end

#transform(x) ⇒ NMatrix

Perform standardization the given samples.

Parameters:

  • x (NMatrix)

    (shape: [n_samples, n_features]) The samples to be scaled.

Returns:

  • (NMatrix)

    The scaled samples.



61
62
63
64
# File 'lib/svmkit/preprocessing/standard_scaler.rb', line 61

def transform(x)
  n_samples, = x.shape
  (x - @mean_vec.repeat(n_samples, 0)) / @std_vec.repeat(n_samples, 0)
end