|
Harshit Laxkar Oodles

Harshit Laxkar (Backend-Associate Consultant L2- Development)

Experience:Below 1 yr

Harshit is an accomplished Backend Developer with extensive experience in MEAN stack. His expertise in API implementations, web services, development testing, and deployments has been instrumental in the successful completion of numerous client projects. In addition to his professional endeavors, Harshit is an enthusiastic gamer who stays updated with the latest advancements in technology.

Harshit Laxkar Oodles
Harshit Laxkar
(Associate Consultant L2- Development)

Harshit is an accomplished Backend Developer with extensive experience in MEAN stack. His expertise in API implementations, web services, development testing, and deployments has been instrumental in the successful completion of numerous client projects. In addition to his professional endeavors, Harshit is an enthusiastic gamer who stays updated with the latest advancements in technology.

LanguageLanguages

DotENGLISH

Bilingual

DotHindi

Fluent

Skills
Skills

DotJavascript

60%

DotMySQL

60%

DotAngular/AngularJS

60%

DotStable Diffusion

40%

DotPHP

60%

DotMEAN

60%

DotRESTful API

60%

DotFrontend

60%

DotNode Js

60%

DotFullstack

60%

DotChatgpt

60%

DotExpress.js

60%

DotHTML, CSS

100%
ExpWork Experience / Trainings / Internship

Jul 2023-Feb 2024

Full Stack Developer

Remote


Genefitletics

Remote

Jul 2023-Feb 2024

EducationEducation

2021-2024

Dot

Rajasthan Technical University Kota

Btech-Information Technology

Top Blog Posts
Building a Recommendation Model with KNN

Building a Recommendation Model Using K-Nearest Neighbors (KNN)

Recommendation systems are everywhere these days. From e-commerce sites to recipe apps, they are all around. One of the simplest and most intuitive way to build a recommendation engine is the K-Nearest Neighbors (KNN) algorithm. Here we will walk through how to build a recommendation engine using a pipeline for preprocessing and scaling with code snippets for each step.

K-Nearest Neighbors (KNN)

KNN is a non-parametric, instance-based learning algorithm. It works by finding the k nearest data points (neighbors) to a given input and then providing recommendations based on the similarity between these points. In our recommendation engine, we will be using cosine similarity as the metric to determine how similar data points (recipes or food items) are to each other.

The steps to build the recommendation model are:

  1. Extract the relevant columns for nutrition content.
  2. Scale the data so all features are comparable.
  3. Use KNN to find the nearest neighbors based on cosine distance.
  4. Build a pipeline.
  5. Filter the data based on user preferences (e.g. include or exclude specific ingredients).
  6. Make recommendations and calculate accuracy.

Now, let's get into the code.

Step 1: Extract Nutrition Columns

First we need to extract the relevant nutrition columns, such as calories, fat, protein etc. These features are used to measure the similarity between different food items.

def extract_nutrition_columns(dataframe):
    columns = ['Calories', 'FatContent', 'SaturatedFatContent', 'CholesterolContent',
               'SodiumContent', 'CarbohydrateContent', 'FiberContent', 'SugarContent', 'ProteinContent']
    return dataframe[columns]

Step 2: Scale the Data

We scale the data so no single feature dominates (e.g. calories vs fiber) using StandardScaler.
Here we use StandardScaler to standardize the features by removing the mean and scaling to unit variance. This makes the KNN algorithm work better.

from sklearn.preprocessing import StandardScaler
def scaling(dataframe):
    scaler = StandardScaler()
    prep_data = scaler.fit_transform(dataframe.to_numpy())
    return prep_data, scaler

Step 3: KNN Predictor

Next we use the KNN algorithm to create a model that will find the nearest neighbors based on cosine similarity.
We use cosine metric because it measures the cosine of the angle between two vectors, perfect for measuring similarity between food items based on nutrition values.

from sklearn.neighbors import NearestNeighbors
def nn_predictor(prep_data):
    neigh = NearestNeighbors(metric='cosine', algorithm='brute')
    neigh.fit(prep_data)
    return neigh

Step 4: Pipeline

We then build a pipeline to chain the scaling and neighbor prediction steps together so we can apply the model to new inputs.

The pipeline makes the code cleaner and more modular. It ensures the data is scaled before it goes into the KNN model.

from sklearn.pipeline import Pipeline
from sklearn.preprocessing import FunctionTransformer
def build_pipeline(neigh, scaler, params):
    transformer = FunctionTransformer(neigh.kneighbors, kw_args=params)
    pipeline = Pipeline([('std_scaler', scaler), ('NN', transformer)])
    return pipeline

Step 5: Filter Data by Tags

Sometimes users want to include or exclude specific ingredients from the recommendations. We implement a function to filter data by the given tags.

def extract_ingredient_filtered_data(dataframe, include_tags=None, exclude_tags=None):
    extracted_data = dataframe.copy()
    def filter_row(tags_string):
        if tags_string:
            tags = [tag.strip().lower() for tag in tags_string.split(',')]
            if include_tags:
                for tag in include_tags:
                    if tag.lower() not in tags:
                        return False
            if exclude_tags:
                for tag in exclude_tags:
                    if tag.lower() in tags:
                        return False
        return True
    
    extracted_data = extracted_data[extracted_data['Tags'].apply(filter_row)]
    return extracted_data

Step 6. Make Recommendations

Use the pipeline and KNN model to make recommendations based on the user's input.

import numpy as np
def apply_pipeline(pipeline, _input, extracted_data):
    _input = np.array(_input).reshape(1, -1)
    data = pipeline.transform(_input)
    
    return extracted_data.iloc[data[1][0]], data[0][0]

Step 7: Calculating Accuracy

We calculate the "accuracy" based on how close the recommended items are to the input data.
This simple function converts the cosine distances into percentage-based accuracy scores.

def get_accuracy(distances):
    accuracy = [100 - (i * 100) for i in distances]
    return accuracy

 

Step 8. Combine all the steps 

 The recommend function combines all the steps to make recommendations. It first filters the data based on tags, extracts the relevant nutrition columns, scales the data, and applies the KNN model to provide the nearest neighbors.

def recommend(dataframe, _input, include_tags=[], exclude_tags=[], n_neighbors=5):
    extracted_data = dataframe.copy()
    params = {'n_neighbors': n_neighbors, 'return_distance': True}
    if include_tags or exclude_tags:
        extracted_data = extract_ingredient_filtered_data(dataframe, include_tags, exclude_tags)
    
    params['n_neighbors'] = min(params['n_neighbors'], extracted_data.shape[0])
    if params['n_neighbors'] == 0:
        return None, None
    extracted_cols = extract_nutrition_columns(dataframe)
    prep_data, scaler = scaling(extracted_cols)
    neigh = nn_predictor(prep_data)
    pipeline = build_pipeline(neigh, scaler, params)
    data, distances = apply_pipeline(pipeline, _input, extracted_data)
    
    return data, get_accuracy(distances)

 

Conclusion

Using KNN for recommendation systems is a straightforward yet powerful approach. By combining data preprocessing, filtering, and the KNN algorithm, we've built a model that can make personalized recommendations based on nutritional content. The modular nature of this implementation makes it adaptable to various applications, from food recommendations to product suggestions.

You can easily extend this model by incorporating more features, experimenting with different distance metrics, or enhancing the filtering mechanism based on user preferences.

Category: Artificial Intelligence
Factors Affecting a Recommendation System

Factors Affecting a Recommendation System

Recommendation systems play a strong role in shaping user experiences in the modern world. Whether you are on Netflix, trying to find products on Amazon, or simply scrolling down your favourite social media feed, these systems help you find new and interesting content and products. So, what really matters about how these systems work?

In this post, I'll break up key factors that influence a recommendation system and walk through some simple code snippets that illustrate these ideas.

1. User Data

Any recommendation system is basically built on top of user data. The more detailed and the more accurate data you have about preferences of your users, the better recommendations your system can make. Such data can be explicit-ratings and likes, for example-or implicit-browsing history and clicks, for instance.

For example, a minimalistic user profile will look like this:

user = {

    'id': 1,

    'name': 'John Doe',

    'preferences': {

        'genres': ['action', 'comedy'],

        'liked_items': [101, 202, 303]

    },

    'browsing_history': [404, 505, 606]

}

2. Item Data

The item data is actually the thing you're recommending-that is, its contents or product information. This includes metadata about genres, tags, descriptions, and so on. The better your system is at mirroring the preferences of the user regarding items, the richer the data involved will be.

items = [

    {'id': 101, 'title': 'Action Movie 1', 'genres': ['action', 'thriller']},

    {'id': 202, 'title': 'Comedy Show', 'genres': ['comedy']},

    {'id': 303, 'title': 'Documentary', 'genres': ['history', 'educational']}

]

 

3. Collaborative Filtering

Collaborative filtering is a technique which utilises the preferences of similar users to make recommendations. In case when two users liked the same kind of items in the past, then it is likely that they will like the same kinds of items again.

 

For example, suppose that User A and User B have liked "Action Movie 1" and User A also liked "Comedy Show," then "Comedy Show" would be recommended to User B.

 

def get_similar_users(user, all_users):

    return [other_user for other_user in all_users 

            if any(genre in user['preferences']['genres'] 

                   for genre in other_user['preferences']['genres'])]

 

all_users = [

    {'id': 2, 'preferences': {'genres': ['action', 'drama'], 'liked_items': [101, 404]}},

    {'id': 3, 'preferences': {'genres': ['comedy', 'thriller'], 'liked_items': [202, 505]}}

]

 

similar_users = get_similar_users(user, all_users)

 

4. Content-Based Filtering

This type of filtering filters items on the basis of characteristics of the items themselves, meaning an item recommended would be similar to what a user interacts with. For example, if a user likes action movies, the system will suggest more action movies based on what other users have liked.

def recommend_based_on_content(user, items):

    return [item for item in items 

            if any(genre in user['preferences']['genres'] 

                   for genre in item['genres'])]

 

recommended_items = recommend_based_on_content(user, items)

print(recommended_items)  # [{'id': 101, 'title': 'Action Movie 1', 'genres': ['action', 'thriller']}]

5. Cold Start Problem

The "cold start" problem is one of the issues with recommendation systems. This is when a new user or a new item enters the system, and there is not much to go on for recommendations. We could use popularity trends in general, demographic data, or ask users to set up their first preferences.

def recommend_popular_items(popular_items):

    return popular_items[:5]  # Recommend top 5 popular items

popular_items = [

    {'id': 505, 'title': 'Blockbuster Movie', 'genres': ['action', 'drama']},

    {'id': 606, 'title': 'Hit Comedy', 'genres': ['comedy']}

]

print(recommend_popular_items(popular_items))  # Top 5 recommendations

6. Personalization vs. Diversity

The perfect recommendation system should, therefore, attain harmony between personalizing to the individual's tastes and ensuring diversity. If such a system really displays the most similar items that the user has seen, this would considerably limit their discovery. Deviation from diversity ensures that users are not exposed to new genres or contents in which they could not anticipate their fun.

import random

 

def diversify_recommendations(recommendations, all_items):

    diversified = list(recommendations)

    random_item = random.choice(all_items)

    if random_item not in recommendations:

        diversified.append(random_item)

    return diversified

Diversity helps prevent the recommendation system from becoming stale by adding new or unexpected items into the mix.

7. Feedback Loops

Good recommendations also rely on the feedback the system receives. Positive feedback, such as ratings or clicks will hone the system so that future recommendations will be even closer to your actual preferences. Negative feedback-like skipping or disliking-will do the same, except their effect is opposite in direction.

def update_user_preferences(user, new_like):

    user['preferences']['liked_items'].append(new_like)

update_user_preferences(user, 404)

 

Conclusion

Recommendation systems are complex. But their reasons are obvious. Generally, factors of user data, item data, collaborative filtering, content-based filtering, and the cold start problem determine the change in the system. Understanding these factors can help us create more personalized and better experiences for the users.

 

Category: Artificial Intelligence
Understanding How Recommendation Engines Work

Understanding How Recommendation Engines Work

Recommendation engines are essential components of many modern applications and websites, playing a crucial role in delivering personalised experiences. They help users discover content, products, or services based on their preferences and behaviours. In this blog post, we will explore how recommendation engines work, the different types, and some common algorithms used in the process.

What is a Recommendation Engine?

A recommendation engine is a system that suggests items to users based on various criteria. This could include user behaviour, preferences, or demographic data. They are widely used in e-commerce, streaming services, social media, and more to enhance user engagement and satisfaction.

How Recommendation Engines Work

Recommendation engines typically follow these steps:

  1. Data Collection: The first step involves gathering data. This can include user behaviour data (like clicks, views, purchases), demographic information, and even feedback (ratings or reviews).
  2. Data Processing: The collected data is then processed to extract meaningful features. This often involves cleaning the data, handling missing values, and transforming raw data into a usable format.
  3. Model Selection: The engine chooses a model or algorithm to generate recommendations. The choice of model depends on the type of recommendation approach used.
  4. Recommendation Generation: Based on the selected model, the engine generates a list of recommended items for each user.
  5. Feedback Loop: Recommendations are continuously refined based on user interactions with the suggested items, creating a feedback loop that helps improve the model over time.

Types of Recommendation Systems

Recommendation systems can generally be categorised into three main types:

1. Collaborative Filtering

Collaborative filtering relies on the behavior and preferences of users. It can be further divided into:

  • User-based Collaborative Filtering: This approach recommends items by finding similar users. If User A and User B have a high overlap in items they like, User A might receive recommendations based on what User B enjoys.
  • Item-based Collaborative Filtering: This method looks at item similarity rather than user similarity. If a user likes a particular item, the system recommends other items that are frequently liked together.

2. Content-based Filtering

Content-based filtering recommends items based on the features of the items themselves. For instance, in a movie recommendation system, if a user enjoys action films, the engine might recommend other movies that are labelled as action. This approach requires detailed information about the items, such as genre, director, or keywords.

3. Hybrid Systems

Hybrid systems combine both collaborative and content-based filtering methods. By leveraging the strengths of both approaches, hybrid systems can provide more accurate and diverse recommendations. For example, Netflix uses a hybrid model to suggest movies and shows to its users.

Common Algorithms

Several algorithms are commonly used in recommendation engines:

  • Matrix Factorization: This technique decomposes the user-item interaction matrix into lower-dimensional matrices, revealing latent factors that explain user preferences.
  • K-Nearest Neighbors (KNN): This algorithm identifies similar users or items based on distance metrics and provides recommendations accordingly.
  • Deep Learning: Neural networks, particularly recurrent neural networks (RNNs) and convolutional neural networks (CNNs), can model complex relationships in data for more nuanced recommendations.

Challenges in Recommendation Systems

While recommendation engines are powerful, they face several challenges:

  • Cold Start Problem: New users or items without historical data can hinder the effectiveness of collaborative filtering.
  • Scalability: As the user base and item catalog grow, the computation required for generating recommendations can increase significantly.
  • Diversity vs. Accuracy: Striking a balance between providing accurate recommendations and offering diverse options is essential for user satisfaction.

Conclusion

Recommendation engines have transformed how we interact with technology, making experiences more personalized and engaging. Understanding the underlying mechanisms can help businesses improve their offerings and better serve their users. As data and algorithms continue to evolve, the future of recommendation systems promises even more sophisticated and effective methods of understanding user preferences. Whether you're an entrepreneur, developer, or simply a curious mind, exploring this field can reveal a wealth of insights and opportunities.

 

Category: Artificial Intelligence
Banner

Don't just hire talent,
But build your dream team

Our experience in providing the best talents in accordance with diverse industry demands sets us apart from the rest. Hire a dedicated team of experts to build & scale your project, achieve delivery excellence, and maximize your returns. Rest assured, we will help you start and launch your project, your way – with full trust and transparency!