Created
November 27, 2017 13:28
-
-
Save y-uti/90c2db3598ba9df00ba52a312517476d to your computer and use it in GitHub Desktop.
Classification of the iris dataset by LogisticRegression
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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