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.
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:
A slightly more explicit version looks like this:
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:
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
ktoo 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
- Random row selection in Pandas dataframe
- random sample of two 100X100 multidimensional arrays, with same row no. in python numpy
- Randomness in Artificial Intelligence Machine Learning
- Rank items in an array using Python/NumPy, without sorting array twice
- Random Shuffling in Java or any language Probabilities
- Random shuffling of an array
- Ranking algorithms
- Re-train model with new classes

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 courseTrack 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.