mkobos / pca_transform Goto Github PK
View Code? Open in Web Editor NEWJava PCA transformation of a data matrix
License: MIT License
Java PCA transformation of a data matrix
License: MIT License
mkobos, I'm trying to use your library to do PCA in Java but I'm not quite sure how it works exactly. I have a set of n-points (xyz cartesian) and I want to find the plane along which they best fit; I was under the assumption that the eigenvectors would do this for me. However, your example doesn't explain itself well.
You create training data to initialize your PCA class with and then transform test data. The training data seems to have six components per instance as does the test data. However the transformed data only has two components (I'm assuming the components that aren't useful are being dropped).
However, I don't see a way to either have it not drop these components nor how to find out which components were removed in order to build a vector the correct vector.
As I said, I have cartesian coordinates and would like to find the best fit plane for them (well, the normal specifically). If you could add a quick example for me (or a way to query which components are being dropped) that would be very helpful. If I'm doing something wrong, please let me know.
Thanks!
Stephen
Here's my code sample; points contains all my vertices, centroid is the average coordinate of all the vertices. I've set the points to all have a y-value of 0 as a test case. I'm assuming to get an eigenvector of [0,1,0] for the plane normal (or something close to it).
final List<Tuple3d> points = new ArrayList<>();
final Tuple3d centroid = new Tuple3d(0,0,0);
for(int x = -10; x <= 10; x++) {
for(int z = -10; z <= 10; z++) {
points.add(new Tuple3d(x,0,z));
}
}
final double[][] samples = new double[points.size()][3];
for(int i = 0; i < points.size(); i++) {
final Tuple3d tuple = points.get(i);
samples[i][0] = tuple.x - centroid.x;
samples[i][1] = tuple.y - centroid.y;
samples[i][2] = tuple.z - centroid.z;
}
final Matrix sampleMatrix = new Matrix(samples);
final PCA pca = new PCA(sampleMatrix, false);
final Matrix eigenMatrix = pca.getEigenvectorsMatrix();
for(int i = 0; i < pca.getOutputDimsNo(); i++) {
System.out.println("eigenvalue " + i + ": " + pca.getEigenvalue(i));
}
final double[][] vectors = eigenMatrix.getArray();
for(int i = 0; i < vectors.length; i++) {
System.out.println("eigenvector " + i + ": " + Arrays.toString(vectors[i]));
}
And the output
eigenvalue 0: 36.75000000000001
eigenvalue 1: 36.749999999999964
eigenvector 0: [1.0, 0.0]
eigenvector 1: [0.0, 0.0]
eigenvector 2: [0.0, 1.0]
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.