-
Notifications
You must be signed in to change notification settings - Fork 3
Open
Description
Hi, I tried your PCA and crashed at wcenter of you method. I trace your code and find an error when the matrix is not squared. following is the code. In my case, m is 30 and n is 3000, that's m > n. Matrix S is a Transposed one. It becomes a n x m matrix, but your j is within m, the large one, So, S[i][j] will be out of range to crash. I think it's should be S[j][i], or exchange n and m in first loop like second, right?
I want to know when I assign a matrix to PCA, should row for record or column for records? the one I try is 30 records with 3000 features.
thanks.
Henry
/**
* Center and weight Sample variance for grouped data
*
* s^2 = SUM(Mi - xbar)^2 / (n-1)
*
* @return
*/
public Matrix wcenter() {
// center the data
Matrix s = center().transpose();
double[][] S = s.getArray();
// calculate the sample variance
double[] sigma = new double[n];
for (int j = 0; j < n; j++) {
double ssum = 0;
for (int i = 0; i < m; i++) {
ssum += (S[i][j] * S[i][j]);
}
sigma[j] = Math.sqrt(ssum / (m - 1));
}
// weigh the data
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
S[i][j] = S[i][j] / sigma[j];
}
}
return s;
}
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels