2
$\begingroup$

I'm currently working with a dataset of 4000 molecules and their values for a given physical constant. I use the molecules to calculate ~1400 descriptors, then use the descriptors to predict the constants via ML algorithms from scikit-learn.

When using PCA, my understanding is the principal components are constructed from the whole dataset - I can then split the principal components into training and testing sets for use with a regression model (eg scikit-learn's GradientBoostingRegressor).

If I want to use the resulting model to predict targets for new molecules with no associated constant, can PCA still be applied? I can still calculate the original 1400 descriptors, but I'm not sure if the PCA steps can/should be completed without retraining the predictive model on the new output.

$\endgroup$
1
  • 2
    $\begingroup$ That's not the right way to do it. You must first split the data into train and test. Then run PCA on train only (but save the weight matrix). Then build the machine learning model. Then utilize the weight matrix of PCA from train stage to map the test set. Then check the ML model. $\endgroup$ Commented Jan 23, 2020 at 11:22

1 Answer 1

1
$\begingroup$

A key idea to remember about a test or validation set in machine learning is that you use it to mimic the real application of using your model to make predictions on data where you truly don’t know the answer. Do your split; then ignore the holdout data for the remainder of the training. In your case, this is exactly what Cagdas Ozgenc described: diagonalize the covariance matrix of just the training data, and then use those eigenvectors on the holdout data to create the out-of-sample principal components.

Once you’re happy with how you’ve tuned your model, then you would combine all data and train the model on everything, but at that point, you’ve decided that you think the model is sufficiently general and will not overfit so badly. This would be like Apple training Siri on 800 in-sample data points and then validating on 200 out-of-sample data points. Then they figure out what model they want. Then they train the model parameters on all 1000 data points, and that model is what makes it into iPhones that people buy.

$\endgroup$

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.