AI summary by TwinMind

StatQuest: PCA in Python

StatQuest with Josh Starmer

11m 37s

Introduction and Setup

  • Josh Stormer presenting StatQuest tutorial on PCA in Python
  • Acknowledgment to Chris Yoga Zimsky for code contribution
  • Tutorial covers: data generation, PCA function usage, variation analysis, matplotlib graphing, loading scores examination

Package Imports

  • pandas: data manipulation (panel data)
  • numpy: random number generation, mathematical operations
  • random: example dataset generation
  • sklearn.decomposition.PCA: most commonly used Python PCA function
  • sklearn.preprocessing: data scaling functions before PCA
  • matplotlib: graph visualization
  • Note: Python requires importing packages unlike R's built-in support

Sample Dataset Generation

  • Created 100 gene names array (gene1, gene2, etc.)
  • Sample arrays: 5 wild-type (WT) samples, 5 knockout (KO) samples
  • range(1,6) generates values 1-5 (up to but not including 6)
  • Pandas DataFrame creation with unpacked arrays (*WT, *KO) for column names
  • Gene names used as index (row names equivalent)
  • Random data from two Poisson distributions (one per sample type)
  • Poisson means vary between 10-1000 per gene
  • Final dimensions: 100 genes × 10 samples
  • Note: Poisson distribution not ideal for RNA-seq data but sufficient for example

Data Preprocessing

  • Centering: average value per gene = 0
  • Scaling: standard deviation per gene = 1
  • Data transposed before scaling (scale function expects samples in rows)
  • Alternative method: StandardScaler.fit_transform (more common for machine learning)
  • Scaling difference between scikit-learn vs R:
    • Scikit-learn: (measurements - mean)² / n
    • R (scale/prcomp): (measurements - mean)² / (n-1)
    • Differences don't affect PCA analysis but minor effect on final graph coordinates

PCA Implementation

  • Created PCA object (scikit-learn uses objects vs functions)
  • Object can be trained on one dataset, applied to another
  • fit() method performs PCA math: calculates loading scores, variation per component
  • transform() generates coordinates for PCA graph based on loading scores and scaled data

Scree Plot Analysis

  • Calculated percentage variation per principal component
  • Created labels (PC1, PC2, etc.)
  • Matplotlib bar plot creation
  • Results: almost all variation along first principal component
  • Conclusion: 2D graph using PC1 & PC2 adequate for representing original data

PCA Plot Visualization

  • Coordinates from PCA.transform() placed in matrix
  • Rows: sample labels, Columns: PC labels
  • Scatter plot with title and axis labels
  • Loop added sample names to graph
  • Results:
    • WT samples clustered left side (correlated with each other)
    • KO samples clustered right side (correlated with each other)
    • Separation along x-axis indicates WT vs KO differences

Loading Scores Analysis

  • Examined PC1 loading scores to identify genes influencing cluster separation
  • Created pandas Series object with loading scores
  • Principal components zero-indexed (PC1 = 0)
  • Sorted by magnitude (absolute value)
  • Top 10 gene names and corresponding loading scores
  • Results: values super similar, many genes contributed to sample separation (not just 1-2)

TwinMind

Get summaries like this for your own meetings

TwinMind takes notes in the background during meetings, lectures, and calls, then writes the summary for you.

Free plan to get started

No bot joins your calls

Phone, desktop & Chrome apps

Your next meeting, summarized like this

TwinMind listens in the background and turns meetings, lectures, and calls into searchable notes you can chat with. Start free.

Available on phone, desktop & Chrome

Your next meeting, summarized like this

TwinMind listens in the background and turns meetings, lectures, and calls into searchable notes you can chat with. Start free.

Available on phone, desktop & Chrome

Your next meeting, summarized like this

TwinMind listens in the background and turns meetings, lectures, and calls into searchable notes you can chat with. Start free.

Available on phone, desktop & Chrome