我正在使用OpenCV编写人脸识别程序.
当产生的特征脸:
我说的是特征脸生成,这是" 学习 "步骤.
我需要使用多少张照片才能获得不错的精确度?更像20或2000?
谢谢
opencv face-recognition computer-vision face-detection eigenvector
如何找出与特定特征值对应的特征向量?
我有一个随机矩阵(P),其中一个特征值是1.我需要找到对应于特征值1的特征向量.
scipy函数scipy.linalg.eig返回特征值和特征向量的数组.
D, V = scipy.linalg.eig(P)
Run Code Online (Sandbox Code Playgroud)
这里D(值的数组)和V(矢量的数组)都是矢量.
一种方法是在D中进行搜索并在V中提取相应的特征向量.有更简单的方法吗?
我正在尝试编写一个获取A任意大小矩阵的程序,并且SVD将其分解:
A = U * S * V'
Run Code Online (Sandbox Code Playgroud)
A用户输入的矩阵在哪里,U正交矩阵由特征值的特征向量组成A * A',S是奇异值的对角矩阵,并且V是特征向量的正交矩阵A' * A.
问题是:MATLAB函数eig有时会返回错误的特征向量.
这是我的代码:
function [U,S,V]=badsvd(A)
W=A*A';
[U,S]=eig(W);
max=0;
for i=1:size(W,1) %%sort
for j=i:size(W,1)
if(S(j,j)>max)
max=S(j,j);
temp_index=j;
end
end
max=0;
temp=S(temp_index,temp_index);
S(temp_index,temp_index)=S(i,i);
S(i,i)=temp;
temp=U(:,temp_index);
U(:,temp_index)=U(:,i);
U(:,i)=temp;
end
W=A'*A;
[V,s]=eig(W);
max=0;
for i=1:size(W,1) %%sort
for j=i:size(W,1)
if(s(j,j)>max)
max=s(j,j);
temp_index=j;
end
end
max=0;
temp=s(temp_index,temp_index);
s(temp_index,temp_index)=s(i,i);
s(i,i)=temp;
temp=V(:,temp_index);
V(:,temp_index)=V(:,i);
V(:,i)=temp;
end
s=sqrt(s);
end
Run Code Online (Sandbox Code Playgroud)
我的代码返回正确的s矩阵,也"几乎"正确U …
什么是最快的计算方法,我看到有些人使用矩阵,当我在互联网上搜索时,他们谈论了特征值和特征向量(对这些东西一无所知)……有一个问题简化为递归方程f(n)=(2 * f(n-1))+ 2,f(1)= 1,n可能高达10 ^ 9 ....我已经尝试使用DP,最多存储1000000个值并使用常见的快速求幂方法,在这些模数问题中通常都超时了,通常较弱,这需要计算较大的值
我正在做一些矩阵计算,想要计算这个特定矩阵的特征值和特征向量:

