We can use $z^T (X^TX)z = (Xz)^T(Xz)=||Xz||^2_2 \geq 0$ to prove $X^TX$ is positive semidefinite. However, when I use numpy.linalg.eig to compute the eigenvalues of dot product matrix, I cannot get all positive eigenvalues. How to explain for it?
import numpy as np
import math
np.random.seed(33)
X = np.random.randint(10, size=(2,6))
print(X)
Kernel_Matrix = np.dot(X.T,X)
eigenvalue, eigenvector = np.linalg.eig(Kernel_Matrix)
print(eigenvalue > 0)
print(eigenvalue)
Output:
[[4 7 8 2 2 9]
[9 3 6 3 3 1]]
[[ 97 55 86 35 35 45]
[ 55 58 74 23 23 66]
[ 86 74 100 34 34 78]
[ 35 23 34 13 13 21]
[ 35 23 34 13 13 21]
[ 45 66 78 21 21 82]]
[ True True False False True False]
[ 3.12680220e+02 5.03197805e+01 -9.39575362e-15 -1.44635182e-14
1.43755791e-16 -7.87904824e-32]
print(eigenvalue > -1e-12), you should find that all eigenvalues are "positive up to a small margin of error". – Ben Grossmann Oct 01 '20 at 15:00X = np.random.randint(10, size=(6,2))andKernel_Matrix = np.dot(X.T,X)to build $X^TX$ as a size 6 matrix. I still get same result. – Z Mario Oct 01 '20 at 15:08numpy.linalg.eigh. This is a specialized routine for complex-hermitian or real-symmetric matrices. This may provide a bit more accuracy, and will certainly suppress those imaginary numbers, to convince you that your matrix is semidefinite. – Charlie S Oct 01 '20 at 15:09