Phpml\SupportVectorMachine\SupportVectorMachine::train PHP Method

train() public method

public train ( array $samples, array $labels )
$samples array
$labels array
    public function train(array $samples, array $labels)
    {
        $this->labels = $labels;
        $trainingSet = DataTransformer::trainingSet($samples, $labels, in_array($this->type, [Type::EPSILON_SVR, Type::NU_SVR]));
        file_put_contents($trainingSetFileName = $this->varPath . uniqid(), $trainingSet);
        $modelFileName = $trainingSetFileName . '-model';
        $command = $this->buildTrainCommand($trainingSetFileName, $modelFileName);
        $output = '';
        exec(escapeshellcmd($command), $output);
        $this->model = file_get_contents($modelFileName);
        unlink($trainingSetFileName);
        unlink($modelFileName);
    }

Usage Example

 public function testPredictSampleFromMultipleClassWithRbfKernel()
 {
     $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]);
 }