繁体   English   中英

php-ml 回归预测奇怪的值

[英]php-ml regression predicts weird values

我的输入值是 1、2、3、4、……而我的 output 值是 1*1、2*2、3*3、4*4、……我的代码如下所示:

$reg = new LeastSquares();

$samples = array();
$targets = array();
for ($i = 1; $i < 100; $i++)
{  
  $samples[] = [$i];
  $targets[] = $i*$i;
}

$reg->train($samples, $targets);
  
echo $reg->predict([5])."\n";
echo $reg->predict([10])."\n";

我预计它大约为 output 25 和 100。但我得到:

-1183.3333333333
-683.33333333333

我还尝试使用 SVR 而不是 LeastSquares,但值也很奇怪:

2498.23
2498.23

我是机器学习的新手。 我究竟做错了什么?

看起来您提供的代码几乎是正确的。 为了使其正常工作,您需要解决一些问题:

您需要定义 LeastSquares class 并实施训练和预测方法。 您需要包含必要的依赖项(例如库或其他 PHP 文件)。 您需要确保 $samples 和 $targets arrays 已正确初始化并填充了所需的值。 下面是一个示例,说明如何修复代码以在 PHP 中执行最小二乘回归:

<?php

// include necessary dependencies
require_once 'Matrix.php';

class LeastSquares
{
// solve least squares problem using normal equations
public function train($samples, $targets)
{
// number of samples
$m = count($samples);

// number of features
$n = count($samples[0]);

// add ones column to samples
for ($i = 0; $i < $m; $i++)
  array_unshift($samples[$i], 1);

// transpose samples
$samples_t = Matrix::transpose($samples);

// compute dot product
$dot = Matrix::dot($samples_t, $samples);

// invert dot product
$inv = Matrix::inv($dot);

// compute dot product
$dot = Matrix::dot($inv, $samples_t);

// compute weights
$this->weights = Matrix::dot($dot, $targets);
}

// make a prediction
public function predict($sample)
{
// add ones to sample
array_unshift($sample, 1);

// compute prediction
return Matrix::dot($this->weights, $sample);
}
}

// create instance of LeastSquares class
$reg = new LeastSquares();

// create arrays of samples and targets
$samples = array();
$targets = array();
for ($i = 1; $i < 100; $i++)
 {
$samples[] = [$i];
$targets[] = $i*$i;
}

// fit a linear model to the samples using least squares
$reg->train($samples, $targets);

 // predict the output for two new samples
echo $reg->predict([5])."\n";
echo $reg->predict([10])."\n";

?>

正如其他人在评论中指出的那样, LeastSquares用于将线性 model 拟合到您的数据(训练示例)。

您的数据集 (target = samples^2) 本质上是非线性的。 如果你试图想象当你将最好的(在残差的最小二乘意义上)线拟合到二次曲线时会发生什么,你会得到一个负的 y 截距(下面的草图):

在此处输入图像描述

您已经在高达 x=99、y=9801 的数据上训练了线性 model,这意味着您有一个非常大的 y 轴截距。 因此,在 x=5 或 x=10 处,您最终会得到一个很大的负值,正如您所发现的那样。

如果您使用具有 2 次多项式的支持向量回归,它将很好地捕获数据模式:

<?php
require_once __DIR__ . '/vendor/autoload.php';
use Phpml\Regression\SVR;
use Phpml\SupportVectorMachine\Kernel;

$samples = array();
$targets = array();
for ($i = 1; $i <= 100; $i++)
{  
  $samples[] = [$i];
  $targets[] = $i*$i;
}

$reg = new SVR(Kernel::POLYNOMIAL, $degree = 2);
$reg->train($samples, $targets);

echo $reg->predict([5])."\n";
echo $reg->predict([10])."\n";
?>

退货:

25.0995
100.098

从您在评论中的回复中可以清楚地看出,您正在寻求应用神经网络,这样您就不必担心 model 的度数适合您的数据。 具有单个隐藏层的神经网络可以任意拟合任何连续的 function,具有足够的隐藏节点和足够的训练数据。

不幸的是,php-ml 似乎没有开箱即用的 MLP(多层感知器 - 神经网络的另一个术语)回归。 我相信您可以从适当的层构建一个,但如果您的目标是快速启动和运行训练回归模型,它可能不是最好的方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM