Module: Rumale::PairwiseMetric
- Defined in:
- rumale-core/lib/rumale/pairwise_metric.rb
Overview
Module for calculating pairwise distances, similarities, and kernels.
Class Method Summary collapse
-
.cosine_distance(x, y = nil) ⇒ Numo::DFloat
Calculate the pairwise cosine distances between x and y.
-
.cosine_similarity(x, y = nil) ⇒ Numo::DFloat
Calculate the pairwise cosine simlarities between x and y.
-
.euclidean_distance(x, y = nil) ⇒ Numo::DFloat
Calculate the pairwise euclidean distances between x and y.
-
.linear_kernel(x, y = nil) ⇒ Numo::DFloat
Calculate the linear kernel between x and y.
-
.manhattan_distance(x, y = nil) ⇒ Numo::DFloat
Calculate the pairwise manhattan distances between x and y.
-
.polynomial_kernel(x, y = nil, degree = 3, gamma = nil, coef = 1) ⇒ Numo::DFloat
Calculate the polynomial kernel between x and y.
-
.rbf_kernel(x, y = nil, gamma = nil) ⇒ Numo::DFloat
Calculate the rbf kernel between x and y.
-
.sigmoid_kernel(x, y = nil, gamma = nil, coef = 1) ⇒ Numo::DFloat
Calculate the sigmoid kernel between x and y.
-
.squared_error(x, y = nil) ⇒ Numo::DFloat
Calculate the pairwise squared errors between x and y.
Class Method Details
.cosine_distance(x, y = nil) ⇒ Numo::DFloat
Calculate the pairwise cosine distances between x and y.
74 75 76 77 78 |
# File 'rumale-core/lib/rumale/pairwise_metric.rb', line 74 def cosine_distance(x, y = nil) dist_mat = 1 - cosine_similarity(x, y) dist_mat[dist_mat.diag_indices] = 0 if y.nil? dist_mat.clip(0, 2) end |
.cosine_similarity(x, y = nil) ⇒ Numo::DFloat
Calculate the pairwise cosine simlarities between x and y.
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'rumale-core/lib/rumale/pairwise_metric.rb', line 55 def cosine_similarity(x, y = nil) x_norm = Numo::NMath.sqrt((x**2).sum(axis: 1)) x_norm[x_norm.eq(0)] = 1 x /= x_norm.(1) if y.nil? x.dot(x.transpose) else y_norm = Numo::NMath.sqrt((y**2).sum(axis: 1)) y_norm[y_norm.eq(0)] = 1 y /= y_norm.(1) x.dot(y.transpose) end end |
.euclidean_distance(x, y = nil) ⇒ Numo::DFloat
Calculate the pairwise euclidean distances between x and y.
15 16 17 |
# File 'rumale-core/lib/rumale/pairwise_metric.rb', line 15 def euclidean_distance(x, y = nil) Numo::NMath.sqrt(squared_error(x, y).abs) end |
.linear_kernel(x, y = nil) ⇒ Numo::DFloat
Calculate the linear kernel between x and y.
97 98 99 100 |
# File 'rumale-core/lib/rumale/pairwise_metric.rb', line 97 def linear_kernel(x, y = nil) y = x if y.nil? x.dot(y.transpose) end |
.manhattan_distance(x, y = nil) ⇒ Numo::DFloat
Calculate the pairwise manhattan distances between x and y.
24 25 26 27 28 29 30 31 32 33 |
# File 'rumale-core/lib/rumale/pairwise_metric.rb', line 24 def manhattan_distance(x, y = nil) y = x if y.nil? n_samples_x = x.shape[0] n_samples_y = y.shape[0] distance_mat = Numo::DFloat.zeros(n_samples_x, n_samples_y) n_samples_x.times do |n| distance_mat[n, true] = (y - x[n, true]).abs.sum(axis: 1) end distance_mat end |
.polynomial_kernel(x, y = nil, degree = 3, gamma = nil, coef = 1) ⇒ Numo::DFloat
Calculate the polynomial kernel between x and y.
110 111 112 113 114 |
# File 'rumale-core/lib/rumale/pairwise_metric.rb', line 110 def polynomial_kernel(x, y = nil, degree = 3, gamma = nil, coef = 1) # rubocop:disable Metrics/ParameterLists 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) ⇒ Numo::DFloat
Calculate the rbf kernel between x and y.
86 87 88 89 90 |
# File 'rumale-core/lib/rumale/pairwise_metric.rb', line 86 def rbf_kernel(x, y = nil, gamma = nil) y = x if y.nil? gamma ||= 1.0 / x.shape[1] Numo::NMath.exp(-gamma * squared_error(x, y)) end |
.sigmoid_kernel(x, y = nil, gamma = nil, coef = 1) ⇒ Numo::DFloat
Calculate the sigmoid kernel between x and y.
123 124 125 126 127 |
# File 'rumale-core/lib/rumale/pairwise_metric.rb', line 123 def sigmoid_kernel(x, y = nil, gamma = nil, coef = 1) y = x if y.nil? gamma ||= 1.0 / x.shape[1] Numo::NMath.tanh(x.dot(y.transpose) * gamma + coef) end |
.squared_error(x, y = nil) ⇒ Numo::DFloat
Calculate the pairwise squared errors between x and y.
40 41 42 43 44 45 46 47 48 |
# File 'rumale-core/lib/rumale/pairwise_metric.rb', line 40 def squared_error(x, y = nil) sum_x_vec = (x**2).sum(axis: 1).(1) sum_y_vec = y.nil? ? sum_x_vec.transpose : (y**2).sum(axis: 1).(1).transpose y = x if y.nil? err_mat = -2 * x.dot(y.transpose) err_mat += sum_x_vec err_mat += sum_y_vec err_mat.class.maximum(err_mat, 0) end |