如何在PHP-ML中训练SVC分类器

时间:2019-09-10 22:29:59

标签: php machine-learning svm

我是机器学习主题的新手。我必须了解它在我的代码示例中的工作方式。该应用程序的目标是:

学习(第一个代码) -我有一些产品的网上商店 -这些产品已添加到数据库中。描述的形式是示例结束URL的形式是Label(仅针对行表中的字段) -我正在将此数据加载到ArrayDataset(在代码上查看) -最后,我正在训练分类器,并将训练后的模型保存到文件中

还原(第二个代码) -当我收到消息并做出预测时,我取得了不错的结果,但是我不明白它是怎么回事

问题是:

1)这是训练分类器的正确方法吗?

2)如何根据来自客户的消息和聊天机器人的响应来训练模型?

3)现在,我正在根据每条用户消息预测产品。我应该将每条消息保存到一个字符串中,然后给他一些响应吗?我能找到任何关于它的信息。如何在PHP / Laravel中做到这一点?邮件没有会话ID

在第二个代码中,您将看到$text = 'z autorską grafiką';->将来,它将是来自客户的消息或类似的消息。现在,它是测试的静态值。

学习

/**
 * The name and signature of the console command.
 *
 * @var string
 */
protected $signature = 'model:learn';

/**
 * The console command description.
 *
 * @var string
 */
protected $description = 'Funkcja do uczenia się :D';

/**
 * Create a new command instance.
 *
 * @return void
 */
public function __construct()
{
    parent::__construct();
}

/**
 * Execute the console command.
 *
 * @return mixed
 */
public function handle()
{   
    Log::debug('Test funkcji Learn START');

    $records = Training::get(); // i'm getting the data from data base
    $label = []; // init empty labels array
    $description = []; // init empty labels array

    foreach($records as $record) {
        $description[] = $record->description;
        $label[] = $record->label;
    } // add the records to arrays

    $dataset = new ArrayDataset(
        $description,
        $label
    ); // init new ArrayDataset

    $pipeline = new Pipeline([
        new TokenCountVectorizer(new NGramTokenizer(1, 3), new Polish()),
        new TfIdfTransformer()
    ], new SVC()); 
    // TokenCountVectorizer - Transform a collection of text samples to a vector of token counts. 
    // NGramTokenizer - Transform a collection of text samples to a vector of token counts.
    // TfIdfTransformer() - Tf–idf, short for term frequency–inverse document frequency, is a numerical statistic that is intended to reflect how important a word is to a document in a collection or corpus.

    $pipeline->train($dataset->getSamples(), $dataset->getTargets()); // train

    $predicted = $pipeline->predict($dataset->getSamples()); // predicted

    $modelManager = new ModelManager();
    $modelManager->saveToFile($pipeline, public_path('/upload/shop.phpml')); // save model to file
    Log::debug('Test funkcji Learn END');
}

还原

/**
 * The name and signature of the console command.
 *
 * @var string
 */
protected $signature = 'model:restore';

/**
 * The console command description.
 *
 * @var string
 */
protected $description = 'Restore';

/**
 * Create a new command instance.
 *
 * @return void
 */
public function __construct()
{
    parent::__construct();
}

/**
 * Execute the console command.
 *
 * @return mixed
 */
public function handle()
{
    $start = microtime(true);
    $modelManager = new ModelManager();
    $model = $modelManager->restoreFromFile(public_path('/upload/shop.phpml'));
    $total = microtime(true) - $start;

    $text = 'z autorsk grafiką';
    $start = microtime(true);
    $predicted = $model->predict([$text])[0];

    echo sprintf('Predicted category: %s in %ss', $predicted, round($total, 6)) . PHP_EOL;
}

我必须以属性方式训练模型。对我来说,这是错误的方法,但我找不到其他方法-更好。希望您能向我解释它应该如何工作。

谢谢。

0 个答案:

没有答案