我发现它的特征值和特征向量是分析性的,并且想要确认我的答案numpy.linalg.eigh,因为这个矩阵是对称的.这是问题:我找到了预期的特征值,但相应的特征向量似乎根本不是特征向量
这是我使用的一小段代码:
import numpy as n
def createA():
#create the matrix A
m=3
T = n.diag(n.ones(m-1.),-1.) + n.diag(n.ones(m)*-4.) +\
n.diag(n.ones(m-1.),1.)
I = n.identity(m)
A = n.zeros([m*m,m*m])
for i in range(m):
a, b, c = i*m, (i+1)*m, (i+2)*m
A[a:b, a:b] = T
if i < m - 1:
A[b:c, a:b] = A[a:b, b:c] = I
return A
A = createA()
ev,vecs = n.linalg.eigh(A)
print vecs[0]
print n.dot(A,vecs[0])/ev[0]
Run Code Online (Sandbox Code Playgroud)
因此,对于第一个特征值/特征向量对,这会产生:
[ 2.50000000e-01 5.00000000e-01 -5.42230975e-17 -4.66157689e-01
3.03192985e-01 2.56458619e-01 -7.84539156e-17 -5.00000000e-01 …Run Code Online (Sandbox Code Playgroud) I am trying to calculate the eigenvectors of many 3x3 matrices using python. My code in python is based on code from Mathematica which uses the Eigenvector[] function. I have tried using the eig() function from both numpy and scipy and the majority of the time the eigenvectors calculated in mathematica and python are identical. However, there are a few instances which the eigenvectors calculated in python are opposite in sign to those calculated in mathematica.
I have already tried …
我有一个非常简单的问题.它与计算容差误差有关.
让我(在最后看)特征向量V和对角特征值D中的矩阵A的特征分解,并通过乘法V ^ -1*D*V再次构建它.
得到的值远不是A,误差很大.
我想知道我是否使用不正确的函数来完成此任务,或者至少我如何减少此错误.先感谢您
in[1]:import numpy
from scipy import linalg
A=matrix([[16,-9,0],[-9,20,-11],[0,-11,11]])
D,V=linalg.eig(A)
D=diagflat(D)
matrix(linalg.inv(V))*matrix(D)*matrix(V)
out[1]:matrix([[ 15.52275377, 9.37603361, 0.79257097],
[9.37603361, 21.12538282, -10.23535271],
[0.79257097, -10.23535271, 10.35186341]])
Run Code Online (Sandbox Code Playgroud) 如何计算python中的左侧特征向量?
>>> import from numpy as np
>>> from scipy.linalg import eig
>>> np.set_printoptions(precision=4)
>>> T = np.mat("0.2 0.4 0.4;0.8 0.2 0.0;0.8 0.0 0.2")
>>> print "T\n", T
T
[[ 0.2 0.4 0.4]
[ 0.8 0.2 0. ]
[ 0.8 0. 0.2]]
>>> w, vl, vr = eig(T, left=True)
>>> vl
array([[ 0.8165, 0.8165, 0. ],
[ 0.4082, -0.4082, -0.7071],
[ 0.4082, -0.4082, 0.7071]])
Run Code Online (Sandbox Code Playgroud)
这似乎不正确,谷歌对此并不友好!
我需要使用一些 python 代码使用转换矩阵的左特征向量找到马尔可夫模型的稳态。
在这个问题中已经确定scipy.linalg.eig 无法提供所描述的实际左特征向量,但在那里演示了修复。官方文档和往常一样,大多无用且难以理解。
比错误格式更大的问题是生成的特征值没有任何特定的顺序(没有排序并且每次都不同)。所以如果你想找到对应于 1 个特征值的左特征向量,你必须寻找它们,这会带来它自己的问题(见下文)。数学很清楚,但如何让 python 计算它并返回正确的特征向量尚不清楚。这个问题的其他答案,比如这个,似乎没有使用左特征向量,所以那些不是正确的解决方案。
这个问题提供了部分解决方案,但它没有考虑较大转换矩阵的无序特征值。所以,只需使用
leftEigenvector = scipy.linalg.eig(A,left=True,right=False)[1][:,0]
leftEigenvector = leftEigenvector / sum(leftEigenvector)
Run Code Online (Sandbox Code Playgroud)
很接近,但通常不起作用,因为[:,0]位置中的条目可能不是正确特征值的特征向量(在我的情况下通常不是)。
好的,但是 的输出scipy.linalg.eig(A,left=True,right=False)是一个数组,其中的[0]元素是每个特征值的列表(不按任何顺序),然后在位置上跟随着[1]与这些特征值对应的顺序的特征向量数组。
我不知道通过特征值对整个事物进行排序或搜索以提取正确的特征向量(所有特征值为 1 的特征向量均由向量条目的总和归一化)的好方法。我的想法是获得特征值的索引等于 1,然后从特征向量数组中提取这些列。我的这个版本又慢又麻烦。首先,我有一个函数(不太好用)来查找与值匹配的最后一个位置:
# Find the positions of the element a in theList
def findPositions(theList, a):
return [i for i, x in enumerate(theList) if x == a]
Run Code Online (Sandbox Code Playgroud)
然后我像这样使用它来获得与特征值 = 1 匹配的特征向量。
M = transitionMatrix( G )
leftEigenvectors = scipy.linalg.eig(M,left=True,right=False)
unitEigenvaluePositions = findPositions(leftEigenvectors[0], 1.000)
steadyStateVectors …Run Code Online (Sandbox Code Playgroud) 您能否解释为什么某些特征向量(2-4)存在符号差异?这种差异是否会影响进一步计算中的进一步计算,例如降维?
MATLAB:
N = 5000;
dataA = rand(N,5);
covA = cov(dataA);
%covA = dataA*dataA'/(length(dataA)-1);
covA = covA + eps.*eye(size(covA));
[~,pA] = chol(covA);
assert(pA==0,'A is not possitive definite')
dataB = rand(N,5);
covB = cov(dataB);
%covB = dataB*dataB'/(length(dataB)-1);
covB = covB + eps.*eye(size(covB));
[~,pB] = chol(covB);
assert(pB==0,'B is not possitive definite')
[V,D] = eig(covA, covB);
V =
-0.4241 -1.0891 1.8175 2.4067 -1.3032
1.4445 -1.8960 -1.4118 -0.6514 -2.0075
0.1214 -2.5039 0.3332 -0.1705 2.3609
-2.1235 -0.7007 1.1632 -2.1532 -1.0554
-2.2599 -0.4405 -2.2236 1.2545 0.0760 …Run Code Online (Sandbox Code Playgroud) eigenvector ×10
python ×5
numpy ×4
scipy ×3
eigenvalue ×2
matlab ×2
matrix ×2
python-2.7 ×2
math ×1
opencv ×1
svd ×1