Class: Rumale::Preprocessing::MinMaxScaler
- Inherits:
- 
      Base::Estimator
      
        - Object
- Base::Estimator
- Rumale::Preprocessing::MinMaxScaler
 
- Includes:
- Base::Transformer
- Defined in:
- rumale-preprocessing/lib/rumale/preprocessing/min_max_scaler.rb
Overview
Normalize samples by scaling each feature to a given range.
Instance Attribute Summary collapse
- 
  
    
      #max_vec  ⇒ Numo::DFloat 
    
    
  
  
  
  
    
      readonly
    
    
  
  
  
  
  
  
    Return the vector consists of the maximum value for each feature. 
- 
  
    
      #min_vec  ⇒ Numo::DFloat 
    
    
  
  
  
  
    
      readonly
    
    
  
  
  
  
  
  
    Return the vector consists of the minimum value for each feature. 
Attributes inherited from Base::Estimator
Instance Method Summary collapse
- 
  
    
      #fit(x)  ⇒ MinMaxScaler 
    
    
  
  
  
  
  
  
  
  
  
    Calculate the minimum and maximum value of each feature for scaling. 
- 
  
    
      #fit_transform(x)  ⇒ Numo::DFloat 
    
    
  
  
  
  
  
  
  
  
  
    Calculate the minimum and maximum values, and then normalize samples to feature_range. 
- 
  
    
      #initialize(feature_range: [0.0, 1.0])  ⇒ MinMaxScaler 
    
    
  
  
  
    constructor
  
  
  
  
  
  
  
    Creates a new normalizer for scaling each feature to a given range. 
- 
  
    
      #transform(x)  ⇒ Numo::DFloat 
    
    
  
  
  
  
  
  
  
  
  
    Perform scaling the given samples according to feature_range. 
Constructor Details
#initialize(feature_range: [0.0, 1.0]) ⇒ MinMaxScaler
Creates a new normalizer for scaling each feature to a given range.
| 32 33 34 35 | # File 'rumale-preprocessing/lib/rumale/preprocessing/min_max_scaler.rb', line 32 def initialize(feature_range: [0.0, 1.0]) super() @params = { feature_range: feature_range } end | 
Instance Attribute Details
#max_vec ⇒ Numo::DFloat (readonly)
Return the vector consists of the maximum value for each feature.
| 27 28 29 | # File 'rumale-preprocessing/lib/rumale/preprocessing/min_max_scaler.rb', line 27 def max_vec @max_vec end | 
#min_vec ⇒ Numo::DFloat (readonly)
Return the vector consists of the minimum value for each feature.
| 23 24 25 | # File 'rumale-preprocessing/lib/rumale/preprocessing/min_max_scaler.rb', line 23 def min_vec @min_vec end | 
Instance Method Details
#fit(x) ⇒ MinMaxScaler
Calculate the minimum and maximum value of each feature for scaling.
| 43 44 45 46 47 48 49 | # File 'rumale-preprocessing/lib/rumale/preprocessing/min_max_scaler.rb', line 43 def fit(x, _y = nil) x = ::Rumale::Validation.check_convert_sample_array(x) @min_vec = x.min(0) @max_vec = x.max(0) self end | 
#fit_transform(x) ⇒ Numo::DFloat
Calculate the minimum and maximum values, and then normalize samples to feature_range.
| 57 58 59 60 61 | # File 'rumale-preprocessing/lib/rumale/preprocessing/min_max_scaler.rb', line 57 def fit_transform(x, _y = nil) x = ::Rumale::Validation.check_convert_sample_array(x) fit(x).transform(x) end | 
#transform(x) ⇒ Numo::DFloat
Perform scaling the given samples according to feature_range.
| 67 68 69 70 71 72 73 74 75 | # File 'rumale-preprocessing/lib/rumale/preprocessing/min_max_scaler.rb', line 67 def transform(x) x = ::Rumale::Validation.check_convert_sample_array(x) n_samples, = x.shape dif_vec = @max_vec - @min_vec dif_vec[dif_vec.eq(0)] = 1.0 nx = (x - @min_vec.tile(n_samples, 1)) / dif_vec.tile(n_samples, 1) nx * (@params[:feature_range][1] - @params[:feature_range][0]) + @params[:feature_range][0] end |