Module: SVMKit::PairwiseMetric
- Defined in:
- lib/svmkit/pairwise_metric.rb
Overview
Module for calculating pairwise distances, similarities, and kernels.
Class Method Summary collapse
-
.euclidean_distance(x, y = nil) ⇒ NMatrix
Calculate the pairwise euclidean distances between x and y.
-
.linear_kernel(x, y = nil) ⇒ NMatrix
Calculate the linear kernel between x and y.
-
.polynomial_kernel(x, y = nil, degree = 3, gamma = nil, coef = 1) ⇒ NMatrix
Calculate the polynomial kernel between x and y.
-
.rbf_kernel(x, y = nil, gamma = nil) ⇒ NMatrix
Calculate the rbf kernel between x and y.
-
.sigmoid_kernel(x, y = nil, gamma = nil, coef = 1) ⇒ NMatrix
Calculate the sigmoid kernel between x and y.
Class Method Details
.euclidean_distance(x, y = nil) ⇒ NMatrix
Calculate the pairwise euclidean distances between x and y.
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/svmkit/pairwise_metric.rb', line 10 def euclidean_distance(x, y = nil) y = x if y.nil? 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.repeat(y.shape[0], 1) + sum_y_vec.transpose.repeat(x.shape[0], 0) distance_matrix.abs.sqrt end |
.linear_kernel(x, y = nil) ⇒ NMatrix
Calculate the linear kernel between x and y.
39 40 41 42 |
# File 'lib/svmkit/pairwise_metric.rb', line 39 def linear_kernel(x, y = nil) y = x if y.nil? x.dot(y.transpose) end |
.polynomial_kernel(x, y = nil, degree = 3, gamma = nil, coef = 1) ⇒ NMatrix
Calculate the polynomial kernel between x and y.
52 53 54 55 56 |
# File 'lib/svmkit/pairwise_metric.rb', line 52 def polynomial_kernel(x, y = nil, degree = 3, gamma = nil, coef = 1) y = x if y.nil? gamma ||= 1.0 / x.shape[1] (x.dot(y.transpose) * gamma + coef)**degree end |
.rbf_kernel(x, y = nil, gamma = nil) ⇒ NMatrix
Calculate the rbf kernel between x and y.
27 28 29 30 31 32 |
# File 'lib/svmkit/pairwise_metric.rb', line 27 def rbf_kernel(x, y = nil, gamma = nil) y = x if y.nil? gamma ||= 1.0 / x.shape[1] distance_matrix = euclidean_distance(x, y) ((distance_matrix**2) * -gamma).exp end |
.sigmoid_kernel(x, y = nil, gamma = nil, coef = 1) ⇒ NMatrix
Calculate the sigmoid kernel between x and y.
65 66 67 68 69 |
# File 'lib/svmkit/pairwise_metric.rb', line 65 def sigmoid_kernel(x, y = nil, gamma = nil, coef = 1) y = x if y.nil? gamma ||= 1.0 / x.shape[1] (x.dot(y.transpose) * gamma + coef).tanh end |