lib/svmkit/linear_model/logistic_regression.rb in svmkit-0.2.7 vs lib/svmkit/linear_model/logistic_regression.rb in svmkit-0.2.8
- old
+ new
@@ -48,10 +48,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
@@ -69,26 +74,29 @@
#
# @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 [LogisticRegression] 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
@weight_vec = Numo::DFloat.zeros(n_classes, n_features)
@bias_term = Numo::DFloat.zeros(n_classes)
n_classes.times do |n|
- bin_y = Numo::Int32.cast(y.eq(@classes[n]))
+ bin_y = Numo::Int32.cast(y.eq(@classes[n])) * 2 - 1
weight, bias = binary_fit(x, bin_y)
@weight_vec[n, true] = weight
@bias_term[n] = bias
end
else
negative_label = y.to_a.uniq.sort.first
- bin_y = Numo::Int32.cast(y.ne(negative_label))
+ bin_y = Numo::Int32.cast(y.ne(negative_label)) * 2 - 1
@weight_vec, @bias_term = binary_fit(x, bin_y)
end
self
end
@@ -96,30 +104,36 @@
# 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)
- return Numo::Int32.cast(decision_function(x).ge(0.5)) * 2 - 1 if @classes.size <= 2
+ SVMKit::Validation.check_sample_array(x)
+ return Numo::Int32.cast(predict_proba(x)[true, 1].ge(0.5)) * 2 - 1 if @classes.size <= 2
+
n_samples, = x.shape
- decision_values = decision_function(x)
+ decision_values = predict_proba(x)
Numo::Int32.asarray(Array.new(n_samples) { |n| @classes[decision_values[n, true].max_index] })
end
# Predict probability for samples.
#
# @param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to predict the probailities.
# @return [Numo::DFloat] (shape: [n_samples, n_classes]) Predicted probability of each class per sample.
def predict_proba(x)
+ SVMKit::Validation.check_sample_array(x)
+
proba = 1.0 / (Numo::NMath.exp(-decision_function(x)) + 1.0)
return (proba.transpose / proba.sum(axis: 1)).transpose if @classes.size > 2
n_samples, = x.shape
probs = Numo::DFloat.zeros(n_samples, 2)
@@ -163,12 +177,12 @@
# random sampling
subset_ids = rand_ids.shift(@params[:batch_size])
rand_ids.concat(subset_ids)
# update the weight vector.
df = samples[subset_ids, true].dot(weight_vec.transpose)
- coef = bin_y[subset_ids] / (Numo::NMath.exp(-bin_y[subset_ids] * df) + 1.0)
+ coef = bin_y[subset_ids] / (Numo::NMath.exp(-bin_y[subset_ids] * df) + 1.0) - bin_y[subset_ids]
mean_vec = samples[subset_ids, true].transpose.dot(coef) / @params[:batch_size]
- weight_vec -= learning_rate(t) * (@params[:reg_param] * weight_vec - mean_vec)
+ weight_vec -= learning_rate(t) * (@params[:reg_param] * weight_vec + mean_vec)
# scale the weight vector.
normalize_weight_vec(weight_vec) if @params[:normalize]
end
split_weight_vec_bias(weight_vec)
end