train($samples, $labels); $this->assertEquals($model, $svm->getModel()); } public function testPredictSampleWithLinearKernel(): void { $samples = [[1, 3], [1, 4], [2, 4], [3, 1], [4, 1], [4, 2]]; $labels = ['a', 'a', 'a', 'b', 'b', 'b']; $svm = new SupportVectorMachine(Type::C_SVC, Kernel::LINEAR, 100.0); $svm->train($samples, $labels); $predictions = $svm->predict([ [3, 2], [2, 3], [4, -5], ]); $this->assertEquals('b', $predictions[0]); $this->assertEquals('a', $predictions[1]); $this->assertEquals('b', $predictions[2]); } public function testPredictSampleFromMultipleClassWithRbfKernel(): void { $samples = [ [1, 3], [1, 4], [1, 4], [3, 1], [4, 1], [4, 2], [-3, -1], [-4, -1], [-4, -2], ]; $labels = [ 'a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c', ]; $svm = new SupportVectorMachine(Type::C_SVC, Kernel::RBF, 100.0); $svm->train($samples, $labels); $predictions = $svm->predict([ [1, 5], [4, 3], [-4, -3], ]); $this->assertEquals('a', $predictions[0]); $this->assertEquals('b', $predictions[1]); $this->assertEquals('c', $predictions[2]); } /** * @expectedException \Phpml\Exception\InvalidArgumentException * @expectedExceptionMessage is not writable */ public function testThrowExceptionWhenVarPathIsNotWritable(): void { $svm = new SupportVectorMachine(Type::C_SVC, Kernel::RBF); $svm->setVarPath('var-path'); } /** * @expectedException \Phpml\Exception\InvalidArgumentException * @expectedExceptionMessage does not exist */ public function testThrowExceptionWhenBinPathDoesNotExist(): void { $svm = new SupportVectorMachine(Type::C_SVC, Kernel::RBF); $svm->setBinPath('bin-path'); } /** * @expectedException \Phpml\Exception\InvalidArgumentException * @expectedExceptionMessage not found */ public function testThrowExceptionWhenFileIsNotFoundInBinPath(): void { $svm = new SupportVectorMachine(Type::C_SVC, Kernel::RBF); $svm->setBinPath('var'); } }