random projection
algorithm
pseudo code
dimensionality reduction
data science

Random projection algorithm pseudo code

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

Random projection is a dimensionality-reduction technique that maps high-dimensional data into a smaller space using a randomly generated matrix. The main appeal is speed: you often get a useful lower-dimensional representation without solving an expensive optimization problem such as PCA.

Core Idea

Suppose your data matrix has m rows and d features, and you want a new representation with only k features where k is much smaller than d. Random projection builds a d x k projection matrix with random values, then multiplies the original data by that matrix.

The intuition is that a carefully scaled random mapping can approximately preserve distances between points, which is why the technique is often explained alongside the Johnson-Lindenstrauss idea.

Pseudo Code

Here is a clean pseudo-code version of the algorithm:

text
1input:
2  X                # m x d data matrix
3  k                # target dimension
4
5algorithm:
6  create random matrix R of size d x k
7  scale R so projected distances stay reasonable
8  compute Y = X * R
9  return Y

A slightly more explicit version looks like this:

text
1function RANDOM_PROJECTION(X, k):
2    d = number_of_columns(X)
3    R = random_gaussian_matrix(d, k)
4    R = R / sqrt(k)
5    Y = matrix_multiply(X, R)
6    return Y

That is the essence of the method. Different variants change how R is sampled, but the overall structure stays the same.

A Small NumPy Example

The algorithm is easy to implement directly:

python
1import numpy as np
2
3rng = np.random.default_rng(0)
4
5X = rng.normal(size=(4, 6))
6k = 3
7
8R = rng.normal(size=(6, k)) / np.sqrt(k)
9Y = X @ R
10
11print("original shape:", X.shape)
12print("projected shape:", Y.shape)
13print(Y)

This example starts with four samples and six features, then projects them down to three features. The output is a smaller matrix that is often good enough for downstream tasks such as approximate similarity search, clustering, or preprocessing.

How to Choose the Random Matrix

The most common teaching version uses a Gaussian matrix, where each entry is sampled from a normal distribution. Sparse random matrices are another option when you care about speed and memory usage.

In practice, the algorithm has three moving parts:

  • The target dimension k
  • The random distribution used for the projection matrix
  • The scaling rule applied to the matrix

Those choices affect distortion, runtime, and memory cost, but they do not change the fundamental algorithm.

When Random Projection Is Useful

Random projection is especially attractive when:

  • The original feature space is very large
  • Exact geometry is less important than speed
  • You need a fast preprocessing step before another algorithm

It is often a strong baseline because it is simple to implement and cheap to compute.

Common Pitfalls

  • Choosing k too small can destroy too much structure in the data.
  • Forgetting to scale the random matrix changes the magnitude of the projected data in unhelpful ways.
  • Random projection is fast, but it is still stochastic, so use a fixed random seed when you need reproducible results.
  • It is a projection method, not a learned representation, so do not expect the same interpretability as PCA components.

Summary

  • Random projection reduces dimensionality by multiplying the data by a random matrix.
  • The algorithm is simple: generate a random projection matrix, scale it, and multiply.
  • A Gaussian random matrix is the usual teaching example, but sparse variants also exist.
  • The method is useful when you want a fast, practical reduction in feature count without heavy optimization.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.