|
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 (absolute_import, division, print_function, |
| 9 | + unicode_literals) |
10 | 10 |
|
11 | 11 | import matplotlib.pyplot as plt
|
12 | 12 | import numpy as np
|
@@ -95,6 +95,7 @@ def plot_pca_component_variance(clf, title='PCA Component Explained Variances',
|
95 | 95 |
|
96 | 96 |
|
97 | 97 | def plot_pca_2d_projection(clf, X, y, title='PCA 2-D Projection',
|
| 98 | + dimensions=[0, 1], |
98 | 99 | biplot=False, feature_labels=None,
|
99 | 100 | ax=None, figsize=None, cmap='Spectral',
|
100 | 101 | title_fontsize="large", text_fontsize="medium",
|
@@ -172,31 +173,31 @@ def plot_pca_2d_projection(clf, X, y, title='PCA 2-D Projection',
|
172 | 173 | colors = plt.cm.get_cmap(cmap)(np.linspace(0, 1, len(classes)))
|
173 | 174 |
|
174 | 175 | for label, color in zip(classes, colors):
|
175 |
| - ax.scatter(transformed_X[y == label, 0], transformed_X[y == label, 1], |
| 176 | + ax.scatter(transformed_X[y == label, dimensions[0]], transformed_X[y == label, dimensions[1]], |
176 | 177 | alpha=0.8, lw=2, label=label, color=color)
|
177 | 178 |
|
178 | 179 | if label_dots:
|
179 |
| - for dot in transformed_X[y == label, 0:2]: |
| 180 | + for dot in transformed_X[y == label, dimensions]: |
180 | 181 | ax.text(*dot, label)
|
181 | 182 |
|
182 | 183 | if biplot:
|
183 |
| - xs = transformed_X[:, 0] |
184 |
| - ys = transformed_X[:, 1] |
185 |
| - vectors = np.transpose(clf.components_[:2, :]) |
| 184 | + xs = transformed_X[:, dimensions[0]] |
| 185 | + ys = transformed_X[:, dimensions[1]] |
| 186 | + vectors = np.transpose(clf.components_[dimensions, :]) |
186 | 187 | vectors_scaled = vectors * [xs.max(), ys.max()]
|
187 | 188 | for i in range(vectors.shape[0]):
|
188 |
| - ax.annotate("", xy=(vectors_scaled[i, 0], vectors_scaled[i, 1]), |
| 189 | + ax.annotate("", xy=(vectors_scaled[i, dimensions[0]], vectors_scaled[i, dimensions[1]]), |
189 | 190 | xycoords='data', xytext=(0, 0), textcoords='data',
|
190 | 191 | arrowprops={'arrowstyle': '-|>', 'ec': 'r'})
|
191 | 192 |
|
192 |
| - ax.text(vectors_scaled[i, 0] * 1.05, vectors_scaled[i, 1] * 1.05, |
| 193 | + ax.text(vectors_scaled[i, dimensions[0]] * 1.05, vectors_scaled[i, dimensions[1]] * 1.05, |
193 | 194 | feature_labels[i] if feature_labels else "Variable" + str(i),
|
194 | 195 | color='b', fontsize=text_fontsize)
|
195 | 196 |
|
196 | 197 | ax.legend(loc='best', shadow=False, scatterpoints=1,
|
197 | 198 | fontsize=text_fontsize)
|
198 |
| - ax.set_xlabel('First Principal Component', fontsize=text_fontsize) |
199 |
| - ax.set_ylabel('Second Principal Component', fontsize=text_fontsize) |
| 199 | + ax.set_xlabel(f'Principal Component {dimensions[0]+1}', fontsize=text_fontsize) |
| 200 | + ax.set_ylabel(f'Principal Component {dimension[1]+1}', fontsize=text_fontsize) |
200 | 201 | ax.tick_params(labelsize=text_fontsize)
|
201 | 202 |
|
202 | 203 | return ax
|
0 commit comments