lib/rumale/ensemble/stacking_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 [StackedClassifier] 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
        @encoder = Rumale::Preprocessing::LabelEncoder.new
        y_encoded = @encoder.fit_transform(y)
        @classes = Numo::NArray[*@encoder.classes]
        @estimators.each_key { |name| @estimators[name].fit(x, y_encoded) }
        @stack_method = detect_stack_method
        @output_size = detect_output_size(n_features)
        n_components = @output_size.values.inject(:+)
        z = Numo::DFloat.zeros(n_samples, n_components)
        kf = Rumale::ModelSelection::StratifiedKFold.new(
          n_splits: @params[:n_splits], shuffle: @params[:shuffle], random_seed: @params[:random_seed]
        )
        kf.split(x, y_encoded).each do |train_ids, valid_ids|
          x_train = x[train_ids, true]
          y_train = y_encoded[train_ids]
          x_valid = x[valid_ids, true]
          f_start = 0
          @estimators.each_key do |name|
            est_fold = Marshal.load(Marshal.dump(@estimators[name]))
            f_last = f_start + @output_size[name]
            f_position = @output_size[name] == 1 ? f_start : f_start...f_last
            z[valid_ids, f_position] = est_fold.fit(x_train, y_train).public_send(@stack_method[name], x_valid)
            f_start = f_last
          end
        end
        z = Numo::NArray.hstack([z, x]) if @params[:passthrough]
        @meta_estimator.fit(z, y_encoded)
        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 @encoder = Rumale :: Preprocessing :: LabelEncoder . new y_encoded = @encoder . fit_transform ( y ) @classes = Numo :: NArray [ * @encoder . classes ] @estimators . each_key { | name | @estimators [ name ] . fit ( x , y_encoded ) } @stack_method = detect_stack_method @output_size = detect_output_size ( n_features ) n_components = @output_size . values . inject ( :+ ) z = Numo :: DFloat . zeros ( n_samples , n_components ) kf = Rumale :: ModelSelection :: StratifiedKFold . new ( n_splits : @params [ :n_splits ] , shuffle : @params [ :shuffle ] , random_seed : @params [ :random_seed ] ) kf . split ( x , y_encoded ) . each do | train_ids , valid_ids | x_train = x [ train_ids , true ] y_train = y_encoded [ train_ids ] x_valid = x [ valid_ids , true ] f_start = 0 @estimators . each_key do | name | est_fold = Marshal . load ( Marshal . dump ( @estimators [ name ] ) ) f_last = f_start + @output_size [ name ] f_position = @output_size [ name ] == 1 ? f_start : f_start ... f_last z [ valid_ids , f_position ] = est_fold . fit ( x_train , y_train ) . public_send ( @stack_method [ name ] , x_valid ) f_start = f_last end end z = Numo :: NArray . hstack ( [ z , x ] ) if @params [ :passthrough ] @meta_estimator . fit ( z , y_encoded ) self end
Fit the model with given training data.
Fit the model with given training data .
# training base classifiers with all training data. # detecting feature extraction method and its size of output for each base classifier. # extracting meta features with base classifiers. # concatenating original features. # training meta classifier.
The learned classifier itself.
The learned classifier itself .
StackedClassifier
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 .