Machine Learning Tutorial

Matrix Factorization for Recommender Systems

Matrix Factorization for Recommender Systems

Matrix Factorization for Recommender Systems

Recommender systems are now a major part of the digital platforms we use every day. Whether we are watching movies on Netflix, shopping on Amazon, or discovering new music on Spotify, recommendation engines help us find content and products that match our interests.

One of the important techniques behind many of these systems is Matrix Factorization (MF). It is widely used in recommender systems, particularly in collaborative filtering, to learn hidden patterns from user-item interactions.

Matrix factorization works by identifying hidden characteristics, known as latent features, from large and usually sparse datasets. Instead of requiring detailed information about every product, movie, or song, it can learn preferences directly from user behavior.

Matrix Factorization for Recommender Systems

Complete Advance AI Topics: Click Here
SQL Tutorial:
Click Here

Understanding Matrix Factorization

Let’s understand how matrix factorization works in a recommender system step by step.

1. Representing the Data

The process starts with a user-item interaction matrix, usually represented by R.

  • Rows represent users.
  • Columns represent items such as movies, books, products, or songs.
  • Cell values represent interactions such as ratings, purchases, views, or clicks.

The main challenge is that users generally interact with only a small percentage of the available items. As a result, the user-item matrix is usually sparse, with many missing values.

2. Factorizing the Matrix

The objective of matrix factorization is to represent the original matrix using two smaller matrices:

  • U (User Matrix): Contains latent feature vectors representing users.
  • V (Item Matrix): Contains latent feature vectors representing items.

These vectors can capture hidden characteristics, such as a user’s preference for a particular movie genre or an item’s relationship with certain types of users.

3. Discovering Latent Features

The model learns the latent features by trying to reconstruct the original interaction matrix as accurately as possible.

Optimization techniques such as Stochastic Gradient Descent (SGD) can be used to reduce the difference between actual interactions and predicted interactions.

4. Predicting Missing Values

After learning the user and item embeddings, the model can combine the corresponding vectors to estimate unknown interactions.

For example, if a user has never rated a particular movie, the model can estimate how much that user may like the movie based on the learned latent features.

5. Generating Recommendations

Once predictions have been generated, the system can rank items according to their predicted ratings or interaction scores.

The items with the highest predicted scores can then be recommended to the user, making the recommendations personalized according to learned user preferences.

Implementing Matrix Factorization Using TensorFlow and Keras

We can implement a simple matrix factorization model using TensorFlow and Keras embeddings. The following example uses movie-rating data to demonstrate the basic approach.

Step 1: Import Required Libraries

import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
import tensorflow as tf
from tensorflow import keras
import os
import random

tf.set_random_seed(1)
np.random.seed(1)
random.seed(1)

Step 2: Load and Prepare the Dataset

Next, we load the ratings and movie information from the dataset.

input_dir = '../input/movielens-preprocessing'

ratings_path = os.path.join(input_dir, 'rating.csv')

ratings_df = pd.read_csv(
    ratings_path,
    usecols=['userId', 'movieId', 'rating', 'y']
)

df = ratings_df

movies_df = pd.read_csv(
    os.path.join(input_dir, 'movie.csv'),
    usecols=['movieId', 'title']
)

The ratings dataset contains user IDs, movie IDs, ratings, and the target value used for training. The movie dataset provides movie titles associated with their IDs.

Step 3: Build the Matrix Factorization Model

We can use embedding layers to represent users and movies as compact latent vectors.

movie_embedding_size = user_embedding_size = 8

user_id_input = keras.Input(
    shape=(1,),
    name='user_id'
)

movie_id_input = keras.Input(
    shape=(1,),
    name='movie_id'
)

user_embedded = keras.layers.Embedding(
    df.userId.max() + 1,
    user_embedding_size,
    input_length=1
)(user_id_input)

movie_embedded = keras.layers.Embedding(
    df.movieId.max() + 1,
    movie_embedding_size,
    input_length=1
)(movie_id_input)

dotted = keras.layers.Dot(2)(

[user_embedded, movie_embedded]

) out = keras.layers.Flatten()(dotted) model = keras.Model( inputs=[user_id_input, movie_id_input], outputs=out ) model.compile( tf.train.AdamOptimizer(0.001), loss=’MSE’, metrics=[‘MAE’] ) model.summary(line_length=88)

Here, both users and movies are represented using embeddings of size 8. The dot product between the user and movie embeddings produces a predicted interaction score.

Step 4: Train the Model

We can now train the model using the user IDs, movie IDs, and their corresponding target values.

history = model.fit(
    [df.userId, df.movieId],
    df.y,
    batch_size=5000,
    epochs=20,
    verbose=0,
    validation_split=0.05,
)

The model is trained for 20 epochs with a batch size of 5000. Five percent of the available data is used for validation during training.

Step 5: Visualize the Results

Finally, we can compare the training and validation performance using Mean Absolute Error (MAE).

history_dir = '../input/embedding-layers'

path = os.path.join(history_dir, 'history-1.csv')

hdf = pd.read_csv(path)

fig, ax = plt.subplots(figsize=(15, 8))

c1 = 'blue'

ax.plot(
    history.epoch,
    history.history['val_mean_absolute_error'],
    '--',
    label='Validation MAE',
    color=c1
)

ax.plot(
    history.epoch,
    history.history['mean_absolute_error'],
    label='Training MAE',
    color=c1
)

c2 = 'orange'

ax.plot(
    hdf.epoch,
    hdf.val_mae,
    '--',
    label='Validation MAE (DNN)',
    color=c2
)

ax.plot(
    hdf.epoch,
    hdf.train_mae,
    label='Training MAE (DNN)',
    color=c2
)

ax.set_xlabel('Epoch')
ax.set_ylabel('Mean Absolute Error')
ax.set_xlim(left=0)

baseline_mae = 0.73

ax.axhline(
    baseline_mae,
    ls='-.',
    label='Baseline',
    color='#002255',
    alpha=.5
)

ax.grid()
fig.legend()

This visualization helps compare the training and validation errors of the matrix factorization model with the reference DNN results and baseline performance.

YT:- DecodeIT

Conclusion: Why Matrix Factorization Matters

Matrix factorization remains an important technique for building recommender systems because it can learn useful patterns from sparse user-item interaction data.

As demonstrated in this example, even relatively small embeddings can learn meaningful relationships between users and items. The learned latent features can then be used to predict missing interactions and generate personalized recommendations.

Like other machine learning approaches, matrix factorization can suffer from overfitting. Techniques such as regularization and hybrid recommendation approaches can help address this issue.

Overall, matrix factorization provides an effective way to connect users with items they may be interested in by learning hidden patterns from their previous interactions.

Stay tuned for more hands-on tutorials and practical guides covering AI, machine learning, and data science.

Keywords: recommender systems, matrix factorization example, matrix factorization in machine learning, matrix factorization Python, matrix factorization algorithms, matrix factorization linear algebra, techniques, recommender systems Python, collaborative filtering, TensorFlow, matrix factorization Keras, for recommender systems

Source Code Available

Interested in This Project?

Get the complete source code for this project at a very affordable price — perfect for your portfolio, college submission, or learning. Message us on WhatsApp and we'll get back to you instantly!

Full source code included Step-by-step setup guide Instant delivery on WhatsApp Instant reply on WhatsApp
Chat on WhatsApp

We usually reply within a few minutes

Leave a Reply

Your email address will not be published. Required fields are marked *

Chat with us