lib/rumale/ensemble/random_forest_classifier.rb
# Fit the model with given training data.
#
# @param x [Numo::DFloat] (shape: [n_samples, n_features]) The training data to be used for fitting the model.
# @param y [Numo::Int32] (shape: [n_samples]) The labels to be used for fitting the model.
# @return [RandomForestClassifier] The learned classifier itself.
Fit the model with given training data.
Fit the model with given training data .
def fit(x, y) 
        x = check_convert_sample_array(x)
        y = check_convert_label_array(y)
        check_sample_label_size(x, y)
        n_samples, n_features = x.shape
        @params[:max_features] = Math.sqrt(n_features).to_i if @params[:max_features].nil?
        @params[:max_features] = [[1, @params[:max_features]].max, n_features].min
        @classes = Numo::Int32.asarray(y.to_a.uniq.sort)
        sub_rng = @rng.dup
        rngs = Array.new(@params[:n_estimators]) { Random.new(sub_rng.rand(Rumale::Values.int_max)) }
        @estimators =
          if enable_parallel?
            parallel_map(@params[:n_estimators]) do |n|
              bootstrap_ids = Array.new(n_samples) { rngs[n].rand(0...n_samples) }
              plant_tree(rngs[n].rand(Rumale::Values.int_max)).fit(x[bootstrap_ids, true], y[bootstrap_ids])
            end
          else
            Array.new(@params[:n_estimators]) do |n|
              bootstrap_ids = Array.new(n_samples) { rngs[n].rand(0...n_samples) }
              plant_tree(rngs[n].rand(Rumale::Values.int_max)).fit(x[bootstrap_ids, true], y[bootstrap_ids])
            end
          end
        @feature_importances =
          if enable_parallel?
            parallel_map(@params[:n_estimators]) { |n| @estimators[n].feature_importances }.reduce(&:+)
          else
            @estimators.map(&:feature_importances).reduce(&:+)
          end
        @feature_importances /= @feature_importances.sum
        self
      end
def fit ( x , y ) x = check_convert_sample_array ( x ) y = check_convert_label_array ( y ) check_sample_label_size ( x , y ) n_samples , n_features = x . shape @params [ :max_features ] = Math . sqrt ( n_features ) . to_i if @params [ :max_features ] . nil? @params [ :max_features ] = [ [ 1 , @params [ :max_features ] ] . max , n_features ] . min @classes = Numo :: Int32 . asarray ( y . to_a . uniq . sort ) sub_rng = @rng . dup rngs = Array . new ( @params [ :n_estimators ] ) { Random . new ( sub_rng . rand ( Rumale :: Values . int_max ) ) } @estimators = if enable_parallel? parallel_map ( @params [ :n_estimators ] ) do | n | bootstrap_ids = Array . new ( n_samples ) { rngs [ n ] . rand ( 0 ... n_samples ) } plant_tree ( rngs [ n ] . rand ( Rumale :: Values . int_max ) ) . fit ( x [ bootstrap_ids , true ] , y [ bootstrap_ids ] ) end else Array . new ( @params [ :n_estimators ] ) do | n | bootstrap_ids = Array . new ( n_samples ) { rngs [ n ] . rand ( 0 ... n_samples ) } plant_tree ( rngs [ n ] . rand ( Rumale :: Values . int_max ) ) . fit ( x [ bootstrap_ids , true ] , y [ bootstrap_ids ] ) end end @feature_importances = if enable_parallel? parallel_map ( @params [ :n_estimators ] ) { | n | @estimators [ n ] . feature_importances } . reduce ( & :+ ) else @estimators . map ( & :feature_importances ) . reduce ( & :+ ) end @feature_importances /= @feature_importances . sum self end
Fit the model with given training data.
Fit the model with given training data .
# rubocop:disable Metrics/AbcSize # Initialize some variables. # Construct forest. # :nocov: # :nocov:
The learned classifier itself.
The learned classifier itself .
RandomForestClassifier
x
[n_samples, n_features]) The training data to be used for fitting the model.
[ n_samples n_features ] ) The training data to be used for fitting the model .
y
[n_samples]) The labels to be used for fitting the model.
[ n_samples ] ) The labels to be used for fitting the model .