PHP code example of krypt0nn / cati-tree

1. Go to this page and download the library: Download krypt0nn/cati-tree 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/ */

    

krypt0nn / cati-tree example snippets


$tree = CATI\Tree::train ([
    'a' => [
        [1, 2, 3],
        [1, 2, 4],
        [5, 6, 7],
        [6, 7, 8],
        [2, 3, 6]
    ],

    'b' => [
        [2, 3, 1]
    ]
]);

echo 'Training accuracy: '. $tree->acuracy();

file_put_contents ('tree.json', json_encode ($tree->export ()));

$tree = CATI\Tree::load (json_decode (file_get_contents ('tree.json'), true));

echo $tree->predict ([6, 7, 8]) ?: 'unknown'; // a

$forest = CATI\RandomForest::create ([
    'a' => [
        [1, 2, 3],
        [1, 2, 4],
        [5, 6, 7],
        [6, 7, 8],
        [2, 3, 6]
    ],

    'b' => [
        [2, 3, 1]
    ]
], forestSize: 5);

echo 'Training accuracy: '. $forest->acuracy();

file_put_contents ('forest.json', json_encode ($forest->export ()));

$forest = CATI\RandomForest::load (json_decode (file_get_contents ('forest.json'), true));

print_r ($forest->probability ([6, 7, 8]));