1
$\begingroup$

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

pca.transform(data) 

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>
    pca.transform(data)

TypeError: transform() missing 1 required positional argument: 'X'
$\endgroup$
1
$\begingroup$

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

$\endgroup$
  • $\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 Jul 5 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$ – vipin bansal Jul 5 at 9:13
1
$\begingroup$

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)
pca.fit(train_features)
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) 
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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