I'm trying to write a random forest classifier for a very large dataset, as such as part of the pre-processing i have applied PCA to reduce from 643 features to 5 PC's. Is it possible to export these settings so I can


in another program.

I have been able to do this to the the scaler using pickle but when i run the transfrom i get given the error: Traceback (most recent call last):

  File "<ipython-input-35-68c9849c2acc>", line 1, in <module>

TypeError: transform() missing 1 required positional argument: 'X'

2 Answers 2


Ideally PCA should not be used as a part of pre-processing feature reduction.

Anyhow regarding saving and reusing PCA model, sharing a basic code snippet which is working very fine in my case(as I'm not able to reproduce the error case).

from sklearn.decomposition import PCA
import pickle as pk
pca = PCA(n_components=2)
result = pca.fit_transform(X) # Assume X is having more than 2 dimensions    
pk.dump(pca, open("pca.pkl","wb"))
# later reload the pickle file
pca_reload = pk.load(open("pca.pkl",'rb'))
result_new = pca_reload .transform(X)

# result and result_new same in my case

  • $\begingroup$ Why shouldn't PCA be used for feature reduction? In this case, i have 643 features that are unnamed and already physically irrelevant (its radar readout data across several GHz). The majority of that data will likely be irrelevant and just lead to overfitting. What should i use if not PCA??? $\endgroup$
    – Tasty213
    Commented Jul 5, 2019 at 8:34
  • $\begingroup$ PCA aggregates the information of different feature and present in a matrix format i.e a covariance matrix. So if you select less component(columns) other than the actual one, in that case you always lose some relevant information, which is not good for model training. You can use other techniques like P- value process, where it derives the relation between different features and present its value between -1 and 1. Where as -1 represent values are indirectly proportional and 1 represent directly proportional. towardsdatascience.com/… $\endgroup$ Commented Jul 5, 2019 at 9:13
  • $\begingroup$ @vipinbansal What do you mean by p-value process? The link you have provided more or less suggests to prune features that are correlated. However, PCA does much more than that. PCA checks if some features can be obtained by arbitrary linear combinations of others. Dropping PCA with extremely low magnitudes should result in no information loss. I do agree though that in OP's case compressing from 600 features to 5 is likely overkill $\endgroup$ Commented Nov 14, 2022 at 16:38

The first argument to transform() is the self argument. From your Traceback, it can be concluded that data is being passed to the self argument.

This happens when you do not create an object of the class you want to use your function from. (Assuming the function is not decorated with a @staticmethod, which in the case of transform, is not.)

Check if you have unintentionally initialized pca as pca = PCA.

For pre-processing script -

pca = PCA(n_components=2)
scaled_train_features = pca.transform(train_features)
# save pca in a pickle file
with open('pca.pkl', 'wb') as pickle_file:
        pickle.dump(pca, pickle_file)

For the other script where you want to use the fitted pca -

with open('pca.pkl', 'rb') as pickle_file:
    pca = pickle.load(pickle_file)
scaled_data = pca.transform(data) 

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.