Class: Liblinear

Inherits:
Object
  • Object
show all
Defined in:
lib/liblinear.rb,
lib/liblinear/array.rb,
lib/liblinear/error.rb,
lib/liblinear/model.rb,
lib/liblinear/example.rb,
lib/liblinear/problem.rb,
lib/liblinear/version.rb,
lib/liblinear/parameter.rb,
lib/liblinear/array/double.rb,
lib/liblinear/feature_node.rb,
lib/liblinear/array/integer.rb,
lib/liblinear/feature_node_matrix.rb

Defined Under Namespace

Classes: Array, Example, FeatureNode, FeatureNodeMatrix, InvalidParameter, Model, Parameter, Problem

Constant Summary collapse

L2R_LR =
Liblinearswig::L2R_LR
L2R_L2LOSS_SVC_DUAL =
Liblinearswig::L2R_L2LOSS_SVC_DUAL
L2R_L2LOSS_SVC =
Liblinearswig::L2R_L2LOSS_SVC
L2R_L1LOSS_SVC_DUAL =
Liblinearswig::L2R_L1LOSS_SVC_DUAL
MCSVM_CS =
Liblinearswig::MCSVM_CS
L1R_L2LOSS_SVC =
Liblinearswig::L1R_L2LOSS_SVC
L1R_LR =
Liblinearswig::L1R_LR
L2R_LR_DUAL =
Liblinearswig::L2R_LR_DUAL
L2R_L2LOSS_SVR =
Liblinearswig::L2R_L2LOSS_SVR
L2R_L2LOSS_SVR_DUAL =
Liblinearswig::L2R_L2LOSS_SVR_DUAL
L2R_L1LOSS_SVR_DUAL =
Liblinearswig::L2R_L1LOSS_SVR_DUAL
VERSION =
'1.0.0'

Class Method Summary collapse

Class Method Details

.check_parameter(problem, parameter) ⇒ String

Parameters:

Returns:

  • (String)


33
34
35
# File 'lib/liblinear.rb', line 33

def check_parameter(problem, parameter)
  Liblinearswig.check_parameter(problem.swig, parameter.swig)
end

.cross_validation(fold, parameter, labels, examples, bias = -1)) ⇒ Array <Float>

Parameters:

  • fold (Integer)
  • parameter (Hash)
  • labels (Array <Integer>)

Returns:

Raises:



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/liblinear.rb', line 43

def cross_validation(fold, parameter, labels, examples, bias = -1)
  parameter = Liblinear::Parameter.new(parameter)
  problem = Liblinear::Problem.new(labels, examples, bias)
  error_message = self.check_parameter(problem, parameter)
  raise Liblinear::InvalidParameter, error_message if error_message
  prediction_swig = Liblinearswig.new_double(labels.size)
  Liblinearswig.cross_validation(problem.swig, parameter.swig, fold, prediction_swig)
  prediction = Liblinear::Array::Double.decode(prediction_swig, labels.size)
  Liblinear::Array::Double.delete(prediction_swig)
  prediction
end

.decision_function_bias(model, label_index) ⇒ Float

Parameters:

Returns:

  • (Float)


115
116
117
# File 'lib/liblinear.rb', line 115

def decision_function_bias(model, label_index)
  Liblinearswig.get_decfun_bias(model.swig, label_index)
end

.decision_function_coefficient(model, feature_id, label_index) ⇒ Float

Parameters:

Returns:

  • (Float)


108
109
110
# File 'lib/liblinear.rb', line 108

def decision_function_coefficient(model, feature_id, label_index)
  Liblinearswig.get_decfun_coef(model.swig, feature_id, label_index)
end

.labels(model) ⇒ Array <Integer>

Parameters:

Returns:



121
122
123
124
125
126
127
# File 'lib/liblinear.rb', line 121

def labels(model)
  labels_swig = Liblinearswig.new_int(model.class_size)
  Liblinearswig.get_labels(model.swig, labels_swig)
  labels = Liblinear::Array::Integer.decode(labels_swig, model.class_size)
  Liblinear::Array::Integer.delete(labels_swig)
  labels
end

.predict(model, example) ⇒ Integer

Parameters:

Returns:

  • (Integer)


71
72
73
74
75
76
# File 'lib/liblinear.rb', line 71

def predict(model, example)
  feature_node = Liblinear::FeatureNode.new(example, model.feature_size, model.bias)
  prediction = Liblinearswig.predict(model.swig, feature_node.swig)
  feature_node.delete
  prediction
end

.predict_probabilities(model, example) ⇒ Array <Float>

Parameters:

Returns:



81
82
83
84
85
86
87
88
89
# File 'lib/liblinear.rb', line 81

def predict_probabilities(model, example)
  feature_node = Liblinear::FeatureNode.new(example, model.feature_size, model.bias)
  probability_swig = Liblinearswig.new_double(model.class_size)
  Liblinearswig.predict_probability(model.swig, feature_node.swig, probability_swig)
  probability = Liblinear::Array::Double.decode(probability_swig, model.class_size)
  Liblinear::Array::Double.delete(probability_swig)
  feature_node.delete
  probability
end

.predict_values(model, example) ⇒ Array <Float>

Parameters:

Returns:



94
95
96
97
98
99
100
101
102
# File 'lib/liblinear.rb', line 94

def predict_values(model, example)
  feature_node = Liblinear::FeatureNode.new(example, model.feature_size, model.bias)
  values_swig = Liblinearswig.new_double(model.class_size)
  Liblinearswig.predict_values(model.swig, feature_node.swig, values_swig)
  values = Liblinear::Array::Double.decode(values_swig, model.class_size)
  Liblinear::Array::Double.delete(values_swig)
  feature_node.delete
  values
end

.train(parameter, labels, examples, bias = -1)) ⇒ Liblinear::Model

Parameters:

Returns:

Raises:



60
61
62
63
64
65
66
# File 'lib/liblinear.rb', line 60

def train(parameter, labels, examples, bias = -1)
  parameter = Liblinear::Parameter.new(parameter)
  problem = Liblinear::Problem.new(labels, examples, bias)
  error_message = self.check_parameter(problem, parameter)
  raise Liblinear::InvalidParameter, error_message if error_message
  Liblinear::Model.train(problem, parameter)
end