Hello,
I am exploring PCA with OpenCV and i do not understand the results i am seeing.
When compared to sklearn.decomposition’s PCA the results seem wrong.
- The same numbers appear too frequently and some rows are the same.
- How do you project the samples back in Python? (Validation)
import numpy as np
import cv2 as cv
x1 = np.asarray([1,1,1,1,1,1],dtype=np.float32)
x2 = np.asarray([20,20,20,20,20,20],dtype=np.float32)
x3 = np.asarray([300,300,300,300,300,300],dtype=np.float32)
x4 = np.asarray([400,400,400,400,400,400],dtype=np.float32)
matrix_test = x1
for file in [x2,x3,x4]:
matrix_test = np.vstack((matrix_test, file))
mean, eigenvectors = cv.PCACompute(matrix_test, mean = np.array([]), maxComponents=10)
print(mean)
#[[180.25 180.25 180.25 180.25 180.25 180.25]]
print(eigenvectors)
# [[ 0.40824828 0.40824828 0.40824828 0.40824828 0.40824828 0.40824828]
# [-0.4082483 -0.4082483 -0.4082483 -0.4082483 -0.4082483 -0.4082483 ]
# [ 0.4082483 0.4082483 0.4082483 0.4082483 0.4082483 0.4082483 ]
# [ 0.4082483 0.4082483 0.4082483 0.4082483 0.4082483 0.4082483 ]]
from sklearn.decomposition import PCA
pca = PCA()
components = pca.fit_transform(matrix_test)
print(components)
# [[-4.3907104e+02 -5.0779790e-06 -5.4410724e-13 1.9930799e-20]
# [-3.9253073e+02 -4.5397310e-06 7.3777291e-13 1.2022075e-20]
# [ 2.9332635e+02 3.7512091e-05 6.0963089e-14 1.6197749e-20]
# [ 5.3827533e+02 -2.7894388e-05 6.0963008e-14 1.6197741e-20]]
out = pca.inverse_transform(components)
print(out)
# [[ 0.99998474 0.99998474 0.99998474 0.99998474 0.99998474
# 0.99998474]
# [ 20. 20. 20. 20. 20.
# 20. ]
# [299.99994 300. 300. 300. 300.
# 300. ]
# [400. 400. 400. 400. 400.
# 400. ]]
Am i missing something obvious?