Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Random Projection: Theory and Implementation

Random Projection: Theory and Implementation

Mahmood Amintoosi, Fall 2026 Computer Science Dept, Ferdowsi University of Mashhad

1. Introduction: Why Random Projection?

In the previous sections, we explored the weird properties of high-dimensional spaces. We learned that as dimensions increase, data becomes sparse, and distance-based methods (like k-NN) struggle. This is the Curse of Dimensionality.

To solve this, we often use Dimensionality Reduction. The most famous method is PCA (Principal Component Analysis), which finds the “best” axes to preserve variance. However, PCA has a major drawback:

  • It is computationally expensive: Calculating the covariance matrix and its eigenvectors takes a long time for very large datasets (O(d2n)O(d^2 n) or O(d3)O(d^3)).

Random Projection (RP) offers a surprising alternative: Instead of finding the “best” axes, we simply project the data onto random axes!

  • It is incredibly fast.

  • It is data-independent (you don’t need to see the data to build the projection matrix).

  • It preserves distances remarkably well.


2. Foundation: Orthogonality in High Dimensions

Recap from High-Dimensional Geometry:

Why does projecting onto random vectors work? The answer lies in the geometry of high-dimensional spaces. As we saw in The Curse of Dimensionality section, if we generate random vectors from a Gaussian distribution in high dimensions (d→∞d \to \infty), they have a unique property:

“Random vectors in high dimensions are nearly orthogonal to each other.”

If you pick kk random vectors r1,r2,…,rkr_1, r_2, \dots, r_k in a high-dimensional space, they effectively form a new, nearly orthogonal coordinate system. Projecting data onto these vectors is like taking a “snapshot” of the data from kk different random angles.

<Figure size 800x500 with 1 Axes>

Observation: The dot products cluster tightly around 0. This confirms that random directions are perpendicular (orthogonal) to each other.


3. The Theory: Johnson-Lindenstrauss Lemma

The core theoretical justification for Random Projection is the Johnson-Lindenstrauss (JL) Lemma.

It states, simply:

Points in a high-dimensional space can be mapped into a lower-dimensional space of dimension kk such that the pairwise distances between the points are nearly preserved.

Mathematically, for any two data points xx and yy:

(1−ϵ)∣∣x−y∣∣2≤∣∣f(x)−f(y)∣∣2≤(1+ϵ)∣∣x−y∣∣2(1 - \epsilon) ||x - y||^2 \le ||f(x) - f(y)||^2 \le (1 + \epsilon) ||x - y||^2

Where:

  • f(⋅)f(\cdot) is the projection function.

  • ϵ\epsilon is the tolerance for distortion (error).

Key Insight: The required lower dimension kk does not depend on the original dimension dd. It only depends on the number of samples nn and the error ϵ\epsilon:

k≥4ln⁡(n)ϵ2/2−ϵ3/3k \ge \frac{4 \ln(n)}{\epsilon^2 / 2 - \epsilon^3 / 3}

This means we can reduce a 1,000,000-dimensional text dataset to a few hundred dimensions just as easily as a 10,000-dimensional one, provided the number of samples nn is the same.

Note: Random Projection Length Scaling

This experiment shows why the scaling factor 1/k1/\sqrt{k} is necessary in random projection.
We take a unit vector in a high-dimensional space (dd) and project it into a lower-dimensional space (kk) using a random matrix.

  • Without scaling: the projected vector length concentrates around k\sqrt{k}.

  • With scaling (1/k1/\sqrt{k}): the projected vector length concentrates around 1, preserving the expected norm.

The histograms below compare both cases, confirming that scaling ensures distances and lengths remain consistent after projection.

Source
<Figure size 1200x500 with 2 Axes>

4. Types of Random Matrices

In practice, we project data XX (shape n×dn \times d) into a lower dimension kk using a random matrix RR (shape d×kd \times k):

Xnew=1kX⋅RX_{new} = \frac{1}{\sqrt{k}} X \cdot R

(The scaling factor 1/k1/\sqrt{k} is to maintain the expected length of vectors).

There are two main ways to generate RR:

A. Gaussian Random Projection

The elements of the matrix RR are drawn from a Normal distribution N(0,1)N(0, 1). This is the classic approach directly following the JL Lemma.

B. Sparse Random Projection (Achlioptas)

Generating a huge matrix of float numbers is slow. Dimitris Achlioptas showed that we can use a much simpler, sparse matrix. The elements rijr_{ij} are drawn from:

rij={+swith prob 12s0with prob 1−1s−swith prob 12sr_{ij} = \begin{cases} +\sqrt{s} & \text{with prob } \frac{1}{2s} \\ 0 & \text{with prob } 1 - \frac{1}{s} \\ -\sqrt{s} & \text{with prob } \frac{1}{2s} \end{cases}

Usually, s=3s=3. This means the matrix is mostly zeros (≈66%\approx 66\%), with some +1 and -1.

  • Benefit: Much faster matrix multiplication and less memory usage.


5. Implementation in Python with Scikit-Learn

Scikit-Learn provides GaussianRandomProjection and SparseRandomProjection.

Let’s verify the JL Lemma: we will reduce a dataset and check if distances are preserved.

To preserve pairwise distances of 500 samples with 10.0% max error,
we need at least 5326 dimensions.
Original shape: (500, 50000)
Projected shape: (500, 5326)
<Figure size 800x600 with 1 Axes>

If we project from 50000 dimensions down to lower than ‘min_dim’ ...

Original shape: (500, 50000)
Projected shape: (500, 300)
<Figure size 800x600 with 1 Axes>

Interpretation of the Plot

If the JL Lemma holds, the histogram of ratios should cluster tightly around 1.0.

  • Most pairs should have a ratio between 0.9 and 1.1 (for ϵ=0.1\epsilon=0.1).

  • This confirms that although we threw away thousands of dimensions, the relative geometry of the data points remains intact.

When to use Random Projection?

  • When the number of features is very large (e.g., text data, genomic data, images).

  • When PCA is too slow to compute.

  • As a preprocessing step before clustering (K-Means) or classification in high-dim space.

References

  1. Blum, A., Hopcroft, J., & Kannan, R. (2020). Foundations of Data Science. Cambridge University Press. (Chapter 2).

  2. Scikit-Learn Documentation: Random Projection.

  3. Achlioptas, D. (2003). Database-friendly random projections: Johnson-Lindenstrauss with binary coins.

  4. Saeed, Mehreen. Random Projection: Theory and Implementation in Python with Scikit-Learn