lib/rumale/linear_model/nnls.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::DFloat] (shape: [n_samples, n_outputs]) The target values to be used for fitting the model.
# @return [NonneagtiveLeastSquare] 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)
        x = expand_feature(x) if fit_bias?
        n_features = x.shape[1]
        n_outputs = single_target?(y) ? 1 : y.shape[1]
        w_init = Rumale::Utils.rand_normal([n_outputs, n_features], @rng.dup).flatten.dup
        w_init[w_init.lt(0)] = 0
        bounds = Numo::DFloat.zeros(n_outputs * n_features, 2)
        bounds.shape[0].times { |n| bounds[n, 1] = Float::INFINITY }
        res = Lbfgsb.minimize(
          fnc: method(:nnls_fnc), jcb: true, x_init: w_init, args: [x, y, @params[:reg_param]], bounds: bounds,
          maxiter: @params[:max_iter], factr: @params[:tol] / Lbfgsb::DBL_EPSILON, verbose: @params[:verbose] ? 1 : -1
        )
        @n_iter = res[:n_iter]
        w = single_target?(y) ? res[:x] : res[:x].reshape(n_outputs, n_features).transpose
        if fit_bias?
          @weight_vec = single_target?(y) ? w[0...-1].dup : w[0...-1, true].dup
          @bias_term = single_target?(y) ? w[-1] : w[-1, true].dup
        else
          @weight_vec = w.dup
          @bias_term = single_target?(y) ? 0 : Numo::DFloat.zeros(y.shape[1])
        end
        self
      end
def fit ( x , y ) x = check_convert_sample_array ( x ) y = check_convert_tvalue_array ( y ) check_sample_tvalue_size ( x , y ) x = expand_feature ( x ) if fit_bias? n_features = x . shape [ 1 ] n_outputs = single_target? ( y ) ? 1 : y . shape [ 1 ] w_init = Rumale :: Utils . rand_normal ( [ n_outputs , n_features ] , @rng . dup ) . flatten . dup w_init [ w_init . lt ( 0 ) ] = 0 bounds = Numo :: DFloat . zeros ( n_outputs * n_features , 2 ) bounds . shape [ 0 ] . times { | n | bounds [ n , 1 ] = Float :: INFINITY } res = Lbfgsb . minimize ( fnc : method ( :nnls_fnc ) , jcb : true , x_init : w_init , args : [ x , y , @params [ :reg_param ] ] , bounds : bounds , maxiter : @params [ :max_iter ] , factr : @params [ :tol ] / Lbfgsb :: DBL_EPSILON , verbose : @params [ :verbose ] ? 1 : - 1 ) @n_iter = res [ :n_iter ] w = single_target? ( y ) ? res [ :x ] : res [ :x ] . reshape ( n_outputs , n_features ) . transpose if fit_bias? @weight_vec = single_target? ( y ) ? w [ 0 ... - 1 ] . dup : w [ 0 ... - 1 , true ] . dup @bias_term = single_target? ( y ) ? w [ - 1 ] : w [ - 1 , true ] . dup else @weight_vec = w . dup @bias_term = single_target? ( y ) ? 0 : Numo :: DFloat . zeros ( y . shape [ 1 ] ) end self end
Fit the model with given training data.
Fit the model with given training data .
The learned regressor itself.
The learned regressor itself .
NonneagtiveLeastSquare
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 values to be used for fitting the model.
[ n_samples n_outputs ] ) The target values to be used for fitting the model .