DEV Community

Cover image for For JS devs: LightRec/lr-ibcf - Open-Source Item-Based Collaborative Filtering in JavaScript
Muktadirul675
Muktadirul675

Posted on

For JS devs: LightRec/lr-ibcf - Open-Source Item-Based Collaborative Filtering in JavaScript

Hello folks! I know we all have seen sections like "Recommended For you", "You may like it" in various platforms. As a developer, I always thought how they made it. After some research I found out that these are made using Machine Learning methods via python.... Wait a bit!
What about the js developers who write their whole code via JS? I am a developer who uses JS a lot and implementing a full machine learning model for every model is tough. That's why I've made "LightRec" (https://lightrec.vercel.app), a platform for recommendation engines and I'll keep publishing new models here.

The first model is "lr-ibcf".

And, its quite easy to use.

1. Installation:

npm install @lightrec/lr-ibcf
Enter fullscreen mode Exit fullscreen mode

2. Import and Initialize

import { RecEngine } from 'lightrec';

// Create a new recommendation engine
const engine = new RecEngine();
Enter fullscreen mode Exit fullscreen mode

3. Pre-Train (Optional)

const interactions = [
    { userId: 'u1', itemId: 'i1', points: 5 },
    { userId: 'u1', itemId: 'i2', points: 3 },
    { userId: 'u2', itemId: 'i1', points: 4 },
]; // Your interaction data

engine.feed(interactions); 
Enter fullscreen mode Exit fullscreen mode

4. Train

const data = {itemId:'i1', userId:'u1', points:3}
engine.act(data.itemId, data.userId, data.points)
Enter fullscreen mode Exit fullscreen mode

5. Recommend

const recommendations = engine.recommendForUser('u1', 5);
console.log(recommendations); // ['i3', 'i4', 'i5', ...]
Enter fullscreen mode Exit fullscreen mode

And, that's it!
Only 5 lines of code and you get Hello folks! I know we all have seen sections like "Recommended For you", "You may like it" in various platforms. As a developer, I always thought how they made it. After some research I found out that these are made using Machine Learning methods via python.... Wait a bit!
What about the js developers who write their whole code via JS? I am a developer who uses JS a lot and implementing a machine learning for every model is tough. That's why I've made "LightRec", a platform for recommendation engines made via JS.

The first model is "lr-ibcf".

Its quite easy to use.

1. Installation:

npm install @lightrec/lr-ibcf
Enter fullscreen mode Exit fullscreen mode

2. Import and Initialize

import { RecEngine } from 'lightrec';

// Create a new recommendation engine
const engine = new RecEngine();
Enter fullscreen mode Exit fullscreen mode

3. Pre-Train (Optional)

const interactions = [
    { userId: 'u1', itemId: 'i1', points: 5 },
    { userId: 'u1', itemId: 'i2', points: 3 },
    { userId: 'u2', itemId: 'i1', points: 4 },
]; // Your interaction data

engine.feed(interactions); 
Enter fullscreen mode Exit fullscreen mode

4. Train

const data = {itemId:'i1', userId:'u1', points:3}
engine.act(data.itemId, data.userId, data.points)
Enter fullscreen mode Exit fullscreen mode

5. Recommend

const recommendations = engine.recommendForUser('u1', 5);
console.log(recommendations); // ['i3', 'i4', 'i5', ...]
Enter fullscreen mode Exit fullscreen mode

And, that's it!
Only 5 lines of code and you get personalized recommendations!

But how this model works?
Its a Item Based Collaborative Filtering method. To know more stay tuned with me, and I'll write another article on how this works.

For now, you may check this github repo:
Github⭐

Have a visit to my portfolio!

Happy Coding!

Top comments (0)