Class: LModel
- Inherits:
-
Object
- Object
- LModel
- Defined in:
- lib/linear.rb
Instance Attribute Summary collapse
-
#model ⇒ Object
Returns the value of attribute model.
-
#probability ⇒ Object
Returns the value of attribute probability.
Instance Method Summary collapse
- #destroy ⇒ Object
- #get_labels ⇒ Object
- #get_nr_class ⇒ Object
-
#initialize(arg1, arg2 = nil) ⇒ LModel
constructor
A new instance of LModel.
- #predict(x) ⇒ Object
- #predict_probability(x) ⇒ Object
- #predict_values(x) ⇒ Object
- #predict_values_raw(x) ⇒ Object
-
#save(filename) ⇒ Object
def get_svr_pdf #get_svr_probability will handle error checking sigma = get_svr_probability() return Proc.new{|z| exp(-z.abs/sigma)/(2*sigma)} # TODO: verify this works end.
Constructor Details
#initialize(arg1, arg2 = nil) ⇒ LModel
Returns a new instance of LModel.
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/linear.rb', line 205 def initialize(arg1,arg2=nil) if arg2 == nil # create model from file filename = arg1 @model = load_model(filename) else # create model from problem and parameter prob,param = arg1,arg2 @prob = prob msg = check_parameter(prob.prob,param.param) raise "ValueError", msg if msg @model = Liblinear::train(prob.prob,param.param) end #setup some classwide variables @nr_class = Liblinear::get_nr_class(@model) #create labels(classes) intarr = new_int(@nr_class) Liblinear::get_labels(@model,intarr) @labels = _int_array_to_list(intarr, @nr_class) delete_int(intarr) end |
Instance Attribute Details
#model ⇒ Object
Returns the value of attribute model.
203 204 205 |
# File 'lib/linear.rb', line 203 def model @model end |
#probability ⇒ Object
Returns the value of attribute probability.
203 204 205 |
# File 'lib/linear.rb', line 203 def probability @probability end |
Instance Method Details
#destroy ⇒ Object
319 320 321 |
# File 'lib/linear.rb', line 319 def destroy destroy_model(@model) end |
#get_labels ⇒ Object
239 240 241 |
# File 'lib/linear.rb', line 239 def get_labels return @labels end |
#get_nr_class ⇒ Object
235 236 237 |
# File 'lib/linear.rb', line 235 def get_nr_class return @nr_class end |
#predict(x) ⇒ Object
227 228 229 230 231 232 |
# File 'lib/linear.rb', line 227 def predict(x) data = _convert_to_feature_node_array(x, @model.nr_feature, @model.bias) ret = Liblinear::predict(@model,data) feature_node_array_destroy(data) return ret end |
#predict_probability(x) ⇒ Object
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
# File 'lib/linear.rb', line 281 def predict_probability(x) # if not @probability # raise TypeError, "model does not support probabiliy estimates" # end #convert x into feature_node, alloc a double array to receive probabilities data = _convert_to_feature_node_array(x, @model.nr_feature, @model.bias) dblarr = new_double(@nr_class) pred = Liblinear::predict_probability(@model, data, dblarr) pv = _double_array_to_list(dblarr, @nr_class) delete_double(dblarr) feature_node_array_destroy(data) p = {} for i in (0..@labels.size-1) p[@labels[i]] = pv[i] end return pred, p end |
#predict_values(x) ⇒ Object
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/linear.rb', line 255 def predict_values(x) v=predict_values_raw(x) #puts v.inspect if false #if @svm_type == NU_SVR or @svm_type == EPSILON_SVR or @svm_type == ONE_CLASS return v[0] else #self.svm_type == C_SVC or self.svm_type == NU_SVC count = 0 # create a width x height array width = @labels.size height = @labels.size d = Array.new(width) d.map! { Array.new(height) } for i in (0..@labels.size-1) for j in (i+1..@labels.size-1) d[@labels[i]][@labels[j]] = v[count] d[@labels[j]][@labels[i]] = -v[count] count += 1 end end return d end end |
#predict_values_raw(x) ⇒ Object
243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/linear.rb', line 243 def predict_values_raw(x) #convert x into feature_node, allocate a double array for return n = (@nr_class*(@nr_class-1)/2).floor data = _convert_to_feature_node_array(x, @model.nr_feature, @model.bias) dblarr = new_double(n) Liblinear::predict_values(@model, data, dblarr) ret = _double_array_to_list(dblarr, n) delete_double(dblarr) feature_node_array_destroy(data) return ret end |
#save(filename) ⇒ Object
def get_svr_pdf
#get_svr_probability will handle error checking
sigma = get_svr_probability()
return Proc.new{|z| exp(-z.abs/sigma)/(2*sigma)} # TODO: verify this works
end
315 316 317 |
# File 'lib/linear.rb', line 315 def save(filename) save_model(filename,@model) end |