我试图计算矩阵的PCA.
有时,得到的特征值/向量是复数值,因此当试图通过将特征向量矩阵与点坐标相乘来将点投影到较低维度平面时,得到以下警告
ComplexWarning: Casting complex values to real discards the imaginary part
Run Code Online (Sandbox Code Playgroud)
在那行代码中 np.dot(self.u[0:components,:],vector)
我用来计算PCA的整个代码
import numpy as np
import numpy.linalg as la
class PCA:
def __init__(self,inputData):
data = inputData.copy()
#m = no of points
#n = no of features per point
self.m = data.shape[0]
self.n = data.shape[1]
#mean center the data
data -= np.mean(data,axis=0)
# calculate the covariance matrix
c = np.cov(data, rowvar=0)
# get the eigenvalues/eigenvectors of c
eval, evec = la.eig(c)
# u = eigen vectors (transposed) …Run Code Online (Sandbox Code Playgroud) 我想将double数组映射到现有的MatrixXd结构.到目前为止,我已经设法将Eigen矩阵映射到一个简单的数组,但我找不到回来的方法.
void foo(MatrixXd matrix, int n){
double arrayd = new double[n*n];
// map the input matrix to an array
Map<MatrixXd>(arrayd, n, n) = matrix;
//do something with the array
.......
// map array back to the existing matrix
}
Run Code Online (Sandbox Code Playgroud) 我有一个较低的三角形MatrixXd,我想将其较低的值复制到上面,因为它将成为一个对称矩阵.我该怎么做?
到目前为止我已经完成了:
MatrixXd m(n,n);
.....
//do something with m
for(j=0; j < n; j++)
{
for(i=0; i<j; i++)
{
m(i,j) = m(j,i);
}
}
Run Code Online (Sandbox Code Playgroud)
有最快的方法吗?我在考虑一些能够将下三角矩阵"复制"到鞋面的内部方法.说我有这个矩阵,我们称之为m:
1 2 3
4 5 6
7 8 9
Run Code Online (Sandbox Code Playgroud)
我需要获得的m是:
1 4 7
4 5 8
7 8 9
Run Code Online (Sandbox Code Playgroud)
我也知道你可以让矩阵的上部或下部做一些事情:
MatrixXd m1(n,n);
m1 = m.triangularView<Eigen::Upper>();
cout << m1 <<endl;
1 2 3
0 5 6
0 0 9
Run Code Online (Sandbox Code Playgroud)
但我还不能得到我想要的东西......
我有一个矩阵100x100,我发现它是最大的特征值.现在我需要找到对应于这个特征值的特征向量.我怎样才能做到这一点?
在MATLAB中我可以发出命令:
[X,L] = eig(A,'nobalance');
Run Code Online (Sandbox Code Playgroud)
为了计算没有平衡选项的特征值.
NumPy中的等效命令是什么?当我运行NumPy版本的eig时,它不会产生与打开nobalance的MATLAB结果相同的结果.
我用Jacobi方法在c代码中找到所有特征值和特征向量.虽然雅可比方法的复杂性是O(n ^ 3),但我矩阵的维数很大(17814 X 17814).这需要很多时间.我想知道一个更好的算法,通过它我可以解决这个问题.如果你想我可以附上我的c代码.
我正在尝试将pcl pointXYZ转换为特征向量
Eigen::Vector4f min (minPnt.x, minPnt.y, minPnt.z);
Eigen::Vector4f max (maxPnt.x, maxPnt.y, maxPnt.z);
Run Code Online (Sandbox Code Playgroud)
其中minPnt和maxPnt的类型为pcl :: PointXYZ。但是,出现错误“错误C2338:THIS_METHOD_IS_ONLY_FOR_VECTORS_OF_A_SPECIFIC_SIZE”。您能否建议其他方法或让我知道我的方法是否错误。
作为当前任务,我需要计算120*120矩阵的特征值和特征向量.首先,我在Java(Apache Commons Math库)和Python 2.7(Numpy库)中以简单的2×2矩阵测试了这些计算.我有一个问题,特征向量值不匹配,如下所示:
//Java
import org.apache.commons.math3.linear.EigenDecomposition;
import org.apache.commons.math3.linear.MatrixUtils;
import org.apache.commons.math3.linear.RealMatrix;
public class TemporaryTest {
public static void main(String[] args) {
double[][] testArray = {{2, -1}, {1, 1}};
RealMatrix testMatrix = MatrixUtils.createRealMatrix(testArray);
EigenDecomposition decomposition = new EigenDecomposition (testMatrix);
System.out.println("eigenvector[0] = " + decomposition.getEigenvector(0));
System.out.println("eigenvector[1] = " + decomposition.getEigenvector(1));
}
Run Code Online (Sandbox Code Playgroud)
特征向量的输出显示为{real_value + imaginary_value; real_value + imaginary_value}:
//Java output
eigenvector[0] = {-0.8660254038; 0}
eigenvector[1] = {0.5; 1}
Run Code Online (Sandbox Code Playgroud)
Python中的代码相同,但使用Numpy库:
# Python
import numpy as np
from numpy import linalg as LA
w, v …Run Code Online (Sandbox Code Playgroud) 我在 Tensorflow 中使用特征分解,发现它非常慢。下面的代码显示了 Tensorflow 的速度与 numpy 和 scipy 的比较:
import numpy as np
import scipy as sp
import tensorflow as tf
from time import time
A = np.random.randn(400, 400)
A_tf = tf.constant(A)
cur = time()
d, v = sp.linalg.eig(A)
print(f'sp: {time() - cur:4.2f} s')
cur = time()
d, v = np.linalg.eig(A)
print(f'np: {time() - cur:4.2f} s')
cur = time()
d, v = tf.linalg.eig(A_tf)
print(f'tf: {time() - cur:4.2f} s')
Run Code Online (Sandbox Code Playgroud)
这给出了以下输出:
sp: 0.09 s
np: 0.08 s
tf: 5.04 s
Run Code Online (Sandbox Code Playgroud)
对这里有什么想法吗?
我正在寻找在 Matlab 中解决广义特征向量和特征值问题。为此,我测试了2种方法。
然后,我们可以在每一边乘以 B^(-1),例如:
所以,从理论角度来看,这是一个简单而经典的特征值问题。
最后,在 Matlab 中,我简单地使用A=FISH_sp和进行了操作B=FISH_xc:
[Phi, Lambda] = eig(inv(FISH_xc)*FISH_sp);
Run Code Online (Sandbox Code Playgroud)
但是,当我在简单的费舍尔综合之后得到的结果不正确(约束太糟糕,而且还出现了nan值。我不知道为什么我没有得到与下面第二个相同的结果。
总而言之,所使用的算法在第 7 页进行了描述。我已经遵循了该算法的所有步骤,当我进行 Fisher 综合时,它似乎给出了更好的结果。
这里是感兴趣的部分(抱歉,我认为 Stakoverflow 上没有 Latex):
这是我的这个方法的小 Matlab 脚本:
% Diagonalize A = FISH_sp and B = Fish_xc
[V1,D1] = eig(FISH_sp);
[V2,D2] = eig(FISH_xc);
% Applying each step of algorithm 1 on page 7
phiB_bar = V2*(D2.^(0.5)+1e-10*eye(7))^(-1);
barA = inv(phiB_bar)*FISH_sp*phiB_bar;
[phiA, vA] = eig(barA);
Phi = phiB_bar*phiA;
Run Code Online (Sandbox Code Playgroud)
所以最后,我找到了 phi 特征向量矩阵 (phi) 和 lambda …
eigenvector ×10
eigenvalue ×8
python ×4
c++ ×3
numpy ×3
c ×2
eigen ×2
matlab ×2
diagonal ×1
java ×1
pca ×1
r ×1
tensorflow ×1