Skip to main content

Similarity Matrix in Machine Learning Explained


The technique used here is generally called Similarity-Based Recommendation or Nearest-Neighbor Recommendation. The matrix itself is called a Similarity Matrix. The fact that it is stored as a Compressed Sparse Matrix is an implementation detail used to save memory and improve performance.


1. Similarity Matrix (Core ML Concept)

A similarity matrix stores how similar every pair of movies is.

Avatar Titanic Alien
Avatar 1.00 0.42 0.87
Titanic 0.42 1.00 0.21
Alien 0.87 0.21 1.00

Each value represents the similarity between two movies.

  • 1.0 = Identical movie (itself)
  • 0.9 = Very similar
  • 0.5 = Moderately similar
  • 0.1 = Barely similar
  • 0.0 = Not similar

Similarity matrices are commonly used in:

  • Content-Based Filtering
  • Item-Based Collaborative Filtering
  • k-Nearest Neighbors (k-NN) Recommendation Systems

2. Compressed Sparse Matrix (Storage Technique)

A Sparse Matrix is a matrix where most values are zero. Instead of storing every value, only the non-zero values and their positions are stored.

Normal Matrix

1.0  0.0  0.0  0.8
0.0  1.0  0.0  0.0
0.0  0.0  1.0  0.0
0.8  0.0  0.0  1.0

Sparse Representation

(Row, Column, Value)

(0,0,1.0)
(0,3,0.8)
(1,1,1.0)
(2,2,1.0)
(3,0,0.8)
(3,3,1.0)

This representation dramatically reduces memory usage when most matrix entries are zero.

Common sparse formats include:

  • CSR (Compressed Sparse Row) 
  • CSC (Compressed Sparse Column)
  • COO (Coordinate Format)

3. How is the Similarity Matrix Created?

The similarity matrix is usually computed using one of the following similarity metrics:

  • Cosine Similarity 
  • Pearson Correlation
  • Jaccard Similarity
  • Euclidean Distance (converted into similarity)

For movie recommendation systems, Cosine Similarity is the most popular choice.


Overall Pipeline

Movie Features
      │
      ▼
Vector Representation
(TF-IDF, Embeddings, Ratings, etc.)
      │
      ▼
Cosine Similarity
      │
      ▼
Similarity Matrix
      │
      ▼
Store as Compressed Sparse Matrix (CSR)
      │
      ▼
Load Matrix
      │
      ▼
Find Most Similar Movies

Python Code (using Compressed Sparse Row (CSR))


import pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
from scipy.sparse import csr_matrix, save_npz, load_npz

# --------------------------------------------------
# Sample Movie Dataset
# (Replace this with pd.read_csv("movies.csv") later)
# --------------------------------------------------

movies = pd.DataFrame({
    "title": [
        "Avatar",
        "Titanic",
        "Alien",
        "Avengers",
        "The Notebook"
    ],

    "genre": [
        "Action Adventure Sci-Fi",
        "Romance Drama",
        "Sci-Fi Horror",
        "Action Superhero Sci-Fi",
        "Romance Drama"
    ],

    "overview": [
        "A marine explores Pandora and fights for the Na'vi.",
        "A tragic love story aboard a sinking ship.",
        "A deadly alien hunts a spaceship crew.",
        "Earth's heroes unite to save humanity.",
        "A romantic love story across many years."
    ]
})

# --------------------------------------------------
# Combine Features
# --------------------------------------------------

movies["features"] = (
    movies["genre"] + " " +
    movies["overview"]
)

# --------------------------------------------------
# Convert text into TF-IDF vectors
# --------------------------------------------------

vectorizer = TfidfVectorizer(stop_words="english")

feature_matrix = vectorizer.fit_transform(movies["features"])

# --------------------------------------------------
# Compute Cosine Similarity
# --------------------------------------------------

similarity = cosine_similarity(feature_matrix)

# --------------------------------------------------
# Convert to CSR Matrix
# --------------------------------------------------

csr_similarity = csr_matrix(similarity)

# --------------------------------------------------
# Save CSR Matrix
# --------------------------------------------------

save_npz("similarity_matrix.npz", csr_similarity)

print("CSR similarity matrix saved.")

# --------------------------------------------------
# Load CSR Matrix
# --------------------------------------------------

loaded_similarity = load_npz("similarity_matrix.npz")

print("CSR matrix loaded.")

# --------------------------------------------------
# Recommendation Function
# --------------------------------------------------

def recommend(movie_title, top_n=5):

    if movie_title not in movies["title"].values:
        print("Movie not found.")
        return

    index = movies[movies["title"] == movie_title].index[0]

    scores = loaded_similarity[index].toarray().flatten()

    similar_indices = scores.argsort()[::-1]

    print(f"\nRecommendations for '{movie_title}':\n")

    count = 0

    for i in similar_indices:

        if i == index:
            continue

        print(
            f"{movies.iloc[i]['title']}"
            f"   Similarity = {scores[i]:.3f}"
        )

        count += 1

        if count == top_n:
            break


# --------------------------------------------------
# Example
# --------------------------------------------------

recommend(input("Enter a movie name: "))
  

Output

Enter a movie name: Avatar Recommendations for 'Avatar': Avengers Similarity = 0.193 Alien Similarity = 0.117 The Notebook Similarity = 0.000 Titanic Similarity = 0.000


Cosine Similarity in Machine Learning

Cosine Similarity is a mathematical technique used to measure how similar two vectors are by calculating the cosine of the angle between them. Instead of comparing their lengths, it compares their direction.


1. Mathematical Formula

Cosine Similarity = (A · B) / (||A|| × ||B||)
Where:
  • A · B = Dot Product of the vectors
  • ||A|| = Magnitude (Length) of vector A
  • ||B|| = Magnitude (Length) of vector B

The result always lies between -1 and 1.

Cosine Similarity Meaning
1 Exactly the same direction (Highly Similar)
0 No similarity (Perpendicular)
-1 Completely opposite direction

2. Example Using Movies

Suppose we represent movies using features:

Feature Avatar Alien
Action 1 1
Sci-Fi 1 1
Romance 0 0
Horror 0 1

Vector A (Avatar)

A = [1, 1, 0, 0]

Vector B (Alien)

B = [1, 1, 0, 1]

3. Step 1 - Dot Product

Multiply corresponding elements and add them.

A · B

= (1×1) + (1×1) + (0×0) + (0×1)

= 1 + 1 + 0 + 0

= 2

4. Step 2 - Magnitude of Each Vector

Magnitude of Avatar vector

||A||

= √(1² + 1² + 0² + 0²)

= √2

Magnitude of Alien vector

||B||

= √(1² + 1² + 0² + 1²)

= √3

5. Step 3 - Calculate Cosine Similarity

Cosine Similarity

= 2 / (√2 × √3)

= 2 / √6

≈ 0.816

Therefore, Avatar and Alien have a cosine similarity of 0.816, indicating that they are quite similar.


6. Why Is It Called Cosine Similarity?

Imagine every movie as an arrow (vector) starting from the origin.

          B
         /
        /
       / θ
------/----------> A

The angle between the vectors is θ.

Cosine Similarity = cos(θ)

Angle Cosine Meaning
1 Exactly the same direction
90° 0 No similarity
180° -1 Completely opposite

7. Cosine Similarity in a Movie Recommendation System

Each movie is converted into a feature vector.

Movie Feature Vector
Avatar [1, 1, 1, 0, 0]
Alien [1, 0, 1, 1, 0]
Titanic [0, 0, 0, 0, 1]

The cosine similarity between every pair of movies is computed.

Avatar Alien Titanic
Avatar 1.00 0.82 0.11
Alien 0.82 1.00 0.05
Titanic 0.11 0.05 1.00

When a user searches for Avatar, the recommender simply selects the movies having the highest cosine similarity score, such as Alien.

Summary

Cosine Similarity measures the angle between two feature vectors instead of their lengths. It is widely used in recommendation systems because movies with similar genres, keywords, or descriptions point in nearly the same direction in vector space. After calculating pairwise cosine similarities, the results are stored in a similarity matrix, often compressed using CSR (Compressed Sparse Row) format for efficient storage and fast retrieval.



Contact Us

Name

Email *

Message *

Popular Posts

BER vs SNR for M-ary QAM, M-ary PSK, QPSK, BPSK, ...(MATLAB Code + Simulator)

Bit Error Rate (BER) & SNR Guide Analyze communication system performance with our interactive simulators and MATLAB tools. 📘 Theory 🧮 Simulators 💻 MATLAB Code 📚 Resources BER Definition SNR Formula BER Calculator MATLAB Comparison 📂 Explore M-ary QAM, PSK, and QPSK Topics ▼ 🧮 Constellation Simulator: M-ary QAM 🧮 Constellation Simulator: M-ary PSK 🧮 BER calculation for ASK, FSK, and PSK 🧮 Approaches to BER vs SNR Calculation What is Bit Error Rate (BER)? The BER indicates how many corrupted bits are received compared to the total number of bits sent. It is the primary figur...

MIMO Channel Matrix | Rank and Condition Number

MIMO / Massive MIMO MIMO Channel Matrix | Rank and Condition...   The channel matrix in wireless communication is a matrix that describes the impact of the channel on the transmitted signal. The channel matrix can be used to model the effects of the atmospheric or underwater environment on the signal, such as the absorption, reflection or scattering of the signal by surrounding objects. When addressing multi-antenna communication, the term "channel matrix" is used. Let's assume that only one TX and one RX are in communication and there's no surrounding object. Here, in our case, we can apply the proper threshold condition to a received signal and get the original transmitted signal at the RX side. However, in real-world situations, we see signal path blockage, reflections, etc.,  (NLOS paths [↗]) more frequently. The obstruction is typically caused by building walls, etc. Multi-antenna communication was introduced to address this issue. It makes diversity app...

UGC NET Electronic Science Previous Year Question Papers with Solutions

Home / Engineering & Other Exams / UGC NET 2026 PYQ ⬇️ Download Papers and Solutions 📋 Exam Pattern 💡 Preparation Tips ❓ FAQs 📊 Exam Highlights: Electronic Science (88) Feature Details Junior Research Fellowship (JRF) ₹37,000 + HRA per month Eligibility M.Sc/M.Tech in Electronics (55%) Validity of Certificate JRF (3 Years) | Lectureship (Lifetime) 📥 Download UGC NET Electronics PDFs Complete collection of previous year question papers, answer keys and explanations for Subject Code 88. Start Downloading 📂 View All Question Papers June 2025 - Question Paper Download PDF June 2025 - Solved Paper + Explanation ...

MATLAB Code for OTFS (Orthogonal Time Frequency Space)

MATLAB Code for OTFS (Orthogonal Time Frequency Space) %% Clear workspace clc; clear; close all ; %% Step 1: OTFS Parameters N_delay = 4; % Number of delay bins (rows) N_doppler = 4; % Number of Doppler bins (columns) N_sym = N_delay * N_doppler; modOrder = 4; % QPSK SNR_dB = 20; % Noise level %% Step 2: Generate random data symbols data = randi([0 modOrder-1], N_sym, 1); txSymbols = pskmod(data, modOrder, pi/4); disp( 'Transmitted Delay-Doppler symbols:' ); disp(reshape(txSymbols, N_delay, N_doppler)); %% Step 3: Map Delay-Doppler → Time-Frequency (ISFFT) % ISFFT: Inverse Symplectic Finite Fourier Transform % 1. Take IDFT along Doppler (columns) % 2. Take DFT along Delay (rows) ddSymbols = reshape(txSymbols, N_delay, N_doppler); % Step 3a: IDFT along columns (Doppler) tfGrid = ifft(ddSymbols, N_doppler, 2); %IFFT (accross columns) along Doppler → spreads in time (Delay → Time) %FFT (accross rows)along Delay → spreads in frequency (Delay → Frequency) % Step 3b: DFT along ...

Online Simulator for ASK, FSK, and PSK Signal Generation

Interactive Digital Signal Processing (DSP) Tutorial and Simulator for ASK, FSK, and BPSK modulation techniques. Try our new Digital Signal Processing Simulator!   •   Interactive ASK, FSK, and BPSK tools updated for 2025. Start Now Digital Modulation Visualizer: ASK, FSK, & BPSK Simulator Learn and visualize binary modulation techniques (ASK, FSK, BPSK) in real-time with adjustable carrier and sampling parameters. Perfect for DSP students and engineers. 📡 ASK Simulator 📶 FSK Simulator 🎚️ BPSK Simulator 📚 More Topics ASK Modulator FSK Modulator BPSK Modulator More Topics 1. ASK (Amplitude Shift Keying) Simulat...

How to Mount Google Drive in Google Colab

How to Mount Google Drive in Google Colab Google Colab provides temporary storage during a session. Any files stored in the /content directory will be deleted when the runtime disconnects. To store datasets, trained models, and results permanently, it is recommended to mount your Google Drive in Colab. Mounting Google Drive allows your notebook to access files directly from your Drive and save outputs there so they remain available even after the Colab session ends. Step 1: Import the Drive Module First import the Google Colab drive module. from google.colab import drive Step 2: Mount Google Drive Run the following command to mount your Google Drive. from google.colab import drive drive.mount('/content/drive') After running the command: A link will appear in the output. Click the link and log in to your Google account. Copy the authentication code provided. Paste the code back into the notebook. Or, a Google authentication page will a...

OFDM Symbols and Subcarriers Explained

This article explains how OFDM (Orthogonal Frequency Division Multiplexing) symbols and subcarriers work. It covers modulation, mapping symbols to subcarriers, subcarrier frequency spacing, IFFT synthesis, cyclic prefix, and transmission. Step 1: Modulation First, modulate the input bitstream. For example, with 16-QAM , each group of 4 bits maps to one QAM symbol. Suppose we generate a sequence of QAM symbols: s0, s1, s2, s3, s4, s5, …, s63 Step 2: Mapping Symbols to Subcarriers Assume N sub = 8 subcarriers. Each OFDM symbol in the frequency domain contains 8 QAM symbols (one per subcarrier): Mapping (example) OFDM symbol 1 → s0, s1, s2, s3, s4, s5, s6, s7 OFDM symbol 2 → s8, s9, s10, s11, s12, s13, s14, s15 … OFDM sym...

Online Pulse Code Modulation (PCM) Simulator

Instructions for Pulse Code Modulation (PCM) Note: Use the input fields to enter values. Step 1: Generate Message Step 2: Plot Quantized Signal Step 3: Generate PCM Signal Message Frequency (Hz): Sampling Frequency (Hz): Quantization Levels: Generate Message Quantized PCM Quantization SNR (dB): Demodulation Cut-off Frequency Demodulate PCM Quantization Table Calculate Bits Levels Step Min Mid Max SNR Print Online Signal Processing Simulations Home Page >