Skip to content

Instantly share code, notes, and snippets.

@y-uti
Created November 27, 2017 13:28
Show Gist options
  • Save y-uti/90c2db3598ba9df00ba52a312517476d to your computer and use it in GitHub Desktop.
Save y-uti/90c2db3598ba9df00ba52a312517476d to your computer and use it in GitHub Desktop.
Classification of the iris dataset by LogisticRegression
<?php
require_once __DIR__ . '/../vendor/autoload.php';
use \Phpml\Classification\Linear\LogisticRegression;
use \Phpml\CrossValidation\StratifiedRandomSplit;
use \Phpml\Dataset\Demo\IrisDataset;
use \Phpml\Metric\Accuracy;
use \Phpml\Metric\ConfusionMatrix;
$dataset = new IrisDataset();
$dataset = new StratifiedRandomSplit($dataset, 0.2, 0);
$classifier = new LogisticRegression(
500,
false,
LogisticRegression::BATCH_TRAINING,
'log',
'L2'
);
$classifier->train(
$dataset->getTrainSamples(),
$dataset->getTrainLabels()
);
$predicted = $classifier->predict(
$dataset->getTestSamples()
);
$actual = $dataset->getTestLabels();
$accuracy = Accuracy::score($actual, $predicted);
$confmat = ConfusionMatrix::compute($actual, $predicted);
echo 'Accuracy = ', $accuracy * 100, "%\n";
foreach ($confmat as $row) {
foreach ($row as $col) {
printf('%3d ', $col);
}
echo "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment