php naive.php

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php naive.php相关的知识,希望对你有一定的参考价值。

<?php

/**
 * Naive Bayes classifier
 */

include __DIR__ . '/../vendor/autoload.php';

function train($samples)
{
    $samples_count = count($samples);
    $classes = [];
    $freq = [];

    foreach ($samples as $sample) {
        $label = $sample['label'];
        $classes[$label] = ($classes[$label] ?? 0) + 1;
        foreach ($sample['features'] as $feature) {
            $freq[$label][$feature] = ($freq[$label][$feature] ?? 0) + 1;
        }
    }
    foreach ($freq as $label => $features) {
        foreach ($features as $feature => $count) {
            $freq[$label][$feature] = $count / $classes[$label];
        }
    }
    foreach ($classes as $label => $count) {
        $classes[$label] = $count / $samples_count;
    }

    return (object) compact('classes', 'freq');
}

function classification($classifier, $features)
{
    $res = ['m' => PHP_INT_MAX, 'label' => null];
    foreach ($classifier->classes as $label => $p) {
        $m = -log($p) + collect($features)->sum(function ($feature) use ($classifier, $label) {
                return - log(array_get($classifier->freq, "{$label}.{$feature}", 10**(-7)));
            });
        if ($m < $res['m']) {
            $res = ['m' => $m, 'label' => $label];
        }
    }

    return $res['label'];
}

function extract_features($data)
{
    return str_split($data);
}


$samples = [
    ['label' => 'd', 'value' => '234'],
    ['label' => 'd', 'value' => 'e14'],
    ['label' => 'd', 'value' => '95094'],
    ['label' => 'd', 'value' => '456'],
    ['label' => 'w', 'value' => 'sdfsldf'],
    ['label' => 'w', 'value' => 'pwper'],
    ['label' => 'w', 'value' => 'eee'],
    ['label' => 'w', 'value' => 'ee1sd'],
    ['label' => 'w', 'value' => 'ee12d'],
];

foreach ($samples as &$sample) {
    $sample['features'] = extract_features($sample['value']);
}
unset($sample);

$classifier = train($samples);

$tests = [
    '180456',
    'mcnxc',
    's89sf66sdf',
    '001u',
    'e0091',
    'ccc',
];
foreach ($tests as $test) {
    echo $test . ': ' . classification($classifier, extract_features($test)) . PHP_EOL;
}

/**
 * output:
 *      180456: d
 *      mcnxc: w
 *      s89sf66sdf: w
 *      001u: d
 *      e0091: d
 *      ccc: w
 */

以上是关于php naive.php的主要内容,如果未能解决你的问题,请参考以下文章

php [guzzle php] guzzle php #php

php send.php php邮件模板#php

IntelliJ IDEA 11编辑php是,支持php文件名为.php5和.php4,如何设置能让其也支持.php呢?

如何从php5升级到php7

请问php中如何调用php文件中的内容?

php [php:PHPMailer示例] php库“PHPMailer”示例。 #PHP