machine learning comparing the accuracy of the original data set with latent variables now that we've decided our data set should be recoded into latent variables rather than the originalsit' time to see how well the new data set works for predicting the quality of wine when compared to the original we'll use the naive bayes classifier algorithm we saw in the previous example for supervised learning to help let' start by seeing how well the original variables could predict the wine quality scores the following listing presents the code to do this listing wine score prediction before principal component analysis from sklearn cross_validation import train_test_split from sklearn naive_bayes import gaussiannb from sklearn metrics import confusion_matrix import pylab as plt gnb gaussiannb(fit gnb fit( ,ypred fit predict(xprint confusion_matrix(pred,yprint confusion_matrix(pred,ytrace(use gaussian distribution naive bayes classifier for estimation fit data predict data for unseen data study confusion matrix count of all correctly classified casesall counts on trace or diagonal summed up after analyzing confusion matrix we can see the naive bayes classifier scores correct predictions out of now we'll run the same prediction testbut starting with only latent variable instead of the original then we'll add anothersee how it didadd anotherand so on to see how the predictive performance improves the following listing shows how this is done listing wine score prediction with increasing number of principal components fit pca model on -variables (featuresthe actual prediction itself using the fitted model array will be filled with correctly predicted observations loops through first detected principal components instantiate pca model with component (first iterationup to components (in th iterationpredicted_correct [ is result in matrix form (actually for in range( , )an array filled with arraysmodel pca(n_components iresults model fit(xuse gaussian distribution naive results transform(xbayes classifier for estimation fit gnb fit( ,ypred fit predict(zpredicted_correct append(confusion_matrix(pred,ytrace()at end of each print predicted_correct iteration we plt plot(predicted_correctprinting this array we append number can see how after each plt show(plot shown easier to see when array plotted iterationnew count of correctly classified observations is appended of correctly classified observations