机器学习无处不在——推荐电影、标记图像,现在甚至对新闻文章进行分类。想象一下如果您可以在 php 中做到这一点!借助 rubix ml,您可以以简单易懂的方式将机器学习的强大功能引入 php。本指南将引导您构建一个简单的新闻分类器,将文章分类为“体育”或“技术”等类别。最后,您将拥有一个工作分类器,可以根据新文章的内容预测其类别。
这个项目非常适合想要使用 php 进行机器学习的初学者,您可以按照 github 上的完整代码进行操作。
rubix ml 是一个 php 机器学习库,它将 ml 工具和算法引入 php 友好的环境中。无论您从事分类、回归、聚类,甚至自然语言处理,rubix ml 都能满足您的需求。它允许您加载和预处理数据、训练模型并评估性能——所有这些都在 php 中进行。
rubix ml 支持广泛的机器学习任务,例如:
让我们深入了解如何使用 rubix ml 在 php 中构建简单的新闻分类器!
我们将首先使用 rubix ml 设置一个新的 php 项目并配置自动加载。
创建一个新的项目目录并导航到其中:
mkdir newsclassifier cd newsclassifier
确保您已安装 composer,然后通过运行以下命令将 rubix ml 添加到您的项目中:
composer require rubix/ml
要从项目的 src 目录自动加载类,请打开或创建一个composer.json 文件并添加以下配置:
{ "autoload": { "psr-4": { "newsclassifier\": "src/" } }, "require": { "rubix/ml": "^2.5" } }
这告诉 composer 自动加载 newsclassifier 命名空间下 src 文件夹中的任何类。
添加自动加载配置后,运行以下命令重新生成 composer 的自动加载器:
composer dump-autoload
您的项目目录应如下所示:
newsclassifier/ ├── src/ │ ├── classification.php │ └── train.php ├── storage/ ├── vendor/ ├── composer.json └── composer.lock
在 src/ 中,创建一个名为 classification.php 的文件。该文件将包含训练模型和预测新闻类别的方法。
<?php namespace newsclassifier; use rubixmlclassifiersknearestneighbors; use rubixmldatasetslabeled; use rubixmldatasetsunlabeled; use rubixmlpersistentmodel; use rubixmlpipeline; use rubixmltokenizersword; use rubixmltransformerstfidftransformer; use rubixmltransformerswordcountvectorizer; use rubixmlpersistersfilesystem; class classification { private $modelpath; public function __construct($modelpath) { $this->modelpath = $modelpath; } public function train() { // sample data and corresponding labels $samples = [ ['the team played an amazing game of soccer'], ['the new programming language has been released'], ['the match between the two teams was incredible'], ['the new tech gadget has been launched'], ]; $labels = [ 'sports', 'technology', 'sports', 'technology', ]; // create a labeled dataset $dataset = new labeled($samples, $labels); // set up the pipeline with a text transformer and k-nearest neighbors classifier $estimator = new pipeline([ new wordcountvectorizer(10000, 1, 1, new word()), new tfidftransformer(), ], new knearestneighbors(4)); // train the model $estimator->train($dataset); // save the model $this->savemodel($estimator); echo "training completed and model saved.n"; } private function savemodel($estimator) { $persister = new filesystem($this->modelpath); $model = new persistentmodel($estimator, $persister); $model->save(); } public function predict(array $samples) { // load the saved model $persister = new filesystem($this->modelpath); $model = persistentmodel::load($persister); // predict categories for new samples $dataset = new unlabeled($samples); return $model->predict($dataset); } }
此分类类包含以下方法:
在 src/ 中创建一个名为 train.php 的脚本来训练模型。
<?php require __dir__ . '/../vendor/autoload.php'; use newsclassifierclassification; // define the model path $modelpath = __dir__ . '/../storage/model.rbx'; // initialize the classification object $classifier = new classification($modelpath); // train the model and save it $classifier->train();
运行此脚本来训练模型:
php src/train.php
如果成功,您将看到:
training completed and model saved.
在 src/ 中创建另一个脚本,predict.php,根据训练的模型对新文章进行分类。
<?php require __dir__ . '/../vendor/autoload.php'; use newsclassifierclassification; // define the path to the saved model $modelpath = __dir__ . '/../storage/model.rbx'; // initialize the classification object $classifier = new classification($modelpath); // define new samples for classification $samples = [ ['the team played an amazing game of soccer, showing excellent teamwork and strategy.'], ['the latest programming language release introduces features that enhance coding efficiency.'], ['an incredible match between two top teams ended in a thrilling draw last night.'], ['this new tech gadget includes features never before seen, setting a new standard in the industry.'], ]; // predict categories $predictions = $classifier->predict($samples); // display predictions foreach ($predictions as $index => $prediction) { echo "sample: " . $samples[$index][0] . "n"; echo "prediction: " . $prediction . "nn"; }
运行预测脚本对样本进行分类:
php src/predict.php
输出应显示每个示例文本及其预测类别。
通过本指南,您已经使用 rubix ml 在 php 中成功构建了一个简单的新闻分类器!这展示了 php 如何比您想象的更加通用,为文本分类、推荐系统等任务引入机器学习功能。该项目的完整代码可在 github 上获取。
尝试不同的算法或数据来扩展分类器。谁知道 php 可以进行机器学习?现在你知道了。
快乐编码!