lib/svmkit/linear_model/svc.rb in svmkit-0.2.7 vs lib/svmkit/linear_model/svc.rb in svmkit-0.2.8
- old
+ new
@@ -47,10 +47,15 @@
# @param batch_size [Integer] The size of the mini batches.
# @param normalize [Boolean] The flag indicating whether to normalize the weight vector.
# @param random_seed [Integer] The seed value using to initialize the random generator.
def initialize(reg_param: 1.0, fit_bias: false, bias_scale: 1.0,
max_iter: 100, batch_size: 50, normalize: true, random_seed: nil)
+ SVMKit::Validation.check_params_float(reg_param: reg_param, bias_scale: bias_scale)
+ SVMKit::Validation.check_params_integer(max_iter: max_iter, batch_size: batch_size)
+ SVMKit::Validation.check_params_boolean(fit_bias: fit_bias, normalize: normalize)
+ SVMKit::Validation.check_params_type_or_nil(Integer, random_seed: random_seed)
+
@params = {}
@params[:reg_param] = reg_param
@params[:fit_bias] = fit_bias
@params[:bias_scale] = bias_scale
@params[:max_iter] = max_iter
@@ -68,10 +73,13 @@
#
# @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 [SVC] The learned classifier itself.
def fit(x, y)
+ SVMKit::Validation.check_sample_array(x)
+ SVMKit::Validation.check_label_array(y)
+
@classes = Numo::Int32[*y.to_a.uniq.sort]
n_classes = @classes.size
_n_samples, n_features = x.shape
if n_classes > 2
@@ -95,17 +103,21 @@
# Calculate confidence scores for samples.
#
# @param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to compute the scores.
# @return [Numo::DFloat] (shape: [n_samples, n_classes]) Confidence score per sample.
def decision_function(x)
+ SVMKit::Validation.check_sample_array(x)
+
x.dot(@weight_vec.transpose) + @bias_term
end
# Predict class labels for samples.
#
# @param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to predict the labels.
# @return [Numo::Int32] (shape: [n_samples]) Predicted class label per sample.
def predict(x)
+ SVMKit::Validation.check_sample_array(x)
+
return Numo::Int32.cast(decision_function(x).ge(0.0)) * 2 - 1 if @classes.size <= 2
n_samples, = x.shape
decision_values = decision_function(x)
Numo::Int32.asarray(Array.new(n_samples) { |n| @classes[decision_values[n, true].max_index] })