lib/rumale/ensemble/stacking_regressor.rb
def fit(x, y)
        x = check_convert_sample_array(x)
        y = check_convert_tvalue_array(y)
        check_sample_tvalue_size(x, y)

        n_samples, n_features = x.shape
        n_outputs = y.ndim == 1 ? 1 : y.shape[1]

        # training base regressors with all training data.
        @estimators.each_key { |name| @estimators[name].fit(x, y) }

        # detecting size of output for each base regressor.
        @output_size = detect_output_size(n_features)

        # extracting meta features with base regressors.
        n_components = @output_size.values.inject(:+)
        z = Numo::DFloat.zeros(n_samples, n_components)

        kf = Rumale::ModelSelection::KFold.new(
          n_splits: @params[:n_splits], shuffle: @params[:shuffle], random_seed: @params[:random_seed]
        )

        kf.split(x, y).each do |train_ids, valid_ids|
          x_train = x[train_ids, true]
          y_train = n_outputs == 1 ? y[train_ids] : y[train_ids, true]
          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).predict(x_valid)
            f_start = f_last
          end
        end

        # concatenating original features.
        z = Numo::NArray.hstack([z, x]) if @params[:passthrough]

        # training meta regressor.
        @meta_estimator.fit(z, y)

        self
      end
# 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::DFloat] (shape: [n_samples, n_outputs]) The target variables to be used for fitting the model.
# @return [StackedRegressor] The learned regressor 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_tvalue_array(y)
        check_sample_tvalue_size(x, y)
        n_samples, n_features = x.shape
        n_outputs = y.ndim == 1 ? 1 : y.shape[1]
        @estimators.each_key { |name| @estimators[name].fit(x, y) }
        @output_size = detect_output_size(n_features)
        n_components = @output_size.values.inject(:+)
        z = Numo::DFloat.zeros(n_samples, n_components)
        kf = Rumale::ModelSelection::KFold.new(
          n_splits: @params[:n_splits], shuffle: @params[:shuffle], random_seed: @params[:random_seed]
        )
        kf.split(x, y).each do |train_ids, valid_ids|
          x_train = x[train_ids, true]
          y_train = n_outputs == 1 ? y[train_ids] : y[train_ids, true]
          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).predict(x_valid)
            f_start = f_last
          end
        end
        z = Numo::NArray.hstack([z, x]) if @params[:passthrough]
        @meta_estimator.fit(z, y)
        self
      end
def fit ( x , y ) x = check_convert_sample_array ( x ) y = check_convert_tvalue_array ( y ) check_sample_tvalue_size ( x , y ) n_samples , n_features = x . shape n_outputs = y . ndim == 1 ? 1 : y . shape [ 1 ] @estimators . each_key { | name | @estimators [ name ] . fit ( x , y ) } @output_size = detect_output_size ( n_features ) n_components = @output_size . values . inject ( :+ ) z = Numo :: DFloat . zeros ( n_samples , n_components ) kf = Rumale :: ModelSelection :: KFold . new ( n_splits : @params [ :n_splits ] , shuffle : @params [ :shuffle ] , random_seed : @params [ :random_seed ] ) kf . split ( x , y ) . each do | train_ids , valid_ids | x_train = x [ train_ids , true ] y_train = n_outputs == 1 ? y [ train_ids ] : y [ train_ids , true ] 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 ) . predict ( x_valid ) f_start = f_last end end z = Numo :: NArray . hstack ( [ z , x ] ) if @params [ :passthrough ] @meta_estimator . fit ( z , y ) self end
Fit the model with given training data.
Fit the model with given training data .
# training base regressors with all training data. # detecting size of output for each base regressor. # extracting meta features with base regressors. # concatenating original features. # training meta regressor.
The learned regressor itself.
The learned regressor itself .
StackedRegressor
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, n_outputs]) The target variables to be used for fitting the model.
[ n_samples n_outputs ] ) The target variables to be used for fitting the model .