Skip to content
< writing

Content-based recommendation system with Django and scikit-learn

April 30, 2024 · 1 min read · #django #machine-learning #python

In this article we will explore how we can utilize Django, Django Rest framework TF-IDF Vectorization, Scikit-learn and Cosine Similarity, to build a fully functional Recommendation system.

Propsed Recommendation model:

Proposed Recommendation model
Proposed Recommendation model

Tools to be used:

  • Django
  • Scikit-Learn
  • Pandas / Numpy
  • Django Rest Framework
  • Pickle (For storing the cached model)

Side Note 1: Link to Github for the completed project - Here

Side Note 2: Link to research paper - Here

Prerequisites:

You should have the following setup:

  1. >= Python3.10 Learn more
  2. Docker (Optional)
  3. Pipenv (Installation and Learning Resource)

First Steps:

Firstly we setup our Virtual environment using

Setting up virtual environment and installng Requirements.
Setting up virtual environment and installng Requirements.

Below is the requirements.txt

asgiref==3.6.0
cachetools==4.2.4
certifi==2022.12.7
charset-normalizer==3.1.0
dj-database-url==1.2.0
Django==4.1.7
django-cors-headers==3.14.0
django-filter==22.1
django-googledrive-storage==1.6.0
django-pandas==0.6.6
djangorestframework==3.14.0
google-api-core==2.10.2
google-api-python-client==2.86.0
google-auth==1.35.0
google-auth-httplib2==0.1.0
googleapis-common-protos==1.59.0
gunicorn==20.1.0
httplib2==0.22.0
idna==3.4
itsdangerous==2.1.2
joblib==1.2.0
numpy==1.24.2
pandas==1.5.3
Pillow==9.4.0
protobuf==4.22.4
psycopg2-binary==2.9.5
pyasn1==0.5.0
pyasn1-modules==0.3.0
pyparsing==3.0.9
python-dateutil==2.8.2
python-dotenv==1.0.0
pytz==2022.7.1
requests==2.30.0
rsa==4.9
scikit-learn==1.2.1
scipy==1.10.1
six==1.16.0
sqlparse==0.4.3
threadpoolctl==3.1.0
tzdata==2023.3
uritemplate==4.1.1
urllib3==2.0.2
whitenoise==6.4.0

After that we setup Django using:

django-admin startproject ecommerce

Next we create a new app using:

django-admin startapp recommender

Next we setup the app in our ecommerce/settings.py

settings.py
settings.py

Lets create a recommender/models.py file and include the below content (File Here):

models.py
models.py

We then register the file in recommender/admin.py

from django.contrib import admin
from .models import SimilarityModel

admin.site.register(SimilarityModel)

Now in recommender/views.py we'll have (File Here)

recommender/views.py
recommender/views.py

Finally for the recommender, we setup recommender/apps.py to ensure we train our models immediately our server is spawned on production (Learn more about ready() and Django Signals).

from django.apps import AppConfig

class RecommenderConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'recommender'

    def ready(self):
        from .views import train_model_init
        train_model_init() # Handle initial setup of model

The end.🔚

Conclusion

This article primarily touched the fundamentals of building a full-scale content-based recommendation systems with Django. View the Completed Project to understand more how the various apps within the Django project communicate and interact.

Extra Resources

  1. Learn more about TF-IDF Vectorizer
  2. Learn more about Cosine Similarity
  3. Learn more about Content Based Recommendation Systems - Practical
  4. View the Github Repository Here
  5. Learn more about Content Based Recommendation Systems - Theory