Sha256: ef748314768145525b80f121fd91359497a9e01a7c7d717b59cbb5c45f7fcbce

Contents?: true

Size: 735 Bytes

Versions: 17

Compression:

Stored size: 735 Bytes

Contents

module DbClustering
  module DistanceMetrics
    class AverageDifference
      include Math

      def initialize(min_dimensions: 1)
        @min_dimensions = min_dimensions
      end

      def distance(vector1, vector2)
        vector1_array = vector1.array_for_comparison(vector2)
        vector2_array = vector2.array_for_comparison(vector1)

        if vector1_array.count != vector2_array.count
          raise "Vectors with different sizes cannot be compared"
        end

        if vector1_array.count < @min_dimensions
          return Float::INFINITY
        end

        sum = vector1_array.map.with_index{ |x, i| (x - vector2_array[i]).abs }.reduce(&:+)
        sum / vector1_array.count.to_f
      end

    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
db_clustering-0.1.17 lib/distance_metrics/average_difference.rb
db_clustering-0.1.16 lib/distance_metrics/average_difference.rb
db_clustering-0.1.15 lib/distance_metrics/average_difference.rb
db_clustering-0.1.14 lib/distance_metrics/average_difference.rb
db_clustering-0.1.13 lib/distance_metrics/average_difference.rb
db_clustering-0.1.12 lib/distance_metrics/average_difference.rb
db_clustering-0.1.11 lib/distance_metrics/average_difference.rb
db_clustering-0.1.10 lib/distance_metrics/average_difference.rb
db_clustering-0.1.9 lib/distance_metrics/average_difference.rb
db_clustering-0.1.8 lib/distance_metrics/average_difference.rb
db_clustering-0.1.7 lib/distance_metrics/average_difference.rb
db_clustering-0.1.6 lib/distance_metrics/average_difference.rb
db_clustering-0.1.5 lib/distance_metrics/average_difference.rb
db_clustering-0.1.4 lib/distance_metrics/average_difference.rb
db_clustering-0.1.3 lib/distance_metrics/average_difference.rb
db_clustering-0.1.2 lib/distance_metrics/average_difference.rb
db_clustering-0.1.1 lib/distance_metrics/average_difference.rb