lib/svmkit/pairwise_metric.rb in svmkit-0.2.7 vs lib/svmkit/pairwise_metric.rb in svmkit-0.2.8

- old
+ new

@@ -9,10 +9,12 @@ # @param x [Numo::DFloat] (shape: [n_samples_x, n_features]) # @param y [Numo::DFloat] (shape: [n_samples_y, n_features]) # @return [Numo::DFloat] (shape: [n_samples_x, n_samples_x] or [n_samples_x, n_samples_y] if y is given) def euclidean_distance(x, y = nil) y = x if y.nil? + SVMKit::Validation.check_sample_array(x) + SVMKit::Validation.check_sample_array(y) sum_x_vec = (x**2).sum(1) sum_y_vec = (y**2).sum(1) dot_xy_mat = x.dot(y.transpose) distance_matrix = dot_xy_mat * -2.0 + sum_x_vec.tile(y.shape[0], 1).transpose + @@ -27,10 +29,13 @@ # @param gamma [Float] The parameter of rbf kernel, if nil it is 1 / n_features. # @return [Numo::DFloat] (shape: [n_samples_x, n_samples_x] or [n_samples_x, n_samples_y] if y is given) def rbf_kernel(x, y = nil, gamma = nil) y = x if y.nil? gamma ||= 1.0 / x.shape[1] + SVMKit::Validation.check_sample_array(x) + SVMKit::Validation.check_sample_array(y) + SVMKit::Validation.check_params_float(gamma: gamma) distance_matrix = euclidean_distance(x, y) Numo::NMath.exp((distance_matrix**2) * -gamma) end # Calculate the linear kernel between x and y. @@ -38,10 +43,12 @@ # @param x [Numo::DFloat] (shape: [n_samples_x, n_features]) # @param y [Numo::DFloat] (shape: [n_samples_y, n_features]) # @return [Numo::DFloat] (shape: [n_samples_x, n_samples_x] or [n_samples_x, n_samples_y] if y is given) def linear_kernel(x, y = nil) y = x if y.nil? + SVMKit::Validation.check_sample_array(x) + SVMKit::Validation.check_sample_array(y) x.dot(y.transpose) end # Calculate the polynomial kernel between x and y. # @@ -52,10 +59,14 @@ # @param coef [Integer] The parameter of polynomial kernel. # @return [Numo::DFloat] (shape: [n_samples_x, n_samples_x] or [n_samples_x, n_samples_y] if y is given) def polynomial_kernel(x, y = nil, degree = 3, gamma = nil, coef = 1) y = x if y.nil? gamma ||= 1.0 / x.shape[1] + SVMKit::Validation.check_sample_array(x) + SVMKit::Validation.check_sample_array(y) + SVMKit::Validation.check_params_float(gamma: gamma) + SVMKit::Validation.check_params_integer(degree: degree, coef: coef) (x.dot(y.transpose) * gamma + coef)**degree end # Calculate the sigmoid kernel between x and y. # @@ -65,9 +76,13 @@ # @param coef [Integer] The parameter of polynomial kernel. # @return [Numo::DFloat] (shape: [n_samples_x, n_samples_x] or [n_samples_x, n_samples_y] if y is given) def sigmoid_kernel(x, y = nil, gamma = nil, coef = 1) y = x if y.nil? gamma ||= 1.0 / x.shape[1] + SVMKit::Validation.check_sample_array(x) + SVMKit::Validation.check_sample_array(y) + SVMKit::Validation.check_params_float(gamma: gamma) + SVMKit::Validation.check_params_integer(coef: coef) Numo::NMath.tanh(x.dot(y.transpose) * gamma + coef) end end end end