[英]Phpml\Exception\MatrixException Message: Matrix is singular
I'm making a program that will predict the next year's collection from the database using php-ml.我正在制作一个程序,该程序将使用 php-ml 从数据库中预测明年的收藏。
and I'm getting this error.我收到这个错误。
Phpml\\Exception\\MatrixException Message: Matrix is singular Phpml\\Exception\\MatrixException 消息:矩阵是单数
Im using this functions我正在使用这个功能
use Phpml\\Regression\\LeastSquares;使用 PHP\\Regression\\LeastSquares;
use \\Phpml\\Math\\Matrix;使用 \\Phpml\\Math\\Matrix;
use \\Phpml\\Math\\Set;使用 \\Phpml\\Math\\Set;
newbie here.新手在这里。
Regression_controller Regression_controller
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');
}
Regression_model Regression_model
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();
}
I also had this problem but I was able to solve it.我也有这个问题,但我能够解决它。 Make sure that you don't have the following:确保您没有以下内容:
Less Than Two Data.少于两个数据。 Upon trial and error, I've found out that it needs at least 2 data.经过反复试验,我发现它至少需要 2 个数据。
Wrong Format.格式错误。 Make sure to follow the proper formatting of the target and sample (see the documentation ).确保遵循目标和示例的正确格式(请参阅文档)。
$samples = [[60], [61], [62], [63], [65]];
$targets = [3.1, 3.6, 3.8, 4, 4.1];
$regression = new LeastSquares();
$regression->train($samples, $targets);
As you can see in $samples
, it's an array of arrays.正如您在$samples
看到的,它是一个数组数组。 So, make sure that each value in your array is an array itself.因此,请确保数组中的每个值都是一个数组本身。
The problem arises when all the values of a dataset attribute are similar in all records.当数据集属性的所有值在所有记录中都相似时,就会出现问题。
$samples = [ [1000,3,145], [1000,5,135], [1000,4,143], [1000,3,123]];
$targets = [ 4, 1, 3, 2];
$regression->train($samples, $targets);
In the above sample, the first values of all records ar equal to 1000
.在上面的示例中,所有记录的第一个值 ar 等于1000
。 Therefore, when $regression->train($samples, $targets)
is executed it sees attribute count of $sample
is 2
instead of 3
, which creates a mismatch between array dimension which is 3 x 4
not 2 x 4
.因此,当执行$regression->train($samples, $targets)
,它看到$sample
属性计数是2
而不是3
,这会造成数组维度之间的不匹配,即3 x 4
而不是2 x 4
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.