简体   繁体   English

php-ml 回归预测奇怪的值

[英]php-ml regression predicts weird values

My input values are 1, 2, 3, 4, ... and my output values are 1*1, 2*2, 3*3, 4*4, ... My code looks like this:我的输入值是 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";

I expect it to output roughly 25 and 100. But I get:我预计它大约为 output 25 和 100。但我得到:

-1183.3333333333
-683.33333333333

I also tried to use SVR instead of LeastSquares but the values are strange too:我还尝试使用 SVR 而不是 LeastSquares,但值也很奇怪:

2498.23
2498.23

I am new to ML.我是机器学习的新手。 What am I doing wrong?我究竟做错了什么?

It looks like the code you provided is almost correct.看起来您提供的代码几乎是正确的。 There are a few issues that you need to fix in order to make it work:为了使其正常工作,您需要解决一些问题:

You need to define the LeastSquares class and implement the train and predict methods.您需要定义 LeastSquares class 并实施训练和预测方法。 You need to include the necessary dependencies (eg libraries or other PHP files).您需要包含必要的依赖项(例如库或其他 PHP 文件)。 You need to make sure that the $samples and $targets arrays are correctly initialized and filled with the desired values.您需要确保 $samples 和 $targets arrays 已正确初始化并填充了所需的值。 Here is an example of how you can fix the code to perform least squares regression in PHP:下面是一个示例,说明如何修复代码以在 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";

?>

As others have pointed out in the comments LeastSquares is for fitting a linear model to your data (training examples).正如其他人在评论中指出的那样, LeastSquares用于将线性 model 拟合到您的数据(训练示例)。

Your data set (target = samples^2) is inherently non-linear.您的数据集 (target = samples^2) 本质上是非线性的。 If you try to picture what happens when you fit the best possible (in a least square of residuals sense) line to a quadratic curve you get a negative y-intercept (a sketch of this below):如果你试图想象当你将最好的(在残差的最小二乘意义上)线拟合到二次曲线时会发生什么,你会得到一个负的 y 截距(下面的草图):

在此处输入图像描述

You've trained your linear model on data up to x=99, y=9801, which will mean you have a very large y-intercept.您已经在高达 x=99、y=9801 的数据上训练了线性 model,这意味着您有一个非常大的 y 轴截距。 So down at x=5 or x=10 you end up with a large negative value as you've found.因此,在 x=5 或 x=10 处,您最终会得到一个很大的负值,正如您所发现的那样。

If you use support vector regression with a degree-2 polynomial it will do a good job of capturing the pattern of your data:如果您使用具有 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";
?>

Returns:退货:

25.0995
100.098

From your response in the comments its clear that you're looking to apply a neural.network so that you don't have to worry about what degree of model to fit to your data.从您在评论中的回复中可以清楚地看出,您正在寻求应用神经网络,这样您就不必担心 model 的度数适合您的数据。 A neural.network with a single hidden layer can fit any continuous function arbitrarily well with enough hidden nodes, and enough training data.具有单个隐藏层的神经网络可以任意拟合任何连续的 function,具有足够的隐藏节点和足够的训练数据。

Unfortunately php-ml doesn't seem to have a MLP (multilayer perceptron - another term for a neural.network) for regression available out-of-the-box.不幸的是,php-ml 似乎没有开箱即用的 MLP(多层感知器 - 神经网络的另一个术语)回归。 I'm sure you could build one from appropriate layers but if your goal is to get up and running with training regression models quickly it might not be the best approach.我相信您可以从适当的层构建一个,但如果您的目标是快速启动和运行训练回归模型,它可能不是最好的方法。

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

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