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

Class Method Details

.cosine_distance(x, y = nil) ⇒ Numo::DFloat

Calculate the pairwise cosine distances between x and y.

Parameters:

  • x (Numo::DFloat)

    (shape: [n_samples_x, n_features])

  • y (Numo::DFloat) (defaults to: nil)

    (shape: [n_samples_y, n_features])

Returns:

  • (Numo::DFloat)

    (shape: [n_samples_x, n_samples_x] or [n_samples_x, n_samples_y] if y is given)



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.

Parameters:

  • x (Numo::DFloat)

    (shape: [n_samples_x, n_features])

  • y (Numo::DFloat) (defaults to: nil)

    (shape: [n_samples_y, n_features])

Returns:

  • (Numo::DFloat)

    (shape: [n_samples_x, n_samples_x] or [n_samples_x, n_samples_y] if y is given)



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.expand_dims(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.expand_dims(1)
    x.dot(y.transpose)
  end
end

.euclidean_distance(x, y = nil) ⇒ Numo::DFloat

Calculate the pairwise euclidean distances between x and y.

Parameters:

  • x (Numo::DFloat)

    (shape: [n_samples_x, n_features])

  • y (Numo::DFloat) (defaults to: nil)

    (shape: [n_samples_y, n_features])

Returns:

  • (Numo::DFloat)

    (shape: [n_samples_x, n_samples_x] or [n_samples_x, n_samples_y] if y is given)



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.

Parameters:

  • x (Numo::DFloat)

    (shape: [n_samples_x, n_features])

  • y (Numo::DFloat) (defaults to: nil)

    (shape: [n_samples_y, n_features])

Returns:

  • (Numo::DFloat)

    (shape: [n_samples_x, n_samples_x] or [n_samples_x, n_samples_y] if y is given)



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.

Parameters:

  • x (Numo::DFloat)

    (shape: [n_samples_x, n_features])

  • y (Numo::DFloat) (defaults to: nil)

    (shape: [n_samples_y, n_features])

Returns:

  • (Numo::DFloat)

    (shape: [n_samples_x, n_samples_x] or [n_samples_x, n_samples_y] if y is given)



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.

Parameters:

  • x (Numo::DFloat)

    (shape: [n_samples_x, n_features])

  • y (Numo::DFloat) (defaults to: nil)

    (shape: [n_samples_y, n_features])

  • degree (Integer) (defaults to: 3)

    The parameter of polynomial kernel.

  • gamma (Float) (defaults to: nil)

    The parameter of polynomial kernel, if nil it is 1 / n_features.

  • coef (Integer) (defaults to: 1)

    The parameter of polynomial kernel.

Returns:

  • (Numo::DFloat)

    (shape: [n_samples_x, n_samples_x] or [n_samples_x, n_samples_y] if y is given)



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.

Parameters:

  • x (Numo::DFloat)

    (shape: [n_samples_x, n_features])

  • y (Numo::DFloat) (defaults to: nil)

    (shape: [n_samples_y, n_features])

  • gamma (Float) (defaults to: nil)

    The parameter of rbf kernel, if nil it is 1 / n_features.

Returns:

  • (Numo::DFloat)

    (shape: [n_samples_x, n_samples_x] or [n_samples_x, n_samples_y] if y is given)



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.

Parameters:

  • x (Numo::DFloat)

    (shape: [n_samples_x, n_features])

  • y (Numo::DFloat) (defaults to: nil)

    (shape: [n_samples_y, n_features])

  • gamma (Float) (defaults to: nil)

    The parameter of polynomial kernel, if nil it is 1 / n_features.

  • coef (Integer) (defaults to: 1)

    The parameter of polynomial kernel.

Returns:

  • (Numo::DFloat)

    (shape: [n_samples_x, n_samples_x] or [n_samples_x, n_samples_y] if y is given)



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.

Parameters:

  • x (Numo::DFloat)

    (shape: [n_samples_x, n_features])

  • y (Numo::DFloat) (defaults to: nil)

    (shape: [n_samples_y, n_features])

Returns:

  • (Numo::DFloat)

    (shape: [n_samples_x, n_samples_x] or [n_samples_x, n_samples_y] if y is given)



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).expand_dims(1)
  sum_y_vec = y.nil? ? sum_x_vec.transpose : (y**2).sum(axis: 1).expand_dims(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