search
HomeBackend DevelopmentPHP TutorialSimple testing and usage sharing of php-ml
Simple testing and usage sharing of php-mlFeb 07, 2018 am 10:24 AM
phpInstructions

php-ml is a machine learning library written in PHP. Although we know that python or C++ provide more machine learning libraries, in fact, most of them are slightly complicated, and many novices feel hopeless when configuring them. This article mainly brings you a simple test and usage method of the PHP machine learning library php-ml. The editor thinks it is quite good, so I will share it with you now and give it as a reference for everyone. Let’s follow the editor to take a look, I hope it can help everyone.

php-ml Although this machine learning library does not have particularly advanced algorithms, it has the most basic machine learning, classification and other algorithms. Our small company can do some simple data analysis, prediction, etc. Enough. In our projects, what we pursue should be cost-effectiveness, not excessive efficiency and precision. Some algorithms and libraries look very powerful, but if we consider going online quickly and our technical staff have no experience in machine learning, complex code and configuration will actually drag down our project. And if we are making a simple machine learning application, then the learning cost of studying complex libraries and algorithms is obviously a bit high. Moreover, if the project encounters strange problems, can we solve them? What should I do if my needs change? I believe everyone has had this experience: while working, the program suddenly reported an error, and I couldn't figure out the reason. I searched on Google or Baidu and found only one question that met the conditions. It was asked five or ten years ago. , and then zero reply. . .

Therefore, it is necessary to choose the simplest, most efficient and most cost-effective method. The speed of php-ml is not slow (change to php7 quickly), and the accuracy is also good. After all, the algorithms are the same, and php is based on c. What bloggers dislike the most is comparing the performance and scope of application between Python, Java and PHP. If you really want performance, please develop in C. If you really want to pursue the scope of application, please use C or even assembly. . .

First of all, if we want to use this library, we need to download it first. This library file can be downloaded from github (https://github.com/php-ai/php-ml). Of course, it is more recommended to use composer to download the library and configure it automatically.

After downloading, we can take a look at the documentation of this library. The documents are some simple examples. We can create a file ourselves and try it out. All are easy to understand. Next, let's test it on actual data. One of the data sets is the data set of Iris stamens, and the other is due to the loss of records, so I don’t know what the data is about. . .

Iris stamen data has three different categories:

Unknown data set, the decimal point is marked as a comma, so when calculating Still need to process:

# Let’s deal with the unknown data set first. First, the file name of our unknown dataset is data.txt. This data set can just be drawn into an x-y line chart first. Therefore, we first draw the original data into a line chart. Since the x-axis is relatively long, we only need to see its rough shape:

The jpgraph library of php is used for drawing. The code is as follows:

<?php include_once &#39;./src/jpgraph.php&#39;;
include_once &#39;./src/jpgraph_line.php&#39;;

$g = new Graph(1920,1080);//jpgraph的绘制操作
$g->SetScale("textint");
$g->title->Set('data');

//文件的处理
$file = fopen('data.txt','r');
$labels = array();
while(!feof($file)){
 $data = explode(' ',fgets($file));  
 $data[1] = str_replace(',','.',$data[1]);//数据处理,将数据中的逗号修正为小数点
 $labels[(int)$data[0]] = (float)$data[1];//这里将数据以键值的方式存入数组,方便我们根据键来排序
} 

ksort($labels);//按键的大小排序

$x = array();//x轴的表示数据
$y = array();//y轴的表示数据
foreach($labels as $key=>$value){
 array_push($x,$key);
 array_push($y,$value);
}


$linePlot = new LinePlot($y);
$g->xaxis->SetTickLabels($x); 
$linePlot->SetLegend('data');
$g->Add($linePlot);
$g->Stroke();

With this original image for comparison, we will study next. We use LeastSquars in php-ml for learning. The output of our test needs to be saved in a file so that we can draw a comparison chart. The learning code is as follows:

<?php  require &#39;vendor/autoload.php&#39;;

 use Phpml\Regression\LeastSquares;
 use Phpml\ModelManager;

 $file = fopen(&#39;data.txt&#39;,&#39;r&#39;);
 $samples = array();
 $labels = array();
 $i = 0;
 while(!feof($file)){
  $data = explode(&#39; &#39;,fgets($file));
  $samples[$i][0] = (int)$data[0];
  $data[1] = str_replace(&#39;,&#39;,&#39;.&#39;,$data[1]);
  $labels[$i] = (float)$data[1];
  $i ++;
 } 
 fclose($file);

 $regression = new LeastSquares();
 $regression->train($samples,$labels);

 //这个a数组是根据我们对原数据处理后的x值给出的,做测试用。
 $a = [0,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,20,22,23,24,25,26,27,29,30,31,37,40,41,45,48,53,55,57,60,61,108,124];
 for($i = 0; $i predict([$a[$i]]))."\n",FILE_APPEND); //以追加的方式存入文件  
 }

After that, we will read out the data stored in the file, draw a graph, and first paste the final rendering:

Code As follows:

<?php include_once &#39;./src/jpgraph.php&#39;;
include_once &#39;./src/jpgraph_line.php&#39;;

$g = new Graph(1920,1080);
$g->SetScale("textint");
$g->title->Set('data');

$file = fopen('putput.txt','r');
$y = array();
$i = 0;
while(!feof($file)){
 $y[$i] = (float)(fgets($file));
 $i ++;   
} 

$x = [0,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,20,22,23,24,25,26,27,29,30,31,37,40,41,45,48,53,55,57,60,61,108,124];

$linePlot = new LinePlot($y);
$g->xaxis->SetTickLabels($x); 
$linePlot->SetLegend('data');
$g->Add($linePlot);
$g->Stroke();

It can be found that the graphics discrepancy is still relatively large, especially in the parts with more jagged graphics. However, this is 40 sets of data after all, and we can see that the general graphic trends are consistent. When general libraries do this kind of learning, the accuracy is very low when the amount of data is low. To achieve relatively high accuracy, a large amount of data is required, and more than 10,000 pieces of data are necessary. If this data requirement cannot be met, then any library we use will be in vain. Therefore, in the practice of machine learning, the real difficulty is not technical problems such as low accuracy and complex configuration, but insufficient data volume or too low quality (too much useless data in a set of data). Before doing machine learning, pre-processing of data is also necessary.

Next, let’s test the stamen data. There are three categories in total. Since we downloaded csv data, we can use the official method of operating csv files provided by php-ml. This is a classification problem, so we choose the SVC algorithm provided by the library for classification. We set the file name of the stamen data as Iris.csv, and the code is as follows:

<?php require &#39;vendor/autoload.php&#39;;

use Phpml\Classification\SVC;
use Phpml\SupportVectorMachine\Kernel;
use Phpml\Dataset\CsvDataset;

$dataset = new CsvDataset(&#39;Iris.csv&#39; , 4, false);
$classifier = new SVC(Kernel::LINEAR,$cost = 1000);
$classifier->train($dataset->getSamples(),$dataset->getTargets());

echo $classifier->predict([$argv[1],$argv[2],$argv[3],$argv[4]]);//$argv是命令行参数,调试这种程序使用命令行较方便

是不是很简单?短短12行代码就搞定了。接下来,我们来测试一下。根据我们上面贴出的图,当我们输入5 3.3 1.4 0.2的时候,输出应该是Iris-setosa。我们看一下:

看,至少我们输入一个原来就有的数据,得到了正确的结果。但是,我们输入原数据集中没有的数据呢?我们来测试两组:

由我们之前贴出的两张图的数据看,我们输入的数据在数据集中并不存在,但分类按照我们初步的观察来看,是合理的。

所以,这个机器学习库对于大多数的人来说,都是够用的。而大多数鄙视这个库鄙视那个库,大谈性能的人,基本上也不是什么大牛。真正的大牛已经忙着捞钱去了,或者正在做学术研究等等。我们更多的应该是掌握算法,了解其中的道理和玄机,而不是夸夸其谈。当然,这个库并不建议用在大型项目上,只推荐小型项目或者个人项目等。

jpgraph只依赖GD库,所以下载引用之后就可以使用,大量的代码都放在了绘制图形和初期的数据处理上。由于库的出色封装,学习代码并不复杂。需要所有代码或者测试数据集的小伙伴可以留言或者私信等,我提供完整的代码,解压即用。

相关推荐:

PHP机器学习库php-ml的实例教程

The above is the detailed content of Simple testing and usage sharing of php-ml. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
php如何使用PHP的Intl扩展?php如何使用PHP的Intl扩展?May 31, 2023 pm 08:10 PM

PHP的Intl扩展是一个非常实用的工具,它提供了一系列国际化和本地化的功能。本文将介绍如何使用PHP的Intl扩展。一、安装Intl扩展在开始使用Intl扩展之前,需要安装该扩展。在Windows下,可以在php.ini文件中打开该扩展。在Linux下,可以通过命令行安装:Ubuntu/Debian:sudoapt-getinstallphp7.4-

如何使用CakePHP中的数据库查询构造器?如何使用CakePHP中的数据库查询构造器?Jun 04, 2023 am 09:02 AM

CakePHP是一个开源的PHPMVC框架,它广泛用于Web应用程序的开发。CakePHP具有许多功能和工具,其中包括一个强大的数据库查询构造器,用于交互性能数据库。该查询构造器允许您使用面向对象的语法执行SQL查询,而不必编写繁琐的SQL语句。本文将介绍如何使用CakePHP中的数据库查询构造器。建立数据库连接在使用数据库查询构造器之前,您首先需要在Ca

php如何使用CI框架?php如何使用CI框架?Jun 01, 2023 am 08:48 AM

随着网络技术的发展,PHP已经成为了Web开发的重要工具之一。而其中一款流行的PHP框架——CodeIgniter(以下简称CI)也得到了越来越多的关注和使用。今天,我们就来看看如何使用CI框架。一、安装CI框架首先,我们需要下载CI框架并安装。在CI的官网(https://codeigniter.com/)上下载最新版本的CI框架压缩包。下载完成后,解压缩

php如何使用PHP的Ctype扩展?php如何使用PHP的Ctype扩展?Jun 03, 2023 pm 10:40 PM

PHP是一种非常受欢迎的编程语言,它允许开发者创建各种各样的应用程序。但是,有时候在编写PHP代码时,我们需要处理和验证字符。这时候PHP的Ctype扩展就可以派上用场了。本文将就如何使用PHP的Ctype扩展展开介绍。什么是Ctype扩展?PHP的Ctype扩展是一个非常有用的工具,它提供了各种函数来验证字符串中的字符类型。这些函数包括isalnum、is

Vue 中的单文件组件是什么,如何使用?Vue 中的单文件组件是什么,如何使用?Jun 10, 2023 pm 11:10 PM

作为一种流行的前端框架,Vue能够提供开发者一个便捷高效的开发体验。其中,单文件组件是Vue的一个重要概念,使用它能够帮助开发者快速构建整洁、模块化的应用程序。在本文中,我们将介绍单文件组件是什么,以及如何在Vue中使用它们。一、单文件组件是什么?单文件组件(SingleFileComponent,简称SFC)是Vue中的一个重要概念,它

php如何使用PHP的geoip扩展?php如何使用PHP的geoip扩展?Jun 01, 2023 am 09:13 AM

PHP是一种流行的服务器端脚本语言,它可以处理网页上的动态内容。PHP的geoip扩展可以让你在PHP中获取有关用户位置的信息。在本文中,我们将介绍如何使用PHP的geoip扩展。什么是PHP的GeoIP扩展?PHP的geoip扩展是一个免费的、开源的扩展,它允许你获取有关IP地址和位置信息的数据。该扩展可以与GeoIP数据库一起使用,这是一个由MaxMin

php如何使用PHP的DOM扩展?php如何使用PHP的DOM扩展?May 31, 2023 pm 06:40 PM

PHP的DOM扩展是一种基于文档对象模型(DOM)的PHP库,可以对XML文档进行创建、修改和查询操作。该扩展可以使PHP语言更加方便地处理XML文件,让开发者可以快速地实现对XML文件的数据分析和处理。本文将介绍如何使用PHP的DOM扩展。安装DOM扩展首先需要确保PHP已经安装了DOM扩展,如果没有安装需要先安装。在Linux系统中,可以使用以下命令来安

php如何使用CI4框架?php如何使用CI4框架?Jun 01, 2023 pm 02:40 PM

PHP是一种广泛使用的服务器端脚本语言,而CodeIgniter4(CI4)是一个流行的PHP框架,它提供了一种快速而优秀的方法来构建Web应用程序。在这篇文章中,我们将通过引导您了解如何使用CI4框架,来使您开始使用此框架来开发出众的Web应用程序。1.下载并安装CI4首先,您需要从官方网站(https://codeigniter.com/downloa

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools