Class: Rumale::Preprocessing::BinDiscretizer
- Inherits:
-
Object
- Object
- Rumale::Preprocessing::BinDiscretizer
- Includes:
- Base::BaseEstimator, Base::Transformer
- Defined in:
- 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 included from Base::BaseEstimator
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.
-
#marshal_dump ⇒ Hash
Dump marshal data.
-
#marshal_load(obj) ⇒ nil
Load marshal data.
-
#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.
40 41 42 43 44 |
# File 'lib/rumale/preprocessing/bin_discretizer.rb', line 40 def initialize(n_bins: 32) @params = {} @params[:n_bins] = n_bins @feature_steps = nil end |
Instance Attribute Details
#feature_steps ⇒ Array<Numo::DFloat> (readonly)
Return the feature steps to be used discretizing.
35 36 37 |
# File 'lib/rumale/preprocessing/bin_discretizer.rb', line 35 def feature_steps @feature_steps end |
Instance Method Details
#fit(x) ⇒ BinDiscretizer
Fit feature ranges to be discretized.
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/rumale/preprocessing/bin_discretizer.rb', line 52 def fit(x, _y = nil) check_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.
69 70 71 72 |
# File 'lib/rumale/preprocessing/bin_discretizer.rb', line 69 def fit_transform(x, _y = nil) check_sample_array(x) fit(x).transform(x) end |
#marshal_dump ⇒ Hash
Dump marshal data.
94 95 96 97 |
# File 'lib/rumale/preprocessing/bin_discretizer.rb', line 94 def marshal_dump { params: @params, feature_steps: @feature_steps } end |
#marshal_load(obj) ⇒ nil
Load marshal data.
101 102 103 104 105 |
# File 'lib/rumale/preprocessing/bin_discretizer.rb', line 101 def marshal_load(obj) @params = obj[:params] @feature_steps = obj[:feature_steps] nil end |
#transform(x) ⇒ Numo::DFloat
Peform discretizing the given samples.
78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/rumale/preprocessing/bin_discretizer.rb', line 78 def transform(x) check_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 |