# Create scree plot
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(6, 6))
# Individual variance explained
variance_explained = pca_full.explained_variance_ratio_
ax1.plot(range(1, len(variance_explained) + 1),
variance_explained, 'bo-', linewidth=2, markersize=8)
ax1.set_xlabel('Principal Component', fontweight='bold', fontsize=11)
ax1.set_ylabel('Variance Explained', fontweight='bold', fontsize=11)
ax1.set_title('Scree Plot: Variance per Component', fontweight='bold', fontsize=12)
ax1.grid(True, alpha=0.3)
ax1.axvline(x=7, color='red', linestyle='--', alpha=0.7, label='Elbow around PC7')
ax1.legend()
# Cumulative variance explained
cumsum = np.cumsum(variance_explained)
ax2.plot(range(1, len(cumsum) + 1), cumsum, 'ro-', linewidth=2, markersize=8)
ax2.axhline(y=0.95, color='green', linestyle='--', linewidth=2, label='95% threshold')
ax2.axhline(y=0.90, color='orange', linestyle='--', linewidth=2, label='90% threshold')
ax2.set_xlabel('Number of Components', fontweight='bold', fontsize=11)
ax2.set_ylabel('Cumulative Variance', fontweight='bold', fontsize=11)
ax2.set_title('Cumulative Variance Explained', fontweight='bold', fontsize=12)
ax2.legend()
ax2.grid(True, alpha=0.3)
# Find components needed for 95%
n_95 = np.argmax(cumsum >= 0.95) + 1
ax2.axvline(x=n_95, color='green', linestyle=':', alpha=0.7)
ax2.text(n_95 + 1, 0.5, f'{n_95} components\nfor 95% variance',
fontsize=10, bbox=dict(boxstyle='round', facecolor='lightgreen', alpha=0.7))
plt.tight_layout()
plt.show()