How to build a simple recommendation system in Azure AI & Machine Learning
Learn how to build a simple recommendation system with Azure AI & Machine Learning to suggest products/items to users. This workflow covers everything from data preparation to creating an endpoint to serve recommendations, useful for personalizing sales or content.
Prerequisites
- Azure account with permissions to create resources (Azure Machine Learning Workspace).
- Azure CLI and az ml extension installed, or access to Azure Machine Learning Studio.
- Python 3.8+ and pip; libraries: pandas, scikit-learn, implicit (or alternatively Surprise).
- Basic concepts of collaborative filtering and model training.
Step 1: Prepare and understand the data
Why: collaborative recommendation systems require a user-item matrix with ratings or interactions. We will use a simplified example dataset (user_id, item_id, rating). Cleaning and transforming to sparse formats increases efficiency.
# exemplo mínimo de preparação em Python
import pandas as pd
from scipy.sparse import coo_matrix
# dados de exemplo
rows = [1,1,2,2,3,3]
cols = [10,11,10,12,11,13]
ratings = [5,4,4,5,2,5]
df = pd.DataFrame({'user_id': rows, 'item_id': cols, 'rating': ratings})
# mapear ids para índices contínuos
user_map = {u:i for i,u in enumerate(df['user_id'].unique())}
item_map = {i:j for j,i in enumerate(df['item_id'].unique())}
users = df['user_id'].map(user_map)
items = df['item_id'].map(item_map)
sparse = coo_matrix((df['rating'], (users, items)))
Step 2: Train a collaborative model (ALS)
Why: Alternating Least Squares (ALS) works well for implicit/sparse data and is efficient for recommendations. We use the implicit library for a quick example. Tune factors and regularization according to your data.
from implicit.als import AlternatingLeastSquares
import numpy as np
# implicit trabalha com item-user matrix esparsa
item_user = sparse.tocsr().T.tocsr()
model = AlternatingLeastSquares(factors=50, regularization=0.01, iterations=20)
model.fit(item_user)
# exemplo: obter top 5 recomendações para user index 0
user_index = 0
recommendations = model.recommend(user_index, sparse.tocsr(), N=5)
print(recommendations) # tuplos (item_index, score)
Step 3: Validate the model locally
Why: evaluating accuracy with simple metrics avoids surprises in production. Use hold-out or cross-validation; at this stage compute Precision@K or Recall@K on test data.
def precision_at_k(model, test_sparse, train_sparse, user_index, K=5):
# items already seen should be ignored
recommended = [i for i,_ in model.recommend(user_index, train_sparse, N=K*2) if i not in train_sparse[user_index].indices]
recommended = recommended[:K]
relevant = set(test_sparse[user_index].indices)
return len([i for i in recommended if i in relevant]) / K
# calcular média para alguns utilizadores
# (exemplo simplificado; em produção usa validação adequada)
Step 4: Package the model for deployment in Azure Machine Learning
Why: to serve recommendations in the cloud, register the model and create an endpoint. Export essential weights/objects and create a scoring script that loads the model and receives JSON requests with user_id.
# salvar o modelo (pickle) e map de ids
import pickle
with open('als_model.pkl','wb') as f:
pickle.dump({'model': model, 'user_map': user_map, 'item_map': item_map}, f)
# scoring.py (esqueleto)
"""
Recebe JSON: {"user_id": 123, "k": 5}
Retorna: {"recommendations": [{"item_id": 456, "score": 0.9}, ...]}
"""
import pickle
model_bundle = None
def init():
global model_bundle
with open('als_model.pkl','rb') as f:
model_bundle = pickle.load(f)
def run(request):
payload = request.get_json()
uid = payload.get('user_id')
k = payload.get('k',5)
umap = model_bundle['user_map']
if uid not in umap:
return {'recommendations': []}
uidx = umap[uid]
recs = model_bundle['model'].recommend(uidx, sparse.tocsr(), N=k)
# converter indices para item_id original
inv_item_map = {v:k for k,v in model_bundle['item_map'].items()}
return {'recommendations': [{'item_id': inv_item_map[i], 'score': float(s)} for i,s in recs]}
Step 5: Create and deploy an Endpoint in Azure
Explain: use Azure Machine Learning to register the model and create a Container Image or Managed Online Endpoint. In Studio or with az ml, define an environment with Python and dependencies (implicit, scikit-learn).
# comandos de exemplo (linha) - conceptual
# az ml model register --name recommendation-model --path ./als_model.pkl
# az ml environment create --file environment.yml
# az ml online-endpoint create -n rec-endpoint -f endpoint.yml
# az ml online-deployment create -e rec-endpoint -n blue --model recommendation-model:1 --inference-config inferenceconfig.json
Verify the result
Make an HTTP POST call to the endpoint with a known user_id and confirm you receive a list of recommendations. Also check logs for latency and errors. Locally, compare recommendations with the test set and compute Precision@K for sample users.
Conclusion
You have just built a complete flow: prepare data, train an ALS model, validate and prepare a scoring script to serve recommendations with Azure Machine Learning. Next steps may include implicit feedback data, hybridizing with content-based features or automating hyperparameter tuning. Tip: start with few users/items to iterate quickly — what real dataset do you want to use?