Implementasi algoritma Naive Bayes dengan menggunakan PHP


18/12/2017 07:15:48 2421 Web

Berikut ialah source code algoritma naive bayes dengan menggunakan PHP:

Nama file NaiveBayes.php

class NaiveBayes
{
    protected $data = [];
    protected $attributes = [];
    protected $prob = [];

    public function __construct(array $data, array $attributes)
    {
        $this->data  = $data;
        $this->attributes = $attributes;
    }

    protected function getTargetValues()
    {
        $targetValues = [];

        foreach ($this->data as $item) {
            $targetValues[] = $item[count($this->attributes)];
        }

        return $targetValues;
    }

    public function getLabelClass()
    {
        return array_unique($this->getTargetValues());
    }

    protected function _hitung() 
    {
        $statisticKelas = array_count_values($this->getTargetValues());
        $probabilitasKelas = [];
        foreach($statisticKelas as $kelas => $stat) {
            $probabilitasKelas[$kelas]['prob'] = $stat/count($this->data);
        }

        foreach ($this->attributes as $indexAttribute=>$attribute) {
            $kelasAttribute = [];
            foreach ($this->getLabelClass() as $labelClass) {
                $p = $this->getDataByAttributeAndClassLabel($indexAttribute, $labelClass);
                $statistikKasusByAttributes = array_count_values($p);
                foreach ($statistikKasusByAttributes as $kasus => $nilai) {
                    $ratio= $nilai/count($p);
                    $probabilitasKelas[$labelClass][$attribute][$kasus] = $ratio;
                }
            }

        }
        $this->prob = $probabilitasKelas;
    }


    protected function getDataByAttributeAndClassLabel(int $attributeIndex, string $labelClass)
    {
        $data = [];

        foreach($this->data as $item) {
            if ($item[count($this->attributes)] == $labelClass) {
                $data[] = $item[$attributeIndex];
            }
        }

        return $data;
    }

    public function run()
    {
        $this->_hitung();

        return $this;
    }

    public function predict(array $data)
    {
        $prediction = [];
        foreach($this->getLabelClass() as $labelClass) {
            $probabilistik = $this->prob[$labelClass]['prob'];
            foreach($data as $indexAttribute=>$av) {
                $probabilistik = $probabilistik * @$this->prob[$labelClass][$this->attributes[$indexAttribute]][$av];
            }
            $prediction[$labelClass] = $probabilistik;
        }
        krsort($prediction);
        return array_keys($prediction)[0];        
    }
}

?>

Penggunaan dan Uji Coba

<?php
require 'NaiveBayes.php'

$data = [
    ["sunny", "working", "go-out"],
    ["rainy", "broken", "go-out"],
    ["sunny", "working", "go-out"],
    ["sunny", "working", "go-out"],
    ["sunny", "working", "go-out"],
    ["rainy", "broken", "stay-home"],
    ["rainy", "broken", "stay-home"],
    ["sunny", "working", "stay-home"],
    ["sunny", "broken", "stay-home"],
    ["rainy", "broken", "stay-home"],
];

$data_to_predict = ['sunny', 'working'];
$nb = new NaiveBayes($data, ["Weather", "Car"]);
echo $nb->run()->predict($data_to_predict);

Simpan source code diatas dengan nama misalkan contoh.php dan jalankan pada web browser. Program diatas akan melakukan training terhadap data yang dimasukan, dan berdasarkan hasil training algoritma naive bayes dapat melakukan prediksi terhadap record selanjutnya.