Class: Rumale::Preprocessing::BinDiscretizer
- Inherits:
-
Base::Estimator
- Object
- Base::Estimator
- Rumale::Preprocessing::BinDiscretizer
- Includes:
- Base::Transformer
- Defined in:
- rumale-preprocessing/lib/rumale/preprocessing/bin_discretizer.rb
Overview
Discretizes features with a given number of bins. In some cases, discretizing features may accelerate decision tree training.
Instance Attribute Summary collapse
-
#feature_steps ⇒ Array<Numo::DFloat>
readonly
Return the feature steps to be used discretizing.
Attributes inherited from Base::Estimator
Instance Method Summary collapse
-
#fit(x) ⇒ BinDiscretizer
Fit feature ranges to be discretized.
-
#fit_transform(x) ⇒ Numo::DFloat
Fit feature ranges to be discretized, then return discretized samples.
-
#initialize(n_bins: 32) ⇒ BinDiscretizer
constructor
Create a new discretizer for features with given number of bins.
-
#transform(x) ⇒ Numo::DFloat
Peform discretizing the given samples.
Constructor Details
#initialize(n_bins: 32) ⇒ BinDiscretizer
Create a new discretizer for features with given number of bins.
42 43 44 45 |
# File 'rumale-preprocessing/lib/rumale/preprocessing/bin_discretizer.rb', line 42 def initialize(n_bins: 32) super() @params = { n_bins: n_bins } end |
Instance Attribute Details
#feature_steps ⇒ Array<Numo::DFloat> (readonly)
Return the feature steps to be used discretizing.
37 38 39 |
# File 'rumale-preprocessing/lib/rumale/preprocessing/bin_discretizer.rb', line 37 def feature_steps @feature_steps end |
Instance Method Details
#fit(x) ⇒ BinDiscretizer
Fit feature ranges to be discretized.
53 54 55 56 57 58 59 60 61 62 63 |
# File 'rumale-preprocessing/lib/rumale/preprocessing/bin_discretizer.rb', line 53 def fit(x, _y = nil) x = ::Rumale::Validation.check_convert_sample_array(x) n_features = x.shape[1] max_vals = x.max(0) min_vals = x.min(0) @feature_steps = Array.new(n_features) do |n| Numo::DFloat.linspace(min_vals[n], max_vals[n], @params[:n_bins] + 1)[0...@params[:n_bins]] end self end |
#fit_transform(x) ⇒ Numo::DFloat
Fit feature ranges to be discretized, then return discretized samples.
71 72 73 74 75 |
# File 'rumale-preprocessing/lib/rumale/preprocessing/bin_discretizer.rb', line 71 def fit_transform(x, _y = nil) x = ::Rumale::Validation.check_convert_sample_array(x) fit(x).transform(x) end |
#transform(x) ⇒ Numo::DFloat
Peform discretizing the given samples.
81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'rumale-preprocessing/lib/rumale/preprocessing/bin_discretizer.rb', line 81 def transform(x) x = ::Rumale::Validation.check_convert_sample_array(x) n_samples, n_features = x.shape transformed = Numo::DFloat.zeros(n_samples, n_features) n_features.times do |n| steps = @feature_steps[n] @params[:n_bins].times do |bin| mask = x[true, n].ge(steps[bin]).where transformed[mask, n] = bin end end transformed end |