Skip to content

Commit 56d8926

Browse files
committed
Jared1
1 parent a5acd67 commit 56d8926

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

test/test_pcvr.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from toolkit.plot import plot_cumulative_variance_ratio
2+
3+
import pytest
4+
import numpy as np
5+
import matplotlib.pyplot as plt
6+
7+
8+
pca_test = type('pca_test', (), {'explained_varianceratio': np.array([0.5, 0.3, 0.2])})
9+
n = 3
10+
11+
def test_plot_cumulative_variance_ratio():
12+
13+
plot_cumulative_variance_ratio(pca_test, n)

toolkit/plot.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,28 @@ def wordcloudviz(column):
169169
plt.tight_layout(pad=0)
170170
plt.show()
171171
>>>>>>> 0f8d8abe7a26aac02e768b21b22a1d2e58bd6d30
172+
173+
def plot_cumulative_variance_ratio(pca, n_features):
174+
175+
'''
176+
Function to visually represent the percentage of variance explained by each PCA component
177+
178+
Parameters =
179+
180+
pca: Name of the variable assigned to the PCA
181+
n_features: Number of PCA components
182+
183+
Returns:
184+
Matplotlib lineplot of the variance explained by each PCA component
185+
186+
187+
'''
188+
cumulative_variance_ratio = np.cumsum(pca.explained_varianceratio)[:n_features]
189+
190+
# Create a bar plot of the cumulative variance ratio
191+
plt.plot(range(1, n_features + 1), cumulative_variance_ratio)
192+
plt.xlabel('Number of Principal Components')
193+
plt.ylabel('Cumulative Variance Ratio')
194+
195+
# Show the plot
196+
plt.show()

0 commit comments

Comments
 (0)