DEV Community

CHANTSZCHEUK
CHANTSZCHEUK

Posted on

My Journey Learning Artificial Intelligence - Day 10

Today I dived into recommendation systems. I learned about collaborative filtering, content-based filtering, and hybrid approaches. Using the Surprise library, I implemented a simple movie recommendation system based on user ratings.

I was intrigued by how these systems can personalize user experiences across various platforms, from e-commerce to streaming services.

from surprise import Dataset, Reader, SVD
from surprise.model_selection import train_test_split
from surprise import accuracy

# Load the movielens-100k dataset
data = Dataset.load_builtin('ml-100k')

# Split the data into training and testing sets
trainset, testset = train_test_split(data, test_size=.25)

# Use SVD algorithm
algo = SVD()

# Train the algorithm on the trainset
algo.fit(trainset)

# Predict ratings for the testset
predictions = algo.test(testset)

# Compute and print Root Mean Squared Error
rmse = accuracy.rmse(predictions)
print(f'RMSE: {rmse}')

# Make recommendations for a specific user
user_id = 196  # example user id
item_id = 302  # example item id
pred = algo.predict(user_id, item_id)
print(f'Predicted rating for user {user_id} on item {item_id}: {pred.est}')
Enter fullscreen mode Exit fullscreen mode

Top comments (0)