3
$\begingroup$

I tried to search for an answer about the evaluation and interpretation of a pattern learned with PCA, on data from a Testing Set, but I found no answer. Let me explain my situation:

My Data Mining task is to implement an instance of the KDD process in order to learn an accurate classifier of Windows applications.

I have a single "dataset.csv" file on which I do the initial split into Training Set and Testing Set (the entire learning & validation phases will focus exclusively on the Training Set).

My choice is to learn a classifier with the Gaussian Naive Bayes algorithm, so I decide to apply the transformation of my Training Set through the PCA, in order to ensure independence between the attributes (so, for example, on my Training Set with 50 features, I decide to transform it into a new Training Set with 10 Principal Components and learn & build from it my pattern with the Gaussian Naive Bayes and identifying the best configuration with a GridSearchCV).

Once learned my pattern, I have to perform the final evaluation on the whole Testing Set splitted at the start.

Here's my problem: The pattern was learned on a Training Set composed of 10 columns (i.e. 10 Principal Components), but my Testing Set is composed of 50 features (like my Training Set), and this conflicts with the learned pattern because the number of columns is different and I cannot make predictions. What should I do?

EDIT

To be correct, I specify the small details of my problem so that you can give me as complete an answer as possible.

I will describe very quickly what I did:

  1. I split the starting dataset into Training Set and Testing Set using sklearn.model_selection.train_test_split with test_size=0.3 (i.e. 30%).
  2. After splitting, I performed (on the Training Set) Data Cleaning (replacing any missing values with statistical.mode for each column) and Data Scaling using sklearn.preprocessing.MinMaxScaler.
  3. After this little PreProcessing, I performed the PCA on the entire Training Set's indipendent variables, thus obtaining the new Training Set composed of 10 columns (i.e. the 10 Principal Components) and then I used sklearn.model_selection.GridSearchCV to learn the best configuration of sklearn.naive_bayes.GaussianNB (which is the estimator parameter), for the different var_smoothing values that I insert in a list and associate with the param_grid parameter, and setting cv=5 parameter for K-Fold Cross Validation used by GridSearchCV and finally fit on the entire Training Set.

This is how I learn my model, which now I have to test on the Testing Set.

I would like slightly more clarity on what should be done from this point on, so my doubt is: Would it be correct to perform the PCA exclusively on the Testing Set, calculating the same number of Principal Components and then evaluating?

$\endgroup$
9
  • $\begingroup$ One would apply the PCA to all dataset before splitting to test/train. $\endgroup$ Jun 29 at 14:33
  • 1
    $\begingroup$ That's totally wrong, the reason is that you should train your model only on the training data, without using any information regarding the testing data. If you apply PCA on the whole data (including the test data) before training the model (so, before the split), then you in fact use some information from the test data. Thus, you cannot really judge the behaviour of your model using the test data, because it is not an unseen data anymore. $\endgroup$
    – DavidZoy
    Jun 29 at 15:34
  • $\begingroup$ Not really. PCA is "unsupervised". There is no peaking here. Test set is still unseen data, instances on the test set are not used in building the model. $\endgroup$ Jun 29 at 18:50
  • $\begingroup$ @MehmetSuzen it causes data leakage, a somewhat related discussion: stats.stackexchange.com/questions/55718/… $\endgroup$
    – gunes
    Jun 29 at 19:02
  • 1
    $\begingroup$ @MehmetSuzen thanks for the article link. I couldn't exactly find parts supporting your idea (just skimmed). It's not only SO community, for example, the data leakage section in sklearn documentation exactly describes this kind of scenario: scikit-learn.org/stable/common_pitfalls.html . Simply, introducing information about the test set may introduce some trends in features that may not be readily available in training data. Thus, target or not, introduction of test set always carry a danger of leakage, despite one might get away with it on occasions. $\endgroup$
    – gunes
    Jun 29 at 19:39
3
$\begingroup$

Just a minor correction: after PCA, you use the projections onto the principal components as features, not the PCs themselves. But, you'll have reduced set of features as you mentioned, say 10.

You'll set up a pipeline (e.g. you can utilize the Pipeline object in scikit-learn as I understand from your notation, you're using it) with steps PCA and GaussianNaiveBayes, and use grid search for hyper-parameter optimization (HPO).

This is different your proposed solution. In your second and third steps, you also introduce some leakage to the validation folds because you did PCA & data scaling beforehand. As I mentioned above, you should think all the operations you performed as a single model/pipeline and apply CV to it. This is harder to implement in code if you don't use pipelines, but it's the right thing to do.

Finally, with the best HPs selected, the final model (pipeline) will be fitted on the training set. This fitted model can predict the test set as well, because the pipeline has PCA step with PCs found for the training set, and there will be no dimension mismatch issue.

To reiterate, you won't fit PCA or scaling to the test set, you'll use fitted models/objects on the training set to be applied on the test set.

$\endgroup$
9
  • $\begingroup$ I didn't know the Pipeline library from sklearn, so I didn't use it. To be correct, I will add a little more details to the question, so that you can give me a more precise answer. $\endgroup$
    – DavidZoy
    Jun 29 at 20:49
  • $\begingroup$ @DavidZoy You don't have to use it, it's just very convenient for this kind of situations. I've amended my answer. $\endgroup$
    – gunes
    Jun 29 at 21:25
  • 1
    $\begingroup$ Use grid.best_estimator_ for the fitted model $\endgroup$
    – gunes
    Jul 1 at 18:52
  • 1
    $\begingroup$ You should have access to them in the best_estimator_ object. $\endgroup$
    – gunes
    Jul 1 at 19:58
  • 1
    $\begingroup$ I found them, thank you again! $\endgroup$
    – DavidZoy
    Jul 1 at 20:06

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.