我制作了一个预测明年收藏的节目 数据库使用php-ml。
我收到此错误。
Phpml \ Exception \ MatrixException消息:Matrix是单数
我正在使用这个功能
使用Phpml \ Regression \ LeastSquares;
使用\ Phpml \ Math \ Matrix;
使用\ Phpml \ Math \ Set;
新手在这里。
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
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();
}
答案 0 :(得分:0)
我也有这个问题,但我能够解决。确保您没有以下内容:
少于两个数据。经过反复试验,我发现它至少需要2个数据。
格式错误。请确保遵循目标和样本的正确格式(请参见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
中所看到的,它是一个数组数组。因此,请确保数组中的每个值都是数组本身。
答案 1 :(得分:0)
当所有记录中的数据集属性的所有值都相似时,就会出现此问题。
$samples = [ [1000,3,145], [1000,5,135], [1000,4,143], [1000,3,123]];
$targets = [ 4, 1, 3, 2];
$regression->train($samples, $targets);
在上面的示例中,所有记录的第一值ar等于1000
。因此,在执行$regression->train($samples, $targets)
时,它会看到$sample
的属性计数是2
而不是3
,这在数组维度之间造成了不匹配,该数组维度是3 x 4
而不是{ {1}}。