Using PCA to reduce dimensionality of training and testing data
$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?
regression machine-learning pca dimensionality-reduction
New contributor
$endgroup$
add a comment |
$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?
regression machine-learning pca dimensionality-reduction
New contributor
$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
add a comment |
$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?
regression machine-learning pca dimensionality-reduction
New contributor
$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
regression machine-learning pca dimensionality-reduction
New contributor
New contributor
edited 1 hour ago
amoeba
59.1k15202261
59.1k15202261
New contributor
asked 4 hours ago
DasBootDasBoot
183
183
New contributor
New contributor
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
$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.
New contributor
$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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
$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.
New contributor
$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
add a comment |
$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.
New contributor
$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
add a comment |
$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.
New contributor
$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.
New contributor
New contributor
answered 2 hours ago
sniggatoothsniggatooth
561
561
New contributor
New contributor
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
add a comment |
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
add a comment |
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.
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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