【问题标题】:Phpml\Exception\MatrixException Message: Matrix is singularPhpml\Exception\MatrixException 消息:矩阵是奇异的
【发布时间】:2018-06-28 06:15:58
【问题描述】:

我正在制作一个程序来预测明年的收藏品 使用php-ml的数据库。

我收到了这个错误。

Phpml\Exception\MatrixException 消息:矩阵是奇异的

我正在使用这个功能

使用 Phpml\Regression\LeastSquares;

使用 \Phpml\Math\Matrix;

使用 \Phpml\Math\Set;

这里是新手。

回归控制器

public function index()
{
    $this->load->model("regression_model") ;
    $array = $this->regression_model->display_data();
    $targets = $this->regression_model->display_data2();

    $matrix = new Matrix($array);
    $set = new Set($targets);

    $arraytrix = $matrix->toArray(); 
    $arrayset = $set->toArray();

    $col[] = array_column($arraytrix, 'year');
    $col2[] = array_column($arrayset, 'total');

    var_dump($col);
    var_dump($col2);

    $regression = new LeastSquares();
    $regression->train($col, $col2);

    $predicted = $regression->predict([2018]);

    var_dump($predicted);

    $this->load->view('regression');

}

回归模型

function display_data()
{
    $query1 = $this->db->query("SELECT year from total_year");
    return $query1->result_array();

}
function display_data2()
{
    $query1 = $this->db->query("SELECT total from total_year");
    return $query1->result_array();
}

【问题讨论】:

  • 你解决了吗?

标签: php codeigniter machine-learning regression php-ml


【解决方案1】:

我也有这个问题,但我能够解决它。确保您没有以下物品:

  • 少于两个数据
  • 格式错误

少于两个数据。经过反复试验,我发现它至少需要两个数据。

格式错误。确保遵循目标和示例的正确格式(请参阅documentation)。

$samples = [[60], [61], [62], [63], [65]];
$targets = [3.1, 3.6, 3.8, 4, 4.1];

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

正如您在$samples 中看到的,它是一个数组数组。因此,请确保数组中的每个值都是数组本身。

【讨论】:

    【解决方案2】:

    当数据集属性的所有值在所有记录中都相似时,就会出现问题。

    $samples = [ [1000,3,145], [1000,5,135], [1000,4,143], [1000,3,123]];
    
    $targets = [ 4, 1, 3, 2];
    
    $regression->train($samples, $targets);
    

    在上面的示例中,所有记录的第一个值都等于1000。因此,当执行$regression->train($samples, $targets) 时,它会看到$sample 的属性计数是2 而不是3,这会在3 x 4 而不是2 x 4 的数组维度之间造成不匹配。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-25
      • 2018-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多