什么站做咨询网站好,互联网专业主要学什么,合肥建设网站查询,郑州网络推广平台有哪些算法步骤:1)将原始数据按列组成n行m列矩阵X2)特征中心化。即每一维的数据都减去该维的均值#xff0c;使每一维的均值都为03)求出协方差矩阵4)求出协方差矩阵的特征值及对应的特征向量5)将特征向量按对应的特征值大小从上往下按行排列成矩阵#xff0c;取前k行组成矩阵p6)YPX…算法步骤:1)将原始数据按列组成n行m列矩阵X2)特征中心化。即每一维的数据都减去该维的均值使每一维的均值都为03)求出协方差矩阵4)求出协方差矩阵的特征值及对应的特征向量5)将特征向量按对应的特征值大小从上往下按行排列成矩阵取前k行组成矩阵p6)YPX 即为降维到k维后的数据
PCA /** 算法步骤:* 1)将原始数据按列组成n行m列矩阵X* 2)特征中心化。即每一维的数据都减去该维的均值使每一维的均值都为0* 3)求出协方差矩阵* 4)求出协方差矩阵的特征值及对应的特征向量* 5)将特征向量按对应的特征值大小从上往下按行排列成矩阵取前k行组成矩阵p* 6)YPX 即为降维到k维后的数据*/
public class PCA {public static DenseMatrix64F runPCA(DenseMatrix64F src,int k) {DenseMatrix64F rs new DenseMatrix64F(src.numRows,k);//计算输入矩阵每个元素和特征值平均的差值矩阵DenseMatrix64F norm_X new DenseMatrix64F(src.numRows,src.numCols);for(int i 0;isrc.numCols;i) {double tmp0;for(int j0;jsrc.numRows;j) {tmpsrc.get(j, i);}tmp /src.numRows;for(int j0;jsrc.numRows;j) {norm_X.set(j,i, src.get(j, i)-tmp);}}//计算协方差矩阵DenseMatrix64F norm_X_T new DenseMatrix64F(src.numCols,src.numRows);CommonOps.transpose(norm_X, norm_X_T);DenseMatrix64F scatter_matrix new DenseMatrix64F(src.numCols,src.numCols);CommonOps.mult(norm_X_T,norm_X,scatter_matrix);//特征向量分解EDInfo ed JacobiCount(new DenseMatrix64F(scatter_matrix),0.001,1000);//选取前k个特征DenseMatrix64F feature new DenseMatrix64F(k,src.numCols);for(int i0;ik;i) {for(int j0;jsrc.numCols;j) {feature.set(i, j, ed.getValues().get(j, i));}}DenseMatrix64F feature_T new DenseMatrix64F(src.numCols,k);CommonOps.transpose(feature, feature_T);CommonOps.mult(norm_X,feature_T,rs);return rs;}public static EDInfo JacobiCount(DenseMatrix64F src, double diff, int iter) {DenseMatrix64F values new DenseMatrix64F(src.numRows,src.numCols);for(int i0;isrc.numRows;i) {for(int j0;jsrc.numCols;j) {if(i j) {values.set(i, j, 1);}else {values.set(i, j, 0);}}}int nCount 0;while(true){double dbMax Double.MIN_VALUE;int nRow 0;int nCol 1;for(int i0;isrc.numRows;i) {for(int j0;jsrc.numCols;j) {if(i ! j Math.abs(src.get(i, j)) dbMax) {dbMax Math.abs(src.get(i, j));nRow i;nCol j;}}}if(dbMax diff)break;if(nCount iter)break;nCount;double dbApp src.get(nRow, nRow);double dbApq src.get(nRow, nCol);double dbAqq src.get(nCol, nCol);double dbAngle 0.5*Math.atan2(-2*dbApq,dbAqq-dbApp);double dbSinTheta Math.sin(dbAngle);double dbCosTheta Math.cos(dbAngle);double dbSin2Theta Math.sin(2*dbAngle);double dbCos2Theta Math.cos(2*dbAngle);src.set(nRow, nRow, dbApp*dbCosTheta*dbCosTheta dbAqq*dbSinTheta*dbSinTheta 2*dbApq*dbCosTheta*dbSinTheta);src.set(nCol, nCol, dbApp*dbSinTheta*dbSinTheta dbAqq*dbCosTheta*dbCosTheta - 2*dbApq*dbCosTheta*dbSinTheta);src.set(nRow, nCol, 0.5*(dbAqq-dbApp)*dbSin2Theta dbApq*dbCos2Theta);src.set(nCol, nRow,src.get(nRow, nCol));for(int i 0; i src.numRows; i ){if((i!nCol) (i!nRow)){dbMax src.get(i, nRow);src.set(i, nRow,src.get(i, nCol)*dbSinThetadbMax*dbCosTheta);src.set(i, nCol,src.get(i, nCol)*dbCosTheta-dbMax*dbSinTheta);}}for (int j 0; j src.numRows; j ){if((j!nCol) (j!nRow)){dbMax src.get(nRow, j);src.set(nRow, j,src.get(nCol, j)*dbSinThetadbMax*dbCosTheta);src.set(nCol, j,src.get(nCol, j)*dbCosTheta-dbMax*dbSinTheta);}}for(int i 0; i src.numRows; i ){dbMax values.get(i,nRow);values.set(i, nRow,values.get(i,nCol)*dbSinThetadbMax*dbCosTheta);values.set(i,nCol,values.get(i,nCol)*dbCosTheta-dbMax*dbSinTheta);}}double[] eig new double[src.numRows];for(int i0;isrc.numRows;i) {eig[i] src.get(i, i);}int[] sortInx argsort(eig);DenseMatrix64F tmpValues new DenseMatrix64F(src.numRows,src.numCols);for(int i0;isrc.numRows;i) {for(int j0;jsrc.numRows;j) {tmpValues.set(i, j, values.get(j,sortInx[i]));}eig[i] src.get(sortInx[i],sortInx[i]);}for(int i 0; i src.numRows; i ){double dSumVec 0;for(int j 0; j src.numRows; j )dSumVec tmpValues.get(j, i);if(dSumVec0){for(int j 0;j src.numRows; j )tmpValues.set(j, i,tmpValues.get(j, i)*-1);}}return new EDInfo(tmpValues,eig);}public static int[] argsort(double[] input) {int[] rs new int[input.length];for(int i0;iinput.length;i){rs[i] i;}for(int i0;iinput.length-1;i) {for(int ji1;jinput.length;j) {if(input[i] input[j]) {double tmp input[i];int tmpIndex rs[j];input[i] input[j];input[j] tmp;rs[j] rs[i];rs[i] tmpIndex;}}}return rs;}static ArrayListString tempcnew ArrayList();public double[][] readData() throws IOException {double[][] resnew double[78][13];try { // 防止文件建立或读取失败用catch捕捉错误并打印也可以throw/* 读入TXT文件 */File filename new File(src/bp/test.txt); // 要读取以上路径的input。txt文件InputStreamReader reader new InputStreamReader(new FileInputStream(filename)); // 建立一个输入流对象readerBufferedReader br new BufferedReader(reader); // 建立一个对象它把文件内容转成计算机能读懂的语言String line ;line br.readLine();int j0;while (line ! null) {String[] templine.split(,);for(int i0;i13;i){res[j][i]Double.parseDouble(temp[i]);System.out.print( res[j][i] );}tempc.add(temp[13]);System.out.println();j;line br.readLine();}} catch (Exception e) {e.printStackTrace();}return res;}public static void writeTxt( DenseMatrix64F denseMatrix64F){try { // 防止文件建立或读取失败用catch捕捉错误并打印也可以throw/* 读入TXT文件 */StringBuilder stringBuildernew StringBuilder();for(int i0;idenseMatrix64F.numRows;i){for(int j0;jdenseMatrix64F.numCols;j)stringBuilder.append(denseMatrix64F.get(i,j)).append(,);stringBuilder.append(tempc.get(i)).append(\n);}File writename new File(src/bp/test(low).txt); // 相对路径如果没有则要建立一个新的output。txt文件writename.createNewFile(); // 创建新文件BufferedWriter out new BufferedWriter(new FileWriter(writename));out.write(stringBuilder.toString()); // \r\n即为换行out.flush(); // 把缓存区内容压入文件out.close(); // 最后记得关闭文件} catch (Exception e) {e.printStackTrace();}}public static void main(String[] args) throws IOException {// TODO Auto-generated catch blockPCA pca new PCA();//获得样本集double[][] primaryArray pca.readData() ;System.out.println();DenseMatrix64F denseMatrix64FrunPCA(new DenseMatrix64F(primaryArray),10);writeTxt(denseMatrix64F);}}
最小最大规范化
public class DealData {static double[] maxnew double[14];static double[] minnew double[14];static ArrayListString list1new ArrayList();public static ListListDouble readTxt(String fileName){ListListDouble listnew ArrayList();Arrays.fill(max,Integer.MIN_VALUE);Arrays.fill(min,Integer.MAX_VALUE);try { // 防止文件建立或读取失败用catch捕捉错误并打印也可以throw/* 读入TXT文件 */File filename new File(fileName); // 要读取以上路径的input。txt文件InputStreamReader reader new InputStreamReader(new FileInputStream(filename)); // 建立一个输入流对象readerBufferedReader br new BufferedReader(reader); // 建立一个对象它把文件内容转成计算机能读懂的语言String line ;line br.readLine();while (line ! null) {if(line.length()0){String[] templine.split(,);ArrayListDouble stringsnew ArrayList();for(int i0;itemp.length-1;i){strings.add(Double.parseDouble(temp[i]));max[i]Math.max(Double.parseDouble(temp[i]),max[i]);min[i]Math.min(Double.parseDouble(temp[i]),min[i]);}list1.add(temp[temp.length-1]);list.add(strings);}line br.readLine();}} catch (Exception e) {e.printStackTrace();}return list;}public static void writeTxt(String content){try { // 防止文件建立或读取失败用catch捕捉错误并打印也可以throw/* 读入TXT文件 */File writename new File(src/bp/trainBayes.txt); // 相对路径如果没有则要建立一个新的output。txt文件writename.createNewFile(); // 创建新文件BufferedWriter out new BufferedWriter(new FileWriter(writename));out.write(content); // \r\n即为换行out.flush(); // 把缓存区内容压入文件out.close(); // 最后记得关闭文件} catch (Exception e) {e.printStackTrace();}}public static void main(String[] args) {ListListDouble list readTxt(src/bp/train(low).txt);StringBuilder stringBuildernew StringBuilder();for(int i0;ilist.size();i){for(int j0;jlist.get(i).size()-1;j){double gapMath.ceil((max[j]-min[j])/8);stringBuilder.append(Math.round((list.get(i).get(j)-min[j])/gap)).append(,);}stringBuilder.append(list1.get(i));stringBuilder.append(\n);}writeTxt(stringBuilder.toString());}
}