Class: Liblinear::Model
- Inherits:
-
Object
- Object
- Liblinear::Model
- Includes:
- Liblinear, Liblinearswig
- Defined in:
- lib/liblinear/model.rb
Constant Summary
Constants included from Liblinear
Instance Attribute Summary collapse
-
#model ⇒ Object
Returns the value of attribute model.
Instance Method Summary collapse
-
#initialize(arg_1, arg_2 = nil) ⇒ Model
constructor
A new instance of Model.
-
#labels ⇒ Object
return [Array<Integer>].
-
#nr_class ⇒ Object
return [Integer].
- #predict(example) ⇒ Object
- #predict_probability(example) ⇒ Hash
- #save(filename) ⇒ Object
Methods included from Liblinear
#convert_to_feature_node_array, #double_array_c_to_ruby, #free_double_array, #free_int_array, #int_array_c_to_ruby, #new_double_array, #new_int_array
Constructor Details
#initialize(arg_1, arg_2 = nil) ⇒ Model
Returns a new instance of Model.
9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/liblinear/model.rb', line 9 def initialize(arg_1, arg_2 = nil) if arg_2 unless arg_1.is_a?(Liblinear::Problem) && arg_2.is_a?(Liblinear::Parameter) raise ArgumentError, 'arguments must be Liblinear::Problem and Liblinear::Parameter' end error_msg = check_parameter(arg_1.prob, arg_2.params) raise InvalidParameter, error_msg if error_msg @model = train(arg_1.prob, arg_2.params) else raise ArgumentError, 'argument must be String' unless arg_1.is_a?(String) @model = load_model(arg_1) end end |
Instance Attribute Details
#model ⇒ Object
Returns the value of attribute model.
5 6 7 |
# File 'lib/liblinear/model.rb', line 5 def model @model end |
Instance Method Details
#labels ⇒ Object
return [Array<Integer>]
29 30 31 32 33 34 35 |
# File 'lib/liblinear/model.rb', line 29 def labels c_int_array = new_int(nr_class) get_labels(@model, c_int_array) labels = int_array_c_to_ruby(c_int_array, nr_class) delete_int(c_int_array) labels end |
#nr_class ⇒ Object
return [Integer]
24 25 26 |
# File 'lib/liblinear/model.rb', line 24 def nr_class get_nr_class(@model) end |
#predict(example) ⇒ Object
38 39 40 41 42 43 |
# File 'lib/liblinear/model.rb', line 38 def predict(example) feature_nodes = convert_to_feature_node_array(example, @model.nr_feature, @model.bias) prediction = Liblinearswig.predict(@model, feature_nodes) feature_node_array_destroy(feature_nodes) prediction end |
#predict_probability(example) ⇒ Hash
47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/liblinear/model.rb', line 47 def predict_probability(example) feature_nodes = convert_to_feature_node_array(example, @model.nr_feature, @model.bias) c_double_array = new_double_array(nr_class) Liblinearswig.predict_probability(@model, feature_nodes, c_double_array) probabilities = double_array_c_to_ruby(c_double_array, nr_class) delete_double(c_double_array) feature_node_array_destroy(feature_nodes) probability_list = {} labels.size.times do |i| probability_list[labels[i]] = probabilities[i] end probability_list end |
#save(filename) ⇒ Object
62 63 64 |
# File 'lib/liblinear/model.rb', line 62 def save(filename) save_model(filename, @model) end |