数据挖掘—主成分分析法降维和最小最大规范化

  • 算法步骤:
  • 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.mzph.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…