Module: Rumale::PairwiseMetric

Defined in:
lib/rumale/pairwise_metric.rb

Overview

Module for calculating pairwise distances, similarities, and kernels.

Class Method Summary collapse

Class Method Details

.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)



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rumale/pairwise_metric.rb', line 14

def euclidean_distance(x, y = nil)
  y = x if y.nil?
  Rumale::Validation.check_sample_array(x)
  Rumale::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 +
                    sum_y_vec.tile(x.shape[0], 1)
  Numo::NMath.sqrt(distance_matrix.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)



48
49
50
51
52
53
# File 'lib/rumale/pairwise_metric.rb', line 48

def linear_kernel(x, y = nil)
  y = x if y.nil?
  Rumale::Validation.check_sample_array(x)
  Rumale::Validation.check_sample_array(y)
  x.dot(y.transpose)
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)



63
64
65
66
67
68
69
70
71
# File 'lib/rumale/pairwise_metric.rb', line 63

def polynomial_kernel(x, y = nil, degree = 3, gamma = nil, coef = 1)
  y = x if y.nil?
  gamma ||= 1.0 / x.shape[1]
  Rumale::Validation.check_sample_array(x)
  Rumale::Validation.check_sample_array(y)
  Rumale::Validation.check_params_float(gamma: gamma)
  Rumale::Validation.check_params_integer(degree: degree, coef: coef)
  (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)



33
34
35
36
37
38
39
40
41
# File 'lib/rumale/pairwise_metric.rb', line 33

def rbf_kernel(x, y = nil, gamma = nil)
  y = x if y.nil?
  gamma ||= 1.0 / x.shape[1]
  Rumale::Validation.check_sample_array(x)
  Rumale::Validation.check_sample_array(y)
  Rumale::Validation.check_params_float(gamma: gamma)
  distance_matrix = euclidean_distance(x, y)
  Numo::NMath.exp((distance_matrix**2) * -gamma)
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)



80
81
82
83
84
85
86
87
88
# File 'lib/rumale/pairwise_metric.rb', line 80

def sigmoid_kernel(x, y = nil, gamma = nil, coef = 1)
  y = x if y.nil?
  gamma ||= 1.0 / x.shape[1]
  Rumale::Validation.check_sample_array(x)
  Rumale::Validation.check_sample_array(y)
  Rumale::Validation.check_params_float(gamma: gamma)
  Rumale::Validation.check_params_integer(coef: coef)
  Numo::NMath.tanh(x.dot(y.transpose) * gamma + coef)
end