thominj / data-miner
一组用于PHP中常见数据挖掘操作的工具。
v1.0.0
2014-08-17 15:09 UTC
Requires
- php: >=5.4.0
Requires (Dev)
- phpunit/phpunit: >=4.1.0
This package is not auto-updated.
Last update: 2024-09-24 03:02:55 UTC
README
#DataMiner
Data Miner是一组用PHP编写的数据挖掘工具。它设计用于易于与现有网站集成。
##安装
您可以使用 Composer 非常容易地安装Data Miner。
在您的 composer.json 文件的 require 部分包含以下内容
"require": {
"thominj/data-miner": "1.*"
}
然后运行
composer install
最后,在您的项目中包含 composer 自动加载文件
require_once(PATH_TO_VENDOR_DIR.'/autoload.php');
##直方图
直方图通过计算给定范围内值出现的次数来对数据进行分箱。分箱范围由一个数组给出,其中每个值对应于范围的左端点(包含)。最后一个值是最后一个分箱的右端点。
例如
$bins = [ 0, 1, 2, 3]; $data = [ -1, 0, 0.5, 1, 3, 4 ]; $histogram = new thominj\DataMiner\Histogram($bins, $data); $result = $histogram->getResult(); print_r($result);
这将输出
Array
(
[less] => 1
[0] => 2
[1] => 1
[2] => 0
[3] => 2
)
标签为 '3' 的最后一个分箱包含所有大于或等于 3 的值。
###设置分箱和添加数据
您可以通过多种不同的方式设置分箱和添加数据。这些都会得到相同的结果
// Set bins and add data in the constructor $histogram = new thominj\DataMiner\Histogram($bins, $data); // Set bins in the constructor, add data later $histogram = new thominj\DataMiner\Histogram($bins); $histogram->addData($data); // Set bins and add data after instancing the histogram $histogram = new thominj\DataMiner\Histogram(); $histogram->setBins($bins); $histogram->addData($data);
###在线模式
您还可以以在线模式运行,以重复添加数据集。直方图将在数据到来时对其进行排序,累积结果,并允许您随时检查它们
// Online mode $histogram = new thominj\DataMiner\Histogram($bins); // Add some data $histogram->addData($data); // Get the result print_r($histogram->getResult()); // Add some more data $histogram->addData($data); // Get the new result print_r($histogram->getResult());
这将给出
Array
(
[less] => 1
[0] => 2
[1] => 1
[2] => 0
[3] => 2
)
Array
(
[less] => 2
[0] => 4
[1] => 2
[2] => 0
[3] => 4
)
###保存和恢复
您甚至可以保存一组结果,然后在添加更多数据之前预加载它。这允许您在长时间过程中保存结果,重新加载它们,并从上次离开的地方继续。
$old_result = array( 'less' => 2, 0 => 4, 1 => 2, 2 => 0, 3 => 4 ); $histogram = new thominj\DataMiner\Histogram(); $histogram->preload($old_result); $histogram->addData($data); print_r($histogram->getResult());
这将输出
Array
(
[less] => 3
[0] => 6
[1] => 3
[2] => 0
[3] => 6
)