簡體   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