Module: OpenTox::Algorithm::Neighbors
- Defined in:
- lib/algorithm.rb
Class Method Summary collapse
-
.local_svm_regression(neighbors, params) ⇒ Hash
Local support vector regression from neighbors.
-
.weighted_majority_vote(neighbors, params = {}) ⇒ Hash
Classification with majority vote from neighbors weighted by similarity.
Class Method Details
.local_svm_regression(neighbors, params) ⇒ Hash
Local support vector regression from neighbors
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/algorithm.rb', line 167 def self.local_svm_regression(neighbors,params ) sims = neighbors.collect{ |n| n[:similarity] } # similarity values between query and neighbors conf = sims.inject{|sum,x| sum + x } acts = neighbors.collect do |n| act = n[:activity] Math.log10(act.to_f) end # activities of neighbors for supervised learning neighbor_matches = neighbors.collect{ |n| n[:features] } # as in classification: URIs of matches gram_matrix = [] # square matrix of similarities between neighbors; implements weighted tanimoto kernel if neighbor_matches.size == 0 raise "No neighbors found" else # gram matrix (0..(neighbor_matches.length-1)).each do |i| gram_matrix[i] = [] unless gram_matrix[i] # upper triangle ((i+1)..(neighbor_matches.length-1)).each do |j| sim = eval("#{params[:similarity_algorithm]}(neighbor_matches[i], neighbor_matches[j], params[:p_values])") gram_matrix[i][j] = Algorithm.gauss(sim) gram_matrix[j] = [] unless gram_matrix[j] gram_matrix[j][i] = gram_matrix[i][j] # lower triangle end gram_matrix[i][i] = 1.0 end LOGGER.debug gram_matrix.to_yaml @r = RinRuby.new(false,false) # global R instance leads to Socket errors after a large number of requests @r.eval "library('kernlab')" # this requires R package "kernlab" to be installed LOGGER.debug "Setting R data ..." # set data @r.gram_matrix = gram_matrix.flatten @r.n = neighbor_matches.size @r.y = acts @r.sims = sims LOGGER.debug "Preparing R data ..." # prepare data @r.eval "y<-as.vector(y)" @r.eval "gram_matrix<-as.kernelMatrix(matrix(gram_matrix,n,n))" @r.eval "sims<-as.vector(sims)" # model + support vectors LOGGER.debug "Creating SVM model ..." @r.eval "model<-ksvm(gram_matrix, y, kernel=matrix, type=\"nu-svr\", nu=0.8)" @r.eval "sv<-as.vector(SVindex(model))" @r.eval "sims<-sims[sv]" @r.eval "sims<-as.kernelMatrix(matrix(sims,1))" LOGGER.debug "Predicting ..." @r.eval "p<-predict(model,sims)[1,1]" prediction = 10**(@r.p.to_f) LOGGER.debug "Prediction is: '" + @prediction.to_s + "'." @r.quit # free R end confidence = conf/neighbors.size if neighbors.size > 0 {:prediction => prediction, :confidence => confidence} end |
.weighted_majority_vote(neighbors, params = {}) ⇒ Hash
Classification with majority vote from neighbors weighted by similarity
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/algorithm.rb', line 141 def self.weighted_majority_vote(neighbors,params={}) conf = 0.0 confidence = 0.0 neighbors.each do |neighbor| case neighbor[:activity].to_s when 'true' conf += Algorithm.gauss(neighbor[:similarity]) when 'false' conf -= Algorithm.gauss(neighbor[:similarity]) end end if conf > 0.0 prediction = true elsif conf < 0.0 prediction = false else prediction = nil end confidence = conf/neighbors.size if neighbors.size > 0 {:prediction => prediction, :confidence => confidence.abs} end |