国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

java回歸中的殘差平方和的均值

2019-11-11 06:05:06
字體:
來源:轉載
供稿:網友

以下,是我在寫一個算法時中的一小部分,程序給了一個簡單的算例。

計算殘差平方和的均值。 以下是殘差平方和公式。 這里寫圖片描述

package sampling.method;import java.util.HashMap;import java.util.Map;import org.apache.commons.math3.linear.Array2DRowRealMatrix;import org.apache.commons.math3.linear.RealMatrix;public class Test { public static void main(String[] args) { Map<Integer,RealMatrix > ymatrix=new HashMap<Integer,RealMatrix >(); double[][] y1={{1,2,3,4}}; RealMatrix y1matrix = new Array2DRowRealMatrix(y1); double[][] y2={{2,3,3,5,7}}; RealMatrix y2matrix = new Array2DRowRealMatrix(y2); //這里的1和2分別表示類別 ymatrix.put(1, y1matrix); ymatrix.put(2, y2matrix); // Map<Integer,RealMatrix > xMatrixList=new HashMap<Integer,RealMatrix >(); double[][] x1={{1,2},{3,4},{2,4},{2,4}}; double[][] x2={{1,2},{3,4},{0,0},{5,4},{2,6}}; RealMatrix x1mat = new Array2DRowRealMatrix(x1); RealMatrix x2mat = new Array2DRowRealMatrix(x2); xMatrixList.put(1, x1mat); xMatrixList.put(2, x2mat); // Map<Integer,RealMatrix > wMatrixList=new HashMap<Integer,RealMatrix >(); double[][] w1={{1,2}}; double[][] w2={{2,3}}; RealMatrix w1mat = new Array2DRowRealMatrix(w1); RealMatrix w2mat = new Array2DRowRealMatrix(w2); wMatrixList.put(1, w1mat); wMatrixList.put(2, w2mat); double sum=0.0; int dimension=0; for( int itemnumber : ymatrix.keySet() ){ RealMatrix yscore=ymatrix.get(itemnumber) ; dimension+=yscore.getColumnDimension(); RealMatrix wxscore=wMatrixList.get(itemnumber).multiply(xMatrixList.get(itemnumber).transpose()) ; RealMatrix ysbuwxscore =yscore.add(wxscore.scalarMultiply(-1.0)); System.out.PRintln(yscore.add(wxscore.scalarMultiply(-1.0))); System.out.println(ysbuwxscore.multiply(ysbuwxscore.transpose())); sum+=sumarray(ysbuwxscore.multiply(ysbuwxscore.transpose())); } System.out.println("殘差平方和均值為:"+sum/dimension); } private static double sumarray(RealMatrix a){ double[][] arr=a.getData(); double sum = 0.0; for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr[i].length; j++) { sum += arr[i][j]; } } return sum; }}

這里寫圖片描述


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 禹城市| 丁青县| 吉木乃县| 普兰县| 临夏市| 乡宁县| 合水县| 黔江区| 滕州市| 富平县| 海城市| 北川| 定州市| 岳阳市| 衡阳市| 荥阳市| 铜鼓县| 丰宁| 射洪县| 五常市| 大方县| 休宁县| 钦州市| 盘山县| 翼城县| 九台市| 隆尧县| 红安县| 合川市| 莱芜市| 诸城市| 忻州市| 泽州县| 金昌市| 府谷县| 台北县| 吴江市| 平原县| 永定县| 岳池县| 揭西县|