spec/model_spec.rb in rb-libsvm-1.1.5 vs spec/model_spec.rb in rb-libsvm-1.2.0
- old
+ new
@@ -7,13 +7,13 @@
end
def create_problem
problem = Problem.new
features = [Node.features([0.2,0.3,0.4,0.4]),
- Node.features([0.1,0.5,0.1,0.9]),
- Node.features([0.2,0.2,0.6,0.5]),
- Node.features([0.3,0.1,0.5,0.9])]
+ Node.features([0.1,0.5,0.1,0.9]),
+ Node.features([0.2,0.2,0.6,0.5]),
+ Node.features([0.3,0.1,0.5,0.9])]
problem.set_examples([1,2,1,2], features)
problem
end
def create_parameter
@@ -37,18 +37,26 @@
@problem = create_problem
@parameter = create_parameter
end
it "results from training on a problem under a certain parameter set" do
- model = Model.train(@problem,@parameter)
- model.should_not be_nil
+ expect(Model.train(@problem, @parameter)).not_to be_nil
end
+ let(:labels) { Model.cross_validation(@problem, @parameter, fold=2) }
+
it "can do cross-validation" do
- labels = Model.cross_validation(@problem, @parameter, fold=2)
- labels.should == [anything, anything, anything, anything]
+ expect(labels).to contain_exactly(
+ an_instance_of(Float),
+ an_instance_of(Float),
+ an_instance_of(Float),
+ an_instance_of(Float))
end
+
+ it "number of labels" do
+ expect(labels.length).to eq(4)
+ end
end
describe "A saved model" do
include ModelSpecHelper
@@ -57,50 +65,72 @@
model = Model.train(create_problem, create_parameter)
model.save(@filename)
end
it "can be loaded" do
- model = Model.load(@filename)
- model.should_not be_nil
+ expect(Model.load(@filename)).to be_an_instance_of(Model)
end
after(:each) do
File.delete(@filename) rescue nil
end
end
-describe "An Libsvm model" do
+describe "A Libsvm model" do
include ModelSpecHelper
before(:each) do
@problem = create_problem
@parameter = create_parameter
@model = Model.train(@problem, @parameter)
@file_path = "tmp/svm_model.model"
File.delete(@file_path) if File.exists?(@file_path)
end
- it "can be saved to a file" do
- @model.save(@file_path)
- File.exist?(@file_path).should be_true
- end
+ describe "basic operations" do
+ it "can be saved to a file" do
+ expect {
+ @model.save(@file_path)
+ }.to change { File.exist?(@file_path) }.from(false).to(true)
+ end
- it "can be asked for it's svm_type" do
- @model.svm_type.should == SvmType::C_SVC
+ it "can be asked for its svm_type" do
+ expect(@model.svm_type).to eq SvmType::C_SVC
+ end
+
+ it "can be asked for its number of classes (aka. labels)" do
+ expect(@model.classes).to eq(2)
+ end
end
- it "can be asked for it's number of classes (aka. labels)" do
- @model.classes.should == 2
+ describe "predict" do
+ it "returns floats" do
+ expect(@model.predict(create_example)).to be_an_instance_of(Float)
+ end
end
- it "can predict" do
- prediction = @model.predict(create_example)
- prediction.should_not be_nil
+ describe "support_vectors" do
+ it "returns count" do
+ expect(@model.support_vectors).to eq(3)
+ end
end
- it "can predict probability" do
- prediction, probabilities = @model.predict_probability(create_example)
- prediction.should_not be_nil
- probabilities.should have(@model.classes).items
- probabilities.each { |e| e.should_not be_nil }
+ describe "predict_probability" do
+ let(:result) { @model.predict_probability(create_example) }
+ let(:prediction) { result.first }
+ let(:probabilities) { result.last }
+
+ it "produces prediction" do
+ expect(prediction).not_to be_nil
+ end
+
+ it "produces probabilities for each class" do
+ expect(probabilities.length).to eq(@model.classes)
+ end
+
+ it "can predict probability" do
+ probabilities.each do |p|
+ expect(p).to be_an_instance_of(Float)
+ end
+ end
end
end