ANALYSIS TOOL BOX

Descriptive Analytics

ConductPrincipalComponentAnalysis

Perform Principal Component Analysis (PCA) and return enriched DataFrame with components.

clusteringdescriptiveanalyticssegmentation

Introduction

A dataset with many correlated numeric columns hides its real structure — how much of the variation is actually independent information, and how many "effective" dimensions does the data have? ConductPrincipalComponentAnalysis answers that by transforming correlated variables into a smaller set of uncorrelated principal components, ordered by how much variance each one explains. It automatically scales features and, when you don't specify a component count, selects the minimum number needed to retain 90% of the total variance — giving you a compressed, decorrelated view of the data that's ready for visualization or downstream modeling.

Teaching Note

Principal Component Analysis is essential for:

  • Dimensionality reduction while preserving data variance
  • Feature engineering and noise reduction in machine learning
  • Multicollinearity elimination in regression analysis
  • Data compression and storage optimization
  • Exploratory data analysis and pattern recognition
  • Visualization of high-dimensional data
  • Computational efficiency improvement for downstream algorithms
  • Identifying the most important directions of variation in data

Parameters

ParameterTypeDefaultDescription
dataframerequiredA pandas DataFrame containing the data to transform. Must include numeric columns for the PCA algorithm to process. Rows with missing values are automatically removed.
list_of_numeric_columnsNoneList of column names to include in the PCA. If None, all numeric columns in the DataFrame will be automatically selected. Defaults to None.
number_of_componentsNoneNumber of principal components to retain. If None, the function automatically determines the optimal number by identifying components that cumulatively explain ≥90% of variance. Defaults to None.
random_seed412Random seed for reproducibility of the PCA algorithm. Use the same seed to ensure consistent results across multiple runs. Defaults to 412.
display_pca_as_markdownTrueWhether to display the principal component loadings table in the output. The table shows how each original feature contributes to each principal component. Defaults to True.

Returns

A pandas DataFrame — the original data with additional columns for each principal component, named PC1, PC2, ..., PCn. Rows with missing values in the selected numeric columns are removed.

Example

python
from analysistoolbox.descriptive_analytics import ConductPrincipalComponentAnalysis
import pandas as pd

# Automatic component selection with variance threshold
marketing_df = pd.DataFrame({
    'ad_spend': [1000, 2000, 1500, 3000, 2500],
    'impressions': [10000, 20000, 15000, 30000, 25000],
    'clicks': [100, 200, 150, 300, 250],
    'conversions': [10, 20, 15, 30, 25]
})
result_df = ConductPrincipalComponentAnalysis(
    marketing_df,
    display_pca_as_markdown=True
)
# Automatically selects components explaining >=90% variance
# Displays scree plot and component loadings