lib/rumale/polynomial_model/factorization_machine_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 [FactorizationMachineClassifier] 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)
        @classes = Numo::Int32[*y.to_a.uniq.sort]
        if multiclass_problem?
          n_classes = @classes.size
          n_features = x.shape[1]
          @factor_mat = Numo::DFloat.zeros(n_classes, @params[:n_factors], n_features)
          @weight_vec = Numo::DFloat.zeros(n_classes, n_features)
          @bias_term = Numo::DFloat.zeros(n_classes)
          if enable_parallel?
            models = parallel_map(n_classes) do |n|
              bin_y = Numo::Int32.cast(y.eq(@classes[n])) * 2 - 1
              partial_fit(x, bin_y)
            end
            n_classes.times { |n| @factor_mat[n, true, true], @weight_vec[n, true], @bias_term[n] = models[n] }
          else
            n_classes.times do |n|
              bin_y = Numo::Int32.cast(y.eq(@classes[n])) * 2 - 1
              @factor_mat[n, true, true], @weight_vec[n, true], @bias_term[n] = partial_fit(x, bin_y)
            end
          end
        else
          negative_label = @classes[0]
          bin_y = Numo::Int32.cast(y.ne(negative_label)) * 2 - 1
          @factor_mat, @weight_vec, @bias_term = partial_fit(x, bin_y)
        end
        self
      end
def fit ( x , y ) x = check_convert_sample_array ( x ) y = check_convert_label_array ( y ) check_sample_label_size ( x , y ) @classes = Numo :: Int32 [ * y . to_a . uniq . sort ] if multiclass_problem? n_classes = @classes . size n_features = x . shape [ 1 ] @factor_mat = Numo :: DFloat . zeros ( n_classes , @params [ :n_factors ] , n_features ) @weight_vec = Numo :: DFloat . zeros ( n_classes , n_features ) @bias_term = Numo :: DFloat . zeros ( n_classes ) if enable_parallel? models = parallel_map ( n_classes ) do | n | bin_y = Numo :: Int32 . cast ( y . eq ( @classes [ n ] ) ) * 2 - 1 partial_fit ( x , bin_y ) end n_classes . times { | n | @factor_mat [ n , true , true ] , @weight_vec [ n , true ] , @bias_term [ n ] = models [ n ] } else n_classes . times do | n | bin_y = Numo :: Int32 . cast ( y . eq ( @classes [ n ] ) ) * 2 - 1 @factor_mat [ n , true , true ] , @weight_vec [ n , true ] , @bias_term [ n ] = partial_fit ( x , bin_y ) end end else negative_label = @classes [ 0 ] bin_y = Numo :: Int32 . cast ( y . ne ( negative_label ) ) * 2 - 1 @factor_mat , @weight_vec , @bias_term = partial_fit ( x , bin_y ) end self end
Fit the model with given training data.
Fit the model with given training data .
# :nocov: # :nocov:
The learned classifier itself.
The learned classifier itself .
FactorizationMachineClassifier
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 .