PHP code example of medansoftware / c45-algorithm-php
1. Go to this page and download the library: Download medansoftware/c45-algorithm-php library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
medansoftware / c45-algorithm-php example snippets
$c45 = new Algorithm\C45('examples/example.xlsx', 'PLAY');
$tree = $c45->initialize()->buildTree();
echo $tree->toString();
$c45 = new Algorithm\C45();
$c45->loadFile('examples/example.xlsx');
$c45->setTargetAttribute('PLAY');
$tree = $c45->initialize()->buildTree();
echo $tree->toString();
$data = [
['OUTLOOK' => 'Sunny', 'TEMPERATURE' => 'Hot', 'HUMIDITY' => 'High', 'WINDY' => 'False', 'PLAY' => 'No'],
['OUTLOOK' => 'Sunny', 'TEMPERATURE' => 'Hot', 'HUMIDITY' => 'High', 'WINDY' => 'True', 'PLAY' => 'No'],
['OUTLOOK' => 'Cloudy', 'TEMPERATURE' => 'Hot', 'HUMIDITY' => 'High', 'WINDY' => 'False', 'PLAY' => 'Yes'],
['OUTLOOK' => 'Rainy', 'TEMPERATURE' => 'Mild', 'HUMIDITY' => 'High', 'WINDY' => 'False', 'PLAY' => 'Yes'],
['OUTLOOK' => 'Rainy', 'TEMPERATURE' => 'Cool', 'HUMIDITY' => 'Normal', 'WINDY' => 'False', 'PLAY' => 'Yes'],
['OUTLOOK' => 'Rainy', 'TEMPERATURE' => 'Cool', 'HUMIDITY' => 'Normal', 'WINDY' => 'True', 'PLAY' => 'No'],
['OUTLOOK' => 'Cloudy', 'TEMPERATURE' => 'Cool', 'HUMIDITY' => 'Normal', 'WINDY' => 'True', 'PLAY' => 'Yes'],
['OUTLOOK' => 'Sunny', 'TEMPERATURE' => 'Mild', 'HUMIDITY' => 'High', 'WINDY' => 'False', 'PLAY' => 'No'],
['OUTLOOK' => 'Sunny', 'TEMPERATURE' => 'Cool', 'HUMIDITY' => 'Normal', 'WINDY' => 'False', 'PLAY' => 'Yes'],
['OUTLOOK' => 'Rainy', 'TEMPERATURE' => 'Mild', 'HUMIDITY' => 'Normal', 'WINDY' => 'False', 'PLAY' => 'Yes'],
['OUTLOOK' => 'Sunny', 'TEMPERATURE' => 'Mild', 'HUMIDITY' => 'Normal', 'WINDY' => 'True', 'PLAY' => 'Yes'],
['OUTLOOK' => 'Cloudy', 'TEMPERATURE' => 'Mild', 'HUMIDITY' => 'High', 'WINDY' => 'True', 'PLAY' => 'Yes'],
['OUTLOOK' => 'Cloudy', 'TEMPERATURE' => 'Hot', 'HUMIDITY' => 'Normal', 'WINDY' => 'False', 'PLAY' => 'Yes'],
['OUTLOOK' => 'Rainy', 'TEMPERATURE' => 'Mild', 'HUMIDITY' => 'High', 'WINDY' => 'True', 'PLAY' => 'No'],
];
$input = new Algorithm\C45\DataInput();
$input->setData($data);
$input->setAttributes(['OUTLOOK', 'TEMPERATURE', 'HUMIDITY', 'WINDY', 'PLAY']);
$c45 = new Algorithm\C45();
$c45->c45 = $input;
$c45->setTargetAttribute('PLAY');
$tree = $c45->initialize()->buildTree();
echo $tree->toString();
$input = new Algorithm\C45\DataInput();
$input->loadCsv('example.csv'); // delimiter defaults to ','
$c45 = new Algorithm\C45();
$c45->c45 = $input;
$c45->setTargetAttribute('PLAY');
$tree = $c45->initialize()->buildTree();
echo $tree->toString();
$newData = [
'OUTLOOK' => 'Sunny',
'TEMPERATURE' => 'Hot',
'HUMIDITY' => 'High',
'WINDY' => 'False',
];
echo $tree->classify($newData); // "No"
$newData = [
'TEMPERATURE' => 'Hot',
'HUMIDITY' => 'High',
'WINDY' => 'False',
// OUTLOOK is missing
];
echo $tree->classify($newData);
$result = $c45->evaluate($tree, $testData);
echo $result['accuracy']; // e.g. 0.86
echo $result['correct'] . '/' . $result['total'];
print_r($result['misclassified']);
[
'accuracy' => 0.86,
'correct' => 86,
'total' => 100,
'misclassified' => [
// rows that were classified incorrectly
],
]
echo $tree->toString();
echo $tree->toJson();
print_r($tree->toArray());
file_put_contents('tree.dot', $tree->toDot());
bash
> composer bash
composer text
.
├── .github/
│ └── workflows/
│ └── tests.yml
├── examples/
│ ├── example.xlsx
│ ├── tree.dot
│ └── tree.png
├── src/
│ ├── C45.php
│ └── C45/
│ ├── Calculator/
│ ├── DataInput/
│ ├── DataInput.php
│ └── TreeNode.php
├── tests/
├── composer.json
├── phpunit.xml
├── CHANGELOG.md
└── LICENSE