scikit-learn provides the pca class in its decomposition package in this examplewe can clearly see that one dimension should be enough to describe the data we can specify that using the n_components parameterfrom sklearn import linear_modeldecompositiondatasets pca decomposition pca(n_components= here we can also use pca' fit(and transform(methods (or its fit_ transform(combinationto analyze the data and project it into the transformed feature spacextrans pca fit_transform(xxtrans contains only one dimensionas we have specified you can see the result in the right graph the outcome is even linearly separable in this case we would not even need complex classifier to distinguish between both classes to get an understanding of the reconstruction errorwe can have look at the variance of the data that we have retained in the transformationprint(pca explained_variance_ratio_ this means that after going from two dimensions to one dimensionwe are still left with percent of the variance of courseit is not always that simple oftenwe don' know what number of dimensions is advisable upfront in that casewe leave the n_components parameter unspecified when initializing pca to let it calculate the full transformation after fitting the dataexplained_variance_ratio_ contains an array of ratios in decreasing order the first value is the ratio of the basis vector describing the direction of the highest variancethe second value is the ratio of the direction of the second highest varianceand so on after plotting this arraywe quickly get feel of how many components we would needthe number of components immediately before the chart has its elbow is often good guess plots displaying the explained variance over the number of components is called scree plot nice example of combining scree plot with grid search to find the best setting for the classification problem can be found at auto_examples/plot_digits_pipe html