Using PCA to reduce dimensionality of training and testing data












3












$begingroup$


I've read so many contradicting opinions that I feel like I need to ask the question myself.



Say I use PCA on a dataset with 60 variables and find that I can explain 98% of variance with 6 principal components and I get a decent model predicting what I want.



Now I get some new data (testing), this data should then be translated to the same "PCA space" in order for my model to interpret it right? So I would scale it using the same scaling used on my training data and then use the loading scores from the original PCA to translate my new data to "PCA space"?



The reason I'm asking is that I've seen tons of people doing PCA before doing test/train splits so their testing data is already "transformed", this seems like a mistake to me? Shouldn't the PCA be used on the training data exclusively and then using the loadings from that PCA translate the testing data to the same dimensionality?










share|cite|improve this question









New contributor




DasBoot is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$








  • 3




    $begingroup$
    I think you are rightfully concerned about the later approach. For test data, it is best practice to simply pretend it doesn't exist during model build and only use it at the very end.
    $endgroup$
    – bi_scholar
    2 hours ago






  • 2




    $begingroup$
    @bi_scholar that's exactly my thinking, treat testing data as non-existing until the model is actually ready to be tested.
    $endgroup$
    – DasBoot
    2 hours ago






  • 2




    $begingroup$
    Possible duplicate of PCA and the train/test split
    $endgroup$
    – amoeba
    1 hour ago
















3












$begingroup$


I've read so many contradicting opinions that I feel like I need to ask the question myself.



Say I use PCA on a dataset with 60 variables and find that I can explain 98% of variance with 6 principal components and I get a decent model predicting what I want.



Now I get some new data (testing), this data should then be translated to the same "PCA space" in order for my model to interpret it right? So I would scale it using the same scaling used on my training data and then use the loading scores from the original PCA to translate my new data to "PCA space"?



The reason I'm asking is that I've seen tons of people doing PCA before doing test/train splits so their testing data is already "transformed", this seems like a mistake to me? Shouldn't the PCA be used on the training data exclusively and then using the loadings from that PCA translate the testing data to the same dimensionality?










share|cite|improve this question









New contributor




DasBoot is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$








  • 3




    $begingroup$
    I think you are rightfully concerned about the later approach. For test data, it is best practice to simply pretend it doesn't exist during model build and only use it at the very end.
    $endgroup$
    – bi_scholar
    2 hours ago






  • 2




    $begingroup$
    @bi_scholar that's exactly my thinking, treat testing data as non-existing until the model is actually ready to be tested.
    $endgroup$
    – DasBoot
    2 hours ago






  • 2




    $begingroup$
    Possible duplicate of PCA and the train/test split
    $endgroup$
    – amoeba
    1 hour ago














3












3








3


1



$begingroup$


I've read so many contradicting opinions that I feel like I need to ask the question myself.



Say I use PCA on a dataset with 60 variables and find that I can explain 98% of variance with 6 principal components and I get a decent model predicting what I want.



Now I get some new data (testing), this data should then be translated to the same "PCA space" in order for my model to interpret it right? So I would scale it using the same scaling used on my training data and then use the loading scores from the original PCA to translate my new data to "PCA space"?



The reason I'm asking is that I've seen tons of people doing PCA before doing test/train splits so their testing data is already "transformed", this seems like a mistake to me? Shouldn't the PCA be used on the training data exclusively and then using the loadings from that PCA translate the testing data to the same dimensionality?










share|cite|improve this question









New contributor




DasBoot is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$




I've read so many contradicting opinions that I feel like I need to ask the question myself.



Say I use PCA on a dataset with 60 variables and find that I can explain 98% of variance with 6 principal components and I get a decent model predicting what I want.



Now I get some new data (testing), this data should then be translated to the same "PCA space" in order for my model to interpret it right? So I would scale it using the same scaling used on my training data and then use the loading scores from the original PCA to translate my new data to "PCA space"?



The reason I'm asking is that I've seen tons of people doing PCA before doing test/train splits so their testing data is already "transformed", this seems like a mistake to me? Shouldn't the PCA be used on the training data exclusively and then using the loadings from that PCA translate the testing data to the same dimensionality?







regression machine-learning pca dimensionality-reduction






share|cite|improve this question









New contributor




DasBoot is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|cite|improve this question









New contributor




DasBoot is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|cite|improve this question




share|cite|improve this question








edited 1 hour ago









amoeba

59.1k15202261




59.1k15202261






New contributor




DasBoot is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 4 hours ago









DasBootDasBoot

183




183




New contributor




DasBoot is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





DasBoot is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






DasBoot is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 3




    $begingroup$
    I think you are rightfully concerned about the later approach. For test data, it is best practice to simply pretend it doesn't exist during model build and only use it at the very end.
    $endgroup$
    – bi_scholar
    2 hours ago






  • 2




    $begingroup$
    @bi_scholar that's exactly my thinking, treat testing data as non-existing until the model is actually ready to be tested.
    $endgroup$
    – DasBoot
    2 hours ago






  • 2




    $begingroup$
    Possible duplicate of PCA and the train/test split
    $endgroup$
    – amoeba
    1 hour ago














  • 3




    $begingroup$
    I think you are rightfully concerned about the later approach. For test data, it is best practice to simply pretend it doesn't exist during model build and only use it at the very end.
    $endgroup$
    – bi_scholar
    2 hours ago






  • 2




    $begingroup$
    @bi_scholar that's exactly my thinking, treat testing data as non-existing until the model is actually ready to be tested.
    $endgroup$
    – DasBoot
    2 hours ago






  • 2




    $begingroup$
    Possible duplicate of PCA and the train/test split
    $endgroup$
    – amoeba
    1 hour ago








3




3




$begingroup$
I think you are rightfully concerned about the later approach. For test data, it is best practice to simply pretend it doesn't exist during model build and only use it at the very end.
$endgroup$
– bi_scholar
2 hours ago




$begingroup$
I think you are rightfully concerned about the later approach. For test data, it is best practice to simply pretend it doesn't exist during model build and only use it at the very end.
$endgroup$
– bi_scholar
2 hours ago




2




2




$begingroup$
@bi_scholar that's exactly my thinking, treat testing data as non-existing until the model is actually ready to be tested.
$endgroup$
– DasBoot
2 hours ago




$begingroup$
@bi_scholar that's exactly my thinking, treat testing data as non-existing until the model is actually ready to be tested.
$endgroup$
– DasBoot
2 hours ago




2




2




$begingroup$
Possible duplicate of PCA and the train/test split
$endgroup$
– amoeba
1 hour ago




$begingroup$
Possible duplicate of PCA and the train/test split
$endgroup$
– amoeba
1 hour ago










1 Answer
1






active

oldest

votes


















4












$begingroup$

Yes this is a common way of overfitting your model to the test data. In NLP a similar mistake is to do vocabulary selection and bag-of-words vectorization on the full train/test data.



This is a bit insidious since doing model selection is a lot easier with most tools once you got your feature matrix. In addition the "boost" you get is not alarmingly big so it is tempting to just think your model is great and pat yourself on the back.



On a positive note I think this was a lot more common 5-10 ten years ago and most practitioners are wise to this error today.






share|cite|improve this answer








New contributor




sniggatooth is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






$endgroup$









  • 1




    $begingroup$
    +1. I voted to close this Q as a duplicate of stats.stackexchange.com/questions/55718/…, but I fully agree with this answer.
    $endgroup$
    – amoeba
    1 hour ago










  • $begingroup$
    (If you want, you can post a version of this answer in that thread which is our "canonical" thread on this topic -- lots of questions get closed as duplicate of that one.)
    $endgroup$
    – amoeba
    1 hour ago










  • $begingroup$
    thanks @amoeba, that link provided some great insight as well as confirming my suspicion.
    $endgroup$
    – DasBoot
    23 mins ago











Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
});
});
}, "mathjax-editing");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "65"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});






DasBoot is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstats.stackexchange.com%2fquestions%2f388740%2fusing-pca-to-reduce-dimensionality-of-training-and-testing-data%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









4












$begingroup$

Yes this is a common way of overfitting your model to the test data. In NLP a similar mistake is to do vocabulary selection and bag-of-words vectorization on the full train/test data.



This is a bit insidious since doing model selection is a lot easier with most tools once you got your feature matrix. In addition the "boost" you get is not alarmingly big so it is tempting to just think your model is great and pat yourself on the back.



On a positive note I think this was a lot more common 5-10 ten years ago and most practitioners are wise to this error today.






share|cite|improve this answer








New contributor




sniggatooth is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






$endgroup$









  • 1




    $begingroup$
    +1. I voted to close this Q as a duplicate of stats.stackexchange.com/questions/55718/…, but I fully agree with this answer.
    $endgroup$
    – amoeba
    1 hour ago










  • $begingroup$
    (If you want, you can post a version of this answer in that thread which is our "canonical" thread on this topic -- lots of questions get closed as duplicate of that one.)
    $endgroup$
    – amoeba
    1 hour ago










  • $begingroup$
    thanks @amoeba, that link provided some great insight as well as confirming my suspicion.
    $endgroup$
    – DasBoot
    23 mins ago
















4












$begingroup$

Yes this is a common way of overfitting your model to the test data. In NLP a similar mistake is to do vocabulary selection and bag-of-words vectorization on the full train/test data.



This is a bit insidious since doing model selection is a lot easier with most tools once you got your feature matrix. In addition the "boost" you get is not alarmingly big so it is tempting to just think your model is great and pat yourself on the back.



On a positive note I think this was a lot more common 5-10 ten years ago and most practitioners are wise to this error today.






share|cite|improve this answer








New contributor




sniggatooth is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






$endgroup$









  • 1




    $begingroup$
    +1. I voted to close this Q as a duplicate of stats.stackexchange.com/questions/55718/…, but I fully agree with this answer.
    $endgroup$
    – amoeba
    1 hour ago










  • $begingroup$
    (If you want, you can post a version of this answer in that thread which is our "canonical" thread on this topic -- lots of questions get closed as duplicate of that one.)
    $endgroup$
    – amoeba
    1 hour ago










  • $begingroup$
    thanks @amoeba, that link provided some great insight as well as confirming my suspicion.
    $endgroup$
    – DasBoot
    23 mins ago














4












4








4





$begingroup$

Yes this is a common way of overfitting your model to the test data. In NLP a similar mistake is to do vocabulary selection and bag-of-words vectorization on the full train/test data.



This is a bit insidious since doing model selection is a lot easier with most tools once you got your feature matrix. In addition the "boost" you get is not alarmingly big so it is tempting to just think your model is great and pat yourself on the back.



On a positive note I think this was a lot more common 5-10 ten years ago and most practitioners are wise to this error today.






share|cite|improve this answer








New contributor




sniggatooth is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






$endgroup$



Yes this is a common way of overfitting your model to the test data. In NLP a similar mistake is to do vocabulary selection and bag-of-words vectorization on the full train/test data.



This is a bit insidious since doing model selection is a lot easier with most tools once you got your feature matrix. In addition the "boost" you get is not alarmingly big so it is tempting to just think your model is great and pat yourself on the back.



On a positive note I think this was a lot more common 5-10 ten years ago and most practitioners are wise to this error today.







share|cite|improve this answer








New contributor




sniggatooth is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|cite|improve this answer



share|cite|improve this answer






New contributor




sniggatooth is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









answered 2 hours ago









sniggatoothsniggatooth

561




561




New contributor




sniggatooth is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





sniggatooth is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






sniggatooth is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 1




    $begingroup$
    +1. I voted to close this Q as a duplicate of stats.stackexchange.com/questions/55718/…, but I fully agree with this answer.
    $endgroup$
    – amoeba
    1 hour ago










  • $begingroup$
    (If you want, you can post a version of this answer in that thread which is our "canonical" thread on this topic -- lots of questions get closed as duplicate of that one.)
    $endgroup$
    – amoeba
    1 hour ago










  • $begingroup$
    thanks @amoeba, that link provided some great insight as well as confirming my suspicion.
    $endgroup$
    – DasBoot
    23 mins ago














  • 1




    $begingroup$
    +1. I voted to close this Q as a duplicate of stats.stackexchange.com/questions/55718/…, but I fully agree with this answer.
    $endgroup$
    – amoeba
    1 hour ago










  • $begingroup$
    (If you want, you can post a version of this answer in that thread which is our "canonical" thread on this topic -- lots of questions get closed as duplicate of that one.)
    $endgroup$
    – amoeba
    1 hour ago










  • $begingroup$
    thanks @amoeba, that link provided some great insight as well as confirming my suspicion.
    $endgroup$
    – DasBoot
    23 mins ago








1




1




$begingroup$
+1. I voted to close this Q as a duplicate of stats.stackexchange.com/questions/55718/…, but I fully agree with this answer.
$endgroup$
– amoeba
1 hour ago




$begingroup$
+1. I voted to close this Q as a duplicate of stats.stackexchange.com/questions/55718/…, but I fully agree with this answer.
$endgroup$
– amoeba
1 hour ago












$begingroup$
(If you want, you can post a version of this answer in that thread which is our "canonical" thread on this topic -- lots of questions get closed as duplicate of that one.)
$endgroup$
– amoeba
1 hour ago




$begingroup$
(If you want, you can post a version of this answer in that thread which is our "canonical" thread on this topic -- lots of questions get closed as duplicate of that one.)
$endgroup$
– amoeba
1 hour ago












$begingroup$
thanks @amoeba, that link provided some great insight as well as confirming my suspicion.
$endgroup$
– DasBoot
23 mins ago




$begingroup$
thanks @amoeba, that link provided some great insight as well as confirming my suspicion.
$endgroup$
– DasBoot
23 mins ago










DasBoot is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















DasBoot is a new contributor. Be nice, and check out our Code of Conduct.













DasBoot is a new contributor. Be nice, and check out our Code of Conduct.












DasBoot is a new contributor. Be nice, and check out our Code of Conduct.
















Thanks for contributing an answer to Cross Validated!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


Use MathJax to format equations. MathJax reference.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstats.stackexchange.com%2fquestions%2f388740%2fusing-pca-to-reduce-dimensionality-of-training-and-testing-data%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Ponta tanko

Tantalo (mitologio)

Erzsébet Schaár