PHP 中的机器学习:使用 Rubix ML 构建新闻分类器
来源:dev.to
时间:2024-11-06 22:16:03 216浏览 收藏
各位小伙伴们,大家好呀!看看今天我又给各位带来了什么文章?本文标题是《PHP 中的机器学习:使用 Rubix ML 构建新闻分类器》,很明显是关于文章的文章哈哈哈,其中内容主要会涉及到等等,如果能帮到你,觉得很不错的话,欢迎各位多多点评和分享!

介绍
机器学习无处不在——推荐电影、标记图像,现在甚至对新闻文章进行分类。想象一下如果您可以在 php 中做到这一点!借助 rubix ml,您可以以简单易懂的方式将机器学习的强大功能引入 php。本指南将引导您构建一个简单的新闻分类器,将文章分类为“体育”或“技术”等类别。最后,您将拥有一个工作分类器,可以根据新文章的内容预测其类别。
这个项目非常适合想要使用 php 进行机器学习的初学者,您可以按照 github 上的完整代码进行操作。
目录
- 什么是 rubix ml?
- 设置项目
- 创建新闻分类类
- 训练模型
- 预测新样本
- 最后的想法
rubix 机器学习是什么?
rubix ml 是一个 php 机器学习库,它将 ml 工具和算法引入 php 友好的环境中。无论您从事分类、回归、聚类,甚至自然语言处理,rubix ml 都能满足您的需求。它允许您加载和预处理数据、训练模型并评估性能——所有这些都在 php 中进行。
rubix ml 支持广泛的机器学习任务,例如:
- 分类:对数据进行分类,例如将电子邮件标记为垃圾邮件或非垃圾邮件。
- 回归:预测连续值,例如房价。
- 聚类:对没有标签的数据进行分组,就像寻找客户群一样。
- 自然语言处理 (nlp):处理文本数据,例如标记并将其转换为 ml 可用的格式。
让我们深入了解如何使用 rubix ml 在 php 中构建简单的新闻分类器!
设置项目
我们将首先使用 rubix ml 设置一个新的 php 项目并配置自动加载。
第1步:初始化项目目录
创建一个新的项目目录并导航到其中:
mkdir newsclassifier cd newsclassifier
第 2 步:安装 rubix ml 和 composer
确保您已安装 composer,然后通过运行以下命令将 rubix ml 添加到您的项目中:
composer require rubix/ml
步骤3:在composer.json中配置自动加载
要从项目的 src 目录自动加载类,请打开或创建一个composer.json 文件并添加以下配置:
{
"autoload": {
"psr-4": {
"newsclassifier\\": "src/"
}
},
"require": {
"rubix/ml": "^2.5"
}
}
这告诉 composer 自动加载 newsclassifier 命名空间下 src 文件夹中的任何类。
第 4 步:运行 composer autoload dump
添加自动加载配置后,运行以下命令重新生成 composer 的自动加载器:
composer dump-autoload
第5步:目录结构
您的项目目录应如下所示:
newsclassifier/ ├── src/ │ ├── classification.php │ └── train.php ├── storage/ ├── vendor/ ├── composer.json └── composer.lock
- src/:包含您的 php 脚本。
- storage/:训练后的模型的保存位置。
- vendor/:包含 composer 安装的依赖项。
创建新闻分类类
在 src/ 中,创建一个名为 classification.php 的文件。该文件将包含训练模型和预测新闻类别的方法。
<?php
namespace newsclassifier;
use rubix\ml\classifiers\knearestneighbors;
use rubix\ml\datasets\labeled;
use rubix\ml\datasets\unlabeled;
use rubix\ml\persistentmodel;
use rubix\ml\pipeline;
use rubix\ml\tokenizers\word;
use rubix\ml\transformers\tfidftransformer;
use rubix\ml\transformers\wordcountvectorizer;
use rubix\ml\persisters\filesystem;
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 newsclassifier\classification; // 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 newsclassifier\classification;
// 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 . "\n\n";
}
运行预测脚本对样本进行分类:
php src/predict.php
输出应显示每个示例文本及其预测类别。
最后的想法
通过本指南,您已经使用 rubix ml 在 php 中成功构建了一个简单的新闻分类器!这展示了 php 如何比您想象的更加通用,为文本分类、推荐系统等任务引入机器学习功能。该项目的完整代码可在 github 上获取。
尝试不同的算法或数据来扩展分类器。谁知道 php 可以进行机器学习?现在你知道了。
快乐编码!
今天关于《PHP 中的机器学习:使用 Rubix ML 构建新闻分类器》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注golang学习网公众号!
-
501 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
267 收藏
-
160 收藏
-
355 收藏
-
113 收藏
-
188 收藏
-
403 收藏
-
401 收藏
-
136 收藏
-
242 收藏
-
347 收藏
-
440 收藏
-
500 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 485次学习