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



76
77
78
79
80
81
# File 'lib/rumale/pairwise_metric.rb', line 76

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

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



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rumale/pairwise_metric.rb', line 26

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



91
92
93
94
95
96
97
98
99
# File 'lib/rumale/pairwise_metric.rb', line 91

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)



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

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)
  Numo::NMath.exp(-gamma * squared_error(x, y).abs)
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)



108
109
110
111
112
113
114
115
116
# File 'lib/rumale/pairwise_metric.rb', line 108

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

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



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rumale/pairwise_metric.rb', line 44

def squared_error(x, y = nil)
  y = x if y.nil?
  Rumale::Validation.check_sample_array(x)
  Rumale::Validation.check_sample_array(y)
  n_features = x.shape[1]
  one_vec = Numo::DFloat.ones(n_features).expand_dims(1)
  sum_x_vec = (x**2).dot(one_vec)
  sum_y_vec = (y**2).dot(one_vec).transpose
  dot_xy_mat = x.dot(y.transpose)
  dot_xy_mat * -2.0 + sum_x_vec + sum_y_vec
end