Module: OpenTox::Algorithm::Similarity
- Includes:
- OpenTox::Algorithm
- Defined in:
- lib/algorithm.rb
Overview
Similarity calculations
Instance Attribute Summary
Attributes included from OpenTox
Class Method Summary collapse
-
.euclidean(properties_a, properties_b, weights = nil) ⇒ Float
Euclidean similarity.
-
.tanimoto(features_a, features_b, weights = nil) ⇒ Float
Tanimoto similarity.
Methods included from OpenTox::Algorithm
gauss, median, #run, #to_rdfxml
Methods included from OpenTox
#add_metadata, all, #delete, #initialize, #load_metadata, login, text_to_html, #to_rdfxml
Class Method Details
.euclidean(properties_a, properties_b, weights = nil) ⇒ Float
Euclidean similarity
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/algorithm.rb', line 117 def self.euclidean(properties_a,properties_b,weights=nil) common_properties = properties_a.keys & properties_b.keys if common_properties.size > 1 dist_sum = 0 common_properties.each do |p| if weights dist_sum += ( (properties_a[p] - properties_b[p]) * Algorithm.gauss(weights[p]) )**2 else dist_sum += (properties_a[p] - properties_b[p])**2 end end 1/(1+Math.sqrt(dist_sum)) else 0.0 end end |
.tanimoto(features_a, features_b, weights = nil) ⇒ Float
Tanimoto similarity
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/algorithm.rb', line 94 def self.tanimoto(features_a,features_b,weights=nil) common_features = features_a & features_b all_features = (features_a + features_b).uniq common_p_sum = 0.0 if common_features.size > 0 if weights common_features.each{|f| common_p_sum += Algorithm.gauss(weights[f])} all_p_sum = 0.0 all_features.each{|f| all_p_sum += Algorithm.gauss(weights[f])} common_p_sum/all_p_sum else common_features.to_f/all_features end else 0.0 end end |