數據挖掘—主成分分析法降維和最小最大規范化

  • 算法步驟:
  • 1)將原始數據按列組成n行m列矩陣X
  • 2)特征中心化。即每一維的數據都減去該維的均值,使每一維的均值都為0
  • 3)求出協方差矩陣
  • 4)求出協方差矩陣的特征值及對應的特征向量
  • 5)將特征向量按對應的特征值大小從上往下按行排列成矩陣,取前k行組成矩陣p
  • 6)Y=PX 即為降維到k維后的數據

PCA


/** 算法步驟:* 1)將原始數據按列組成n行m列矩陣X* 2)特征中心化。即每一維的數據都減去該維的均值,使每一維的均值都為0* 3)求出協方差矩陣* 4)求出協方差矩陣的特征值及對應的特征向量* 5)將特征向量按對應的特征值大小從上往下按行排列成矩陣,取前k行組成矩陣p* 6)Y=PX 即為降維到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;i<src.numCols;i++) {double tmp=0;for(int j=0;j<src.numRows;j++) {tmp+=src.get(j, i);}tmp /=src.numRows;for(int j=0;j<src.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 i=0;i<k;i++) {for(int j=0;j<src.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 i=0;i<src.numRows;i++) {for(int j=0;j<src.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 i=0;i<src.numRows;i++) {for(int j=0;j<src.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)*dbSinTheta+dbMax*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)*dbSinTheta+dbMax*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)*dbSinTheta+dbMax*dbCosTheta);values.set(i,nCol,values.get(i,nCol)*dbCosTheta-dbMax*dbSinTheta);}}double[] eig = new double[src.numRows];for(int i=0;i<src.numRows;i++) {eig[i] = src.get(i, i);}int[] sortInx = argsort(eig);DenseMatrix64F tmpValues = new DenseMatrix64F(src.numRows,src.numCols);for(int i=0;i<src.numRows;i++) {for(int j=0;j<src.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(dSumVec<0){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 i=0;i<input.length;i++){rs[i] = i;}for(int i=0;i<input.length-1;i++) {for(int j=i+1;j<input.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 ArrayList<String> tempc=new ArrayList<>();public  double[][] readData() throws IOException {double[][] res=new 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 j=0;while (line != null) {String[] temp=line.split(",");for(int i=0;i<13;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 stringBuilder=new StringBuilder();for(int i=0;i<denseMatrix64F.numRows;i++){for(int j=0;j<denseMatrix64F.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 denseMatrix64F=runPCA(new DenseMatrix64F(primaryArray),10);writeTxt(denseMatrix64F);}}

最小最大規范化

public class DealData {static double[] max=new double[14];static double[] min=new double[14];static ArrayList<String> list1=new ArrayList<>();public static  List<List<Double>> readTxt(String fileName){List<List<Double>> list=new 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[] temp=line.split(",");ArrayList<Double> strings=new ArrayList<>();for(int i=0;i<temp.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) {List<List<Double>> list=   readTxt("src/bp/train(low).txt");StringBuilder stringBuilder=new StringBuilder();for(int i=0;i<list.size();i++){for(int j=0;j<list.get(i).size()-1;j++){double gap=Math.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());}
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/392095.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/392095.shtml
英文地址,請注明出處:http://en.pswp.cn/news/392095.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

用戶使用說明c語言,(C語言使用指南.docx

(C語言使用指南Turbo C(V2.0)使用指南(本文的許多命令或方法同樣適用于TC3) 在開始看本文以前&#xff0c;我先說明一下C語言的安裝和使用中最應該注意的地方&#xff1a;許多網友在下載Turbo C 2.0和Turbo C 3.0后&#xff0c;向我問得最多的是在使用過程中碰到如下問題&…

三維空間兩直線/線段最短距離、線段計算算法 【轉】

https://segmentfault.com/a/1190000006111226d(ls,lt)|sj?tj||s0?t0(be?cd)u? ?(ae?bd)v? ac?bd(ls,lt)|sj?tj||s0?t0(be?cd)u? ?(ae?bd)v? ac?b2|具體實現代碼如下&#xff08;C#實現&#xff09;&#xff1a; public bool IsEqual(double d1, double d2) { …

【慎思堂】之JS牛腩總結

一 JS基礎 1-定義 Javascript是一種腳本語言/描述語言&#xff0c;是一種解釋性語言。用于開發交互式web網頁&#xff0c;使得網頁和用戶之間實現了一種實時性的、動態的、交互性的關系&#xff0c;使網頁包含更多活躍的元素和更加精彩的內容。 主要用于&#xff1a;表單驗證 …

vuejs 輪播_如何在VueJS中設計和構建輪播功能

vuejs 輪播by Fabian Hinsenkamp由Fabian Hinsenkamp設計 A carousel, slideshow, or slider — however you call it this class of UI — has become one of the core elements used in modern web development. Today, it’s almost impossible to find any Website or UI …

iOS繪圓形圖-CGContextAddArc各參數說明

2019獨角獸企業重金招聘Python工程師標準>>> 1.使用 UIGraphicsGetCurrentContext() 畫圓 CGContextAddArc(<#CGContextRef _Nullable c#>, <#CGFloat x#>, <#CGFloat y#>, <#CGFloat radius#>, <#CGFloat startAngle#>, <#CGFlo…

c語言中if和goto的用法,C語言中if和goto的用法.doc

C語言中if和goto的用法C語言中&#xff0c;if是一個條件語句&#xff0c;用法??if(條件表達式) 語句如果滿足括號里面表達式&#xff0c;表示邏輯為真于是執行后面的語句&#xff0c;否則不執行(表達式為真則此表達式的值不為0&#xff0c;為假則為0&#xff0c;也就是說&…

數據挖掘—K-Means算法(Java實現)

算法描述 &#xff08;1&#xff09;任意選擇k個數據對象作為初始聚類中心 &#xff08;2&#xff09;根據簇中對象的平均值&#xff0c;將每個對象賦給最類似的簇 &#xff08;3&#xff09;更新簇的平均值&#xff0c;即計算每個對象簇中對象的平均值 &#xff08;4&#xf…

自我價值感缺失的表現_不同類型的缺失價值觀和應對方法

自我價值感缺失的表現Before handling the missing values, we must know what all possible types of it exists in the data science world. Basically there are 3 types to be found everywhere on the web, but in some of the core research papers there is one more ty…

[收藏轉載]C# GDI+ 簡單繪圖(一)

最近對GDI這個東西接觸的比較多&#xff0c;也做了些簡單的實例&#xff0c;比如繪圖板&#xff0c;仿QQ截圖等&#xff0e; 廢話不多說了&#xff0c;我們先來認識一下這個GDI&#xff0c;看看它到底長什么樣. GDI&#xff1a;Graphics Device Interface Plus也就是圖形設備接…

mybaties總結+hibernate總結

一、對原生態jdbc程序中問題總結 1.1 jdbc程序 需求&#xff1a;使用jdbc查詢mysql數據庫中用戶表的記錄 statement:向數據庫中發送一個sql語句 預編譯statement&#xff1a;好處&#xff1a;提高數據庫性能。 預編譯statement向數據庫中發送一個sql語句&#xff0c;數據庫編譯…

客戶旅程_我如何充分利用freeCodeCamp的旅程

客戶旅程by Catherine Vassant (aka Codingk8)由凱瑟琳瓦森(Catherine Vassant)(又名Codingk8) 我如何充分利用freeCodeCamp的旅程 (How I made the most out of my freeCodeCamp journey) 我的路線圖&#xff1f; ?超越課程范圍的reeCodeCamp (My road map ?? to freeCode…

Python14 函數

函數 面向對象編程&#xff1a; 類----class 面向過程編程&#xff1a;過程---def 函數式編程&#xff1a;函數---def def test(x):描述x 1return x#def是定義函數的關鍵字#test是函數名稱#&#xff08;x&#xff09;是參數#x1是 函數體&#xff0c;是一段邏輯代碼#return 定義…

學習sql注入:猜測數據庫_面向數據科學家SQL:學習簡單方法

學習sql注入:猜測數據庫We don’t pick a hammer and look for nails — that would be an unusual way of solving problems. The usual way of doing business is to identify the problem first, then look for appropriate tools.我們不用錘子找釘子&#xff0c;那是解決問…

android 百度地圖3.0,android 百度地圖3.0

一&#xff1a;為地圖設置事件注意新版本中要有一個getMapmMapView.getMap().setOnMapStatusChangeListener(listener);OnMapStatusChangeListener listener newOnMapStatusChangeListener() {/*** 手勢操作地圖&#xff0c;設置地圖狀態等操作導致地圖狀態開始改變。* param s…

(摘錄)sockaddr與sockaddr_in,sockaddr_un結構體詳細講解

struct sockaddr { unsigned short sa_family; /* address family, AF_xxx */ char sa_data[14]; /* 14 bytes of protocol address */ }; sa_family是地址家族&#xff0c;一般都是“AF_xxx”的形式。好像通常大多用的是都是AF_INET。 sa_data是14字節協議…

數據挖掘—K-中心點聚類算法(Java實現)

K-中心點聚類算法 &#xff08;1&#xff09;任意選擇k個對象作為初始的簇中心點 &#xff08;2&#xff09;指派每個剩余對象給離他最近的中心點所表示的簇 &#xff08;3&#xff09;選擇一個未被選擇的中心點直到所有的中心點都被選擇過 &#xff08;4&#xff09;選擇一個…

使用akka構建高并發程序_如何使用Akka Cluster創建簡單的應用程序

使用akka構建高并發程序If you read my previous story about Scalachain, you probably noticed that it is far from being a distributed system. It lacks all the features to properly work with other nodes. Add to it that a blockchain composed by a single node is…

pandas之數值計算與統計

數值計算與統計 對于DataFrame來說&#xff0c;求和、最大、最小、平均等統計方法&#xff0c;默認是按列進行統計&#xff0c;即axis 0&#xff0c;如果添加參數axis 1則會按照行進行統計。 如果存在空值&#xff0c;在統計時默認會忽略空值&#xff0c;如果添加參數skipna …

python自動化數據報告_如何:使用Python將實時數據自動化到您的網站

python自動化數據報告This tutorial will be helpful for people who have a website that hosts live data on a cloud service but are unsure how to completely automate the updating of the live data so the website becomes hassle free. For example: I host a websit…

一顆站在技術邊緣的土豆

2012年開始上專業課&#xff0c;2013年打了一年游戲&#xff0c;年底專業課忘光了&#xff0c;但是蒙混過關沒掛科&#xff0c;2014年7月份畢業&#xff0c;對這個社會充滿向往。2014年9月份——方正代理商做網絡安全公司。2015年3月份跳槽到一家vmware代理商公司。2016年6月&a…