0
$\begingroup$

I'm using PCA to reduce my feature vector dimension. I'm saving its model and transformed output like this:

from sklearn.externals import joblib    
from sklearn.decomposition import PCA

pca_transformed_fit = pca.fit(data)
pca_transformed = np.around(pca.fit_transform(data), 10)
np.savetxt('reducedFeatures.txt', pca_transformed, delimiter=',', newline='\n', fmt='%1.10f')
joblib.dump(pca_transformed_fit, 'pca.model')

data is the output of VGG16. As I want to have only 10 decimal places, I rounded the transformed output. So I want to use saved model to process feature vectors again with the code below:

from sklearn.externals import joblib

pca = joblib.load('pca.model')
pca_transformed = np.around(pca.transform(samples), 10)
np.savetxt('reducedFeatures_again.txt', pca_transformed, delimiter=',', newline='\n', fmt='%1.10f')

BUT the problem is the outputs are not the same for a given feature vector.

enter image description here

As long as these transformed data are very meaningful, what can I do to have the exact output??
If you need more clarification, just ask for it. Thank you!

$\endgroup$

1 Answer 1

1
$\begingroup$

You're fitting the PCA twice in the first code snippet. First time here: pca_transformed_fit = pca.fit(data), second time here: pca_transformed = np.around(pca.fit_transform(data), 10). I guess there are some small computational errors that result in a slightly different decomposition upon every repeated call to fit() or fit_transform(). Replace fit_transform() with transform() and it should work (at least it worked for me when I tested it).

$\endgroup$
2
  • $\begingroup$ Sir, first of all THANK YOU! I don't know why I did this mess!!! It's obvious from docs that fit_transform produce different results than fit then transform. Thank you for your help. :) $\endgroup$ Aug 3, 2019 at 9:32
  • $\begingroup$ you're welcome! $\endgroup$ Aug 4, 2019 at 15:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.