5
5
properties shared by scikit-learn estimators. The specific requirements are
6
6
documented per function.
7
7
"""
8
- from __future__ import absolute_import , division , print_function , \
9
- unicode_literals
8
+ from __future__ import (
9
+ absolute_import , division , print_function , unicode_literals
10
+ )
10
11
11
12
import matplotlib .pyplot as plt
12
13
import numpy as np
@@ -97,7 +98,8 @@ def plot_pca_component_variance(clf, title='PCA Component Explained Variances',
97
98
def plot_pca_2d_projection (clf , X , y , title = 'PCA 2-D Projection' ,
98
99
biplot = False , feature_labels = None ,
99
100
ax = None , figsize = None , cmap = 'Spectral' ,
100
- title_fontsize = "large" , text_fontsize = "medium" ):
101
+ title_fontsize = "large" , text_fontsize = "medium" ,
102
+ dimensions = [0 , 1 ], label_dots = False , ):
101
103
"""Plots the 2-dimensional projection of PCA on a given dataset.
102
104
103
105
Args:
@@ -163,21 +165,27 @@ def plot_pca_2d_projection(clf, X, y, title='PCA 2-D Projection',
163
165
fig , ax = plt .subplots (1 , 1 , figsize = figsize )
164
166
165
167
ax .set_title (title , fontsize = title_fontsize )
166
- classes = np .unique (np .array (y ))
168
+ # Get unique classes from y, preserving order of class occurence in y
169
+ _ , class_indexes = np .unique (np .array (y ), return_index = True )
170
+ classes = np .array (y )[np .sort (class_indexes )]
167
171
168
172
colors = plt .cm .get_cmap (cmap )(np .linspace (0 , 1 , len (classes )))
169
173
170
174
for label , color in zip (classes , colors ):
171
- ax .scatter (transformed_X [y == label , 0 ] , transformed_X [y == label , 1 ],
175
+ ax .scatter (transformed_X [y == label , dimensions [ 0 ]] , transformed_X [y == label , dimensions [ 1 ] ],
172
176
alpha = 0.8 , lw = 2 , label = label , color = color )
173
177
178
+ if label_dots :
179
+ for dot in transformed_X [y == label ][:, dimensions ]:
180
+ ax .text (* dot , label )
181
+
174
182
if biplot :
175
- xs = transformed_X [:, 0 ]
176
- ys = transformed_X [:, 1 ]
177
- vectors = np .transpose (clf .components_ [: 2 , :])
183
+ xs = transformed_X [:, dimensions [ 0 ] ]
184
+ ys = transformed_X [:, dimensions [ 1 ] ]
185
+ vectors = np .transpose (clf .components_ [dimensions , :])
178
186
vectors_scaled = vectors * [xs .max (), ys .max ()]
179
187
for i in range (vectors .shape [0 ]):
180
- ax .annotate ("" , xy = (vectors_scaled [i , 0 ] , vectors_scaled [i , 1 ]),
188
+ ax .annotate ("" , xy = (vectors_scaled [i , dimensions [ 0 ]] , vectors_scaled [i , dimensions [ 1 ] ]),
181
189
xycoords = 'data' , xytext = (0 , 0 ), textcoords = 'data' ,
182
190
arrowprops = {'arrowstyle' : '-|>' , 'ec' : 'r' })
183
191
@@ -187,8 +195,8 @@ def plot_pca_2d_projection(clf, X, y, title='PCA 2-D Projection',
187
195
188
196
ax .legend (loc = 'best' , shadow = False , scatterpoints = 1 ,
189
197
fontsize = text_fontsize )
190
- ax .set_xlabel ('First Principal Component' , fontsize = text_fontsize )
191
- ax .set_ylabel ('Second Principal Component' , fontsize = text_fontsize )
198
+ ax .set_xlabel (f' Principal Component { dimensions [ 0 ] + 1 } ' , fontsize = text_fontsize )
199
+ ax .set_ylabel (f' Principal Component { dimensions [ 1 ] + 1 } ' , fontsize = text_fontsize )
192
200
ax .tick_params (labelsize = text_fontsize )
193
201
194
202
return ax
0 commit comments