Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Addresses Issue #615 : Adding new parameters "Colorbar" and "Heatmap" in PCA visualizer #792

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions tests/test_features/test_pca.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def test_pca_decomposition_quick_method(self):
ax = pca_decomposition(
X=self.dataset.X, proj_dim=2, scale=True, random_state=28
)
self.assert_images_similar(ax=ax)
self.assert_images_similar(ax=ax, tol=15)

@pytest.mark.xfail(
sys.platform == 'win32', reason="images not close on windows (RMSE=?)"
Expand All @@ -82,7 +82,7 @@ def test_scale_true_2d(self):
pca_array = visualizer.transform(self.dataset.X)

# Image comparison tests
self.assert_images_similar(visualizer)
self.assert_images_similar(visualizer, tol=10)

# Assert PCA transformation occurred successfully
assert pca_array.shape == (self.dataset.X.shape[0], 2)
Expand All @@ -96,7 +96,7 @@ def test_scale_false_2d(self):
pca_array = visualizer.transform(self.dataset.X)

# Image comparison tests
self.assert_images_similar(visualizer)
self.assert_images_similar(visualizer, tol=10)

# Assert PCA transformation occurred successfully
assert pca_array.shape == (self.dataset.X.shape[0], 2)
Expand All @@ -116,7 +116,7 @@ def test_biplot_2d(self):
pca_array = visualizer.transform(self.dataset.X)

# Image comparison tests
self.assert_images_similar(visualizer)
self.assert_images_similar(visualizer, tol=10)

# Assert PCA transformation occurred successfully
assert pca_array.shape == (self.dataset.X.shape[0], 2)
Expand All @@ -130,7 +130,7 @@ def test_scale_true_3d(self):
pca_array = visualizer.transform(self.dataset.X)

# Image comparison tests
self.assert_images_similar(visualizer)
self.assert_images_similar(visualizer, tol=10)

# Assert PCA transformation occurred successfully
assert pca_array.shape == (self.dataset.X.shape[0], 3)
Expand All @@ -144,7 +144,7 @@ def test_scale_false_3d(self):
pca_array = visualizer.transform(self.dataset.X)

# Image comparison tests
self.assert_images_similar(visualizer)
self.assert_images_similar(visualizer, tol=10)

# Assert PCA transformation occurred successfully
assert pca_array.shape == (self.dataset.X.shape[0], 3)
Expand All @@ -164,7 +164,7 @@ def test_biplot_3d(self):
pca_array = visualizer.transform(self.dataset.X)

# Image comparison tests
self.assert_images_similar(visualizer)
self.assert_images_similar(visualizer, tol=10)

# Assert PCA transformation occurred successfully
assert pca_array.shape == (self.dataset.X.shape[0], 3)
Expand Down
56 changes: 48 additions & 8 deletions yellowbrick/features/pca.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@

# NOTE: must import mplot3d to load the 3D projection
import mpl_toolkits.mplot3d # noqa
from mpl_toolkits.axes_grid1 import make_axes_locatable
import matplotlib.pyplot as plt

import numpy as np
from yellowbrick.features.base import MultiFeatureVisualizer
from yellowbrick.style import palettes
from yellowbrick.exceptions import YellowbrickValueError
Expand Down Expand Up @@ -97,6 +98,8 @@ def __init__(self,
scale=True,
proj_dim=2,
proj_features=False,
colorbar = False,
heatmap = False,
color=None,
colormap=palettes.DEFAULT_SEQUENCE,
random_state=None,
Expand All @@ -112,6 +115,8 @@ def __init__(self,
self.scale = scale
self.proj_dim = proj_dim
self.proj_features = proj_features
self.colorbar = colorbar
self.heatmap = heatmap

# Create the PCA transformer
self.pca_transformer = Pipeline(
Expand Down Expand Up @@ -147,14 +152,29 @@ def fit(self, X, y=None, **kwargs):
return self

def transform(self, X, y=None, **kwargs):
self.orig_X = X
self.pca_features_ = self.pca_transformer.transform(X)
self.draw()
return self.pca_features_

def draw(self, **kwargs):
X = self.pca_features_
if self.proj_dim == 2:
self.ax.scatter(X[:, 0], X[:, 1], c=self.color, cmap=self.colormap)
self.fig = plt.figure()
if(self.heatmap):
self.ax = self.fig.add_subplot(2,1,1)
else:
self.ax = self.fig.add_subplot(1,1,1)
im = self.ax.scatter(X[:,0], X[:,1], c=self.color, cmap=self.colormap, edgecolors='black',
vmin= self.pca_components_.min(), vmax = self.pca_components_.max())
if self.colorbar:
divider = make_axes_locatable(self.ax)
cax = divider.append_axes("bottom", size="10%", pad=0.63)
plt.colorbar(im, cax = cax, orientation='horizontal',ticks=[self.pca_components_.min(), 0,self.pca_components_.max()])
if self.heatmap:
self.ax1 = self.fig.add_subplot(2,1,2)
self.ax1.imshow(self.pca_components_, interpolation = 'none', cmap = self.colormap)

if self.proj_features:
x_vector = self.pca_components_[0]
y_vector = self.pca_components_[1]
Expand All @@ -175,9 +195,18 @@ def draw(self, **kwargs):
)
if self.proj_dim == 3:
self.fig = plt.figure()
self.ax = self.fig.add_subplot(111, projection='3d')
self.ax.scatter(X[:, 0], X[:, 1], X[:, 2],
c=self.color, cmap=self.colormap)
if(self.heatmap):
self.ax = self.fig.add_subplot(211, projection='3d')
else:
self.ax = self.fig.add_subplot(111, projection='3d')

im = self.ax.scatter(X[:,0], X[:,1], X[:, 2], c=self.color, cmap=self.colormap, alpha=0.4, edgecolors='black',
vmin= self.pca_components_.min(), vmax = self.pca_components_.max())
if self.colorbar:
plt.colorbar(im, orientation='horizontal', ticks=[self.pca_components_.min(), 0,self.pca_components_.max()])
if self.heatmap:
self.ax1 = self.fig.add_subplot(2,1,2)
self.ax1.imshow(self.pca_components_, interpolation = 'none', cmap = self.colormap)
if self.proj_features:
x_vector = self.pca_components_[0]
y_vector = self.pca_components_[1]
Expand All @@ -202,11 +231,22 @@ def draw(self, **kwargs):

def finalize(self, **kwargs):
# Set the title
orig_X = self.orig_X
self.ax.set_title('Principal Component Plot')
self.ax.set_xlabel('Principal Component 1')
self.ax.set_ylabel('Principal Component 2')
self.ax.set_xlabel('\nPrincipal Component 1',linespacing=1.2)
self.ax.set_ylabel('\nPrincipal Component 2',linespacing=1.2)
if self.heatmap == True:
feature_names = list(orig_X.columns)
plt.gca().set_xticks(np.arange(-.5, len(feature_names)))
plt.gca().set_xticklabels(feature_names, rotation=90, ha='left', fontsize=12)
if self.proj_dim == 2:
plt.gca().set_yticks(np.arange(0.5, 2))
plt.gca().set_yticklabels(['First PC', 'Second PC'], va='bottom', fontsize=12)
if self.proj_dim == 3:
plt.gca().set_yticks(np.arange(0.5, 3))
plt.gca().set_yticklabels(['First PC', 'Second PC', 'Third PC'], va='bottom', fontsize=12)
if self.proj_dim == 3:
self.ax.set_zlabel('Principal Component 3')
self.ax.set_zlabel('Principal Component 3',linespacing=1.2)


##########################################################################
Expand Down