This time to bring you to the PHP implementation of multiple linear regression simulation curve algorithm steps in detail, PHP implementation of multiple linear regression simulation curve algorithm considerations are what, the following is the actual case, take a look.
Multivariate linear regression model: y = b1x1 + b2x2 + b3x3 + ... +bnxn;
We are based on a set of data: similar to arr_x = [[1, 2, 3, 4, 5], [6, 7, 8, 9, ten], [One, ten,, +]]; arr_y = [5,] ; The last we asked for was a number Group, including from B1 to Bn;
Methods: Using least squares method
Formula: We only use the first half of the formula, that is, the matrix to calculate
The x in the formula is arr_x, a two-dimensional array we can think of as a matrix, the y in the formula is arr_y, and it is considered a matrix (5, 10, 15), but it should be written vertically.
Then we can find that the matrix is multiplied, transpose, and inverse by the formula, so the following code gives:
Public Function Get_complement ($data, $i, $j) {/* x and Y are the number of rows and columns of the matrix data */$x = count ($data); $y = count ($data [0]); /* Data2 for the remaining matrix */$data 2 =[]; for ($k = 0; $k < $x-1; $k + +) {if ($k < $i) {for ($kk = 0; $kk < $y-1; $kk + +) {if ($kk < $ j) {$data 2[$k] [$KK] = $data [$k] [$KK]; } else {$data 2[$k] [$KK] = $data [$k] [$kk +1]; }}} else {for ($kk = 0; $kk < $y-1; $kk + +) {if ($kk < $j) {$data 2[$k] [$KK] = $dat a[$k +1][$kk]; } else {$data 2[$k] [$KK] = $data [$k +1][$kk +1]; }}}} return $data 2;} /* Calculate matrix determinant */public function Cal_det ($data) {$ans = 0; if (count ($data [0]) = = = 2) {$ans = $data [0][0] * $data [1][1]-$data [0][1] * $data [1][0]; } else {for ($i = 0; $i < count ($data [0]), $i + +) {$data _temp = $this->get_complement ($data, 0, $i); if ($i% = = = 0) {$ans = $ans + $data [0][$i] * ($this->cal_det ($data _temp)); }else {$ans = $ans-$data [0][$i] * ($this->cal_det ($data _temp)); }}} return $ans;} /* The adjoint matrix of the computed matrix */public function Ajoint ($data) {$m = count ($data); $n = count ($data [0]); $data 2 =[]; for ($i = 0; $i < $m, $i + +) {for ($j = 0; $j < $n; $j + +) {if (($i + $j)% 2 = = = 0) {$data 2[$i] [$j] = $this->cal_det ($this->get_complement ($data, $i, $j)); } else {$data 2[$i] [$j] =-$this->cal_det ($this->get_complement ($data, $i, $j)); }}} return $this->trans ($data 2);} /* Transpose matrix */public function trans ($data) {$i = count ($data); $j = count ($data [0]); $data 2 =[]; for ($k 2 = 0; $k 2 < $j, $k 2++) {for ($k 1 = 0; $k 1 < $i; $k 1++) {$data 2[$k 2][$k 1] = $data [$k 1][$k 2]; }}/* Transpose The matrix to get the adjoint matrix */return $data 2;} /* For the inverse of the matrix, the input parameter is the original matrix */public function Inv ($data) {$m = count ($data); $n = count ($data [0]); $data 2 =[]; $det _val = $this->cal_det ($data); $data 2 = $this->ajoint ($data); for ($i = 0; $i < $m; $i ++) {for ($j = 0; $j < $n; $j + +) {$data 2[$i] [$j] = $data 2[$i] [$j]/$det _val; }} return $data 2;} /* For the product of two matrices */public function getproduct ($data 1, $data 2) {/* $data 1 is the left multiplicative matrix */$m 1 = count ($data 1); $n 1 = count ($data 1[0]); $m 2 = count ($data 2); $n 2 = count ($data 2[0]); $data _new =[]; if ($n 1!== $m 2) {return false; } else {for ($i = 0; $i <= $m 1-1; $i + +) {for ($k = 0; $k <= $n 2-1; $k + +) {$data _new[$i] [$k] = 0; for ($j = 0; $j <= $n 1-1; $j + +) {$data _new[$i] [$k] + = $data 1[$i] [$j] * $data 2[$j] [$k]; }}}} return $data _new;} /* Multivariate linear equation */public function getparams ($arr _x, $arr _y) {$final =[]; $arr _x_t = $this->trans ($arr _x); $result = $this->getproduct ($this->getproduct ($this->inv ($this->getproduct ($arr _x_t, $arr _x)), $arr _x_ T), $arr _y); foreach ($result as $key = + $val) {foreach ($val as $_k = $_v) {$final [] = $_v; }} return $final;}
The last getParams()
method is to find the method of the B-parameter array, pass in a two-dimensional array arr_x, and a one-dimensional array arr_y.
Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!
Recommended reading:
PHP using the foreach Transform array steps
PHP Delete a one-dimensional array of value elements steps in detail