[英]Phpml\Exception\MatrixException Message: Matrix is singular
我正在制作一個程序,該程序將使用 php-ml 從數據庫中預測明年的收藏。
我收到這個錯誤。
Phpml\\Exception\\MatrixException 消息:矩陣是單數
我正在使用這個功能
使用 PHP\\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();
}
我也有這個問題,但我能夠解決它。 確保您沒有以下內容:
少於兩個數據。 經過反復試驗,我發現它至少需要 2 個數據。
格式錯誤。 確保遵循目標和示例的正確格式(請參閱文檔)。
$samples = [[60], [61], [62], [63], [65]];
$targets = [3.1, 3.6, 3.8, 4, 4.1];
$regression = new LeastSquares();
$regression->train($samples, $targets);
正如您在$samples
看到的,它是一個數組數組。 因此,請確保數組中的每個值都是一個數組本身。
當數據集屬性的所有值在所有記錄中都相似時,就會出現問題。
$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
而不是2 x 4
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.