lib/rumale/pairwise_metric.rb in rumale-0.13.0 vs lib/rumale/pairwise_metric.rb in rumale-0.13.1

- old
+ new

@@ -16,23 +16,36 @@ Rumale::Validation.check_sample_array(x) Rumale::Validation.check_sample_array(y) Numo::NMath.sqrt(squared_error(x, y).abs) end + # Calculate the pairwise manhattan distances between x and y. + # + # @param x [Numo::DFloat] (shape: [n_samples_x, n_features]) + # @param y [Numo::DFloat] (shape: [n_samples_y, n_features]) + # @return [Numo::DFloat] (shape: [n_samples_x, n_samples_x] or [n_samples_x, n_samples_y] if y is given) + 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 + # Calculate the pairwise squared errors between x and y. # # @param x [Numo::DFloat] (shape: [n_samples_x, n_features]) # @param y [Numo::DFloat] (shape: [n_samples_y, n_features]) # @return [Numo::DFloat] (shape: [n_samples_x, n_samples_x] or [n_samples_x, n_samples_y] if y is given) def squared_error(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) - # dot_xy_mat * -2.0 + sum_x_vec.tile(y.shape[0], 1).transpose + sum_y_vec.tile(x.shape[0], 1) - # 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)