0% found this document useful (0 votes)
10 views24 pages

RecommenderSystem File

Recommender system Practical File

Uploaded by

pavitrarao2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views24 pages

RecommenderSystem File

Recommender system Practical File

Uploaded by

pavitrarao2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Practical-1

➢ Aim: Create a bar chart for the categories of various books along with their
recommendations.

Source Code:

import matplotlib.pyplot as p
h=[15,46,56,33,12]
l=[1,2,3,4,5]
bookList=['Hamlet','Frankenstein','Moby-Dick','The great Gatsby','Pride and Prejudice']
p.bar(l,h, label="Book recommendation",tick_label = bookList,
width = 0.8, color = ['violet', 'lavender'])
p.xlabel('Books')
p.ylabel('Recommendations')
p.title('My bar chart!')
p.legend(loc="lower left")
p.show()

Output:
Practical-2
➢ Aim: To familiarize students with loading, exploring, and preprocessing datasets
required for recommender systems.

Description:

Recommender systems are algorithms that suggest relevant items to users based on their
preferences or past behavior. They are commonly used in platforms like Netflix, Amazon, and
Spotify to suggest movies, products, or music tailored to each user’s interests.

Preparing Data for Recommender Systems

Creating a reliable recommendation model requires good data preparation. The key steps involve:
1. Loading the Dataset:
o The dataset usually contains user-item interactions, where users rate items. This step
involves reading the data into a structured format (e.g., a DataFrame) for easy
manipulation and analysis.
2. Exploring the Dataset:
o To understand the dataset’s structure, we inspect the first few rows, the data types of each
column, and summary statistics like average ratings. This helps identify important
features and potential data issues.
3. Checking for Missing Values:
o Missing values are common and can impact the model’s effectiveness. We check for and
handle missing data appropriately to maintain data quality.
4. Preprocessing the Data:
o Dropping Irrelevant Columns: Some columns, like timestamps, may not provide useful
information for recommendation purposes. Dropping them simplifies the dataset and
reduces processing time.
o Normalizing Ratings: Ratings can vary widely across users; normalization ensures they’re
on a comparable scale, allowing the model to interpret them consistently.
5. Visualizing Ratings Distribution:
o Visualizing the rating distribution helps us understand if users tend to rate items highly or
neutrally. A histogram or distribution chart reveals rating biases that can affect
recommendation quality.
6. Creating the User-Item Matrix:
o The user-item matrix is the foundation of collaborative filtering. Each row represents a
user, each column represents an item, and cells contain the user’s rating for that item. This
structured representation enables algorithms to analyze user-item relationships for making
recommendations.

By understanding, cleaning, and structuring the data in these ways, you ensure it’s ready for use in a
recommender system, which can then accurately predict user preferences and provide meaningful,
personalized recommendations.
Source Code:

# Import necessary libraries


import pandas as pd
import numpy as np
from sklearn.preprocessing import MinMaxScaler
import matplotlib.pyplot as plt

# Step 1: Load the dataset


# For this example, we are simulating with a small dataset. You can replace this with an actual
dataset like MovieLens
data = {
'UserID': [1, 2, 3, 4, 5, 1, 2, 3, 4, 5],
'MovieID': [101, 102, 103, 104, 105, 102, 103, 104, 101, 105],
'Rating': [4, 5, 2, 3, 5, 3, 4, 1, 5, 3],
'Timestamp': [
978300760, 978302109, 978301968, 978300275, 978824291,
978300760, 978302109, 978301968, 978300275, 978824291
]
}
df = pd.DataFrame(data)

# Step 2: Explore the dataset


print("First few rows of the dataset:")
print(df.head())

print("\nDataset Information:")
print(df.info())

print("\nSummary Statistics:")
print(df.describe())

# Step 3: Check for missing values


print("\nMissing values in each column:")
print(df.isnull().sum())

# Step 4: Preprocessing - Removing unnecessary columns (Timestamp)


df = df.drop(columns=['Timestamp'])
print("\nDataset after dropping 'Timestamp' column:")
print(df.head())

# Step 5: Normalizing the Ratings (Optional but useful for certain algorithms)
scaler = MinMaxScaler()
df['Rating_Normalized'] = scaler.fit_transform(df[['Rating']])
print("\nDataset after normalizing the 'Rating' column:")
print(df.head())

# Step 6: Visualize Ratings Distribution (Optional step)


plt.hist(df['Rating'], bins=5, alpha=0.7, color='blue')
plt.title('Distribution of Ratings')
plt.xlabel('Rating')
plt.ylabel('Frequency')
plt.show()

# Step 7: Prepare User-Item Matrix (for Collaborative Filtering)


user_item_matrix = df.pivot(index='UserID', columns='MovieID', values='Rating').fillna(0)
print("\nUser-Item Matrix:")
print(user_item_matrix)

Output:
Practical-3
➢ Aim: Write a program for content-based recommendation system in Python.
Source Code:

import pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity

# Sample dataset of books


data = {
'Title': [
'Hamlet',
'Frankenstein',
'Moby-Dick',
'The Great Gatsby',
'Pride and Prejudice'
],
'Description': [
'A tragedy play by Shakespeare involving a prince seeking revenge.',
'A science fiction novel about a scientist creating a monster.',
'A novel about a sea captain’s obsession with a white whale.',
'A novel set in the Jazz Age, exploring themes of wealth and love.',
'A romantic novel about manners and matrimonial machinations.'
]
}

# Creating a DataFrame
df = pd.DataFrame(data)

# Displaying the DataFrame


print("Dataset:\n", df)

tfidf = TfidfVectorizer(stop_words='english')

# Applying TF-IDF to the descriptions


tfidf_matrix = tfidf.fit_transform(df['Description'])

# Calculating the cosine similarity between all books


cosine_sim = cosine_similarity(tfidf_matrix, tfidf_matrix)

# Function to get recommendations


def get_recommendations(title, cosine_sim=cosine_sim):
# Getting the index of the book that matches the title
idx = df[df['Title'] == title].index[0]

# Getting the pairwise similarity scores of all books with that book
sim_scores = list(enumerate(cosine_sim[idx]))
# Sorting the books based on the similarity scores

sim_scores = sorted(sim_scores, key=lambda x: x[1], reverse=True)

# Getting the scores of the 3 most similar books (excluding the first one as it is the same book)
sim_scores = sim_scores[1:4]

# Getting the indices of the similar books


book_indices = [i[0] for i in sim_scores]

# Returning the titles of the top 3 similar books


return df['Title'].iloc[book_indices]

# Example usage: Getting recommendations for 'Hamlet'


print("\nRecommendations for 'Hamlet':")
print(get_recommendations('Hamlet'))

Output:
Practical-4
➢ Aim: To create a book-based recommender system.
Source Code:

import pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity

# Sample book dataset


data = {
'Title': [
'The Catcher in the Rye', 'To Kill a Mockingbird', 'Pride and Prejudice',
'The Great Gatsby', 'Moby-Dick', '1984', 'The Odyssey', 'War and Peace',
'Crime and Punishment', 'The Adventures of Huckleberry Finn'
],
'Author': [
'J.D. Salinger', 'Harper Lee', 'Jane Austen', 'F. Scott Fitzgerald',
'Herman Melville', 'George Orwell', 'Homer', 'Leo Tolstoy',
'Fyodor Dostoevsky', 'Mark Twain'
],
'Genre': [
'Classic, Coming-of-age', 'Classic, Southern Gothic', 'Classic, Romance',
'Classic, Tragedy', 'Classic, Adventure', 'Dystopian, Political Fiction',
'Epic, Mythology', 'Historical, Epic', 'Psychological, Crime', 'Adventure, Satire'
]
}

# Convert the data into a DataFrame


df_books = pd.DataFrame(data)

# Combine title, author, and genre as a single content feature


df_books['Content'] = df_books['Title'] + ' ' + df_books['Author'] + ' ' + df_books['Genre']

# Vectorize the content feature using TF-IDF


tfidf = TfidfVectorizer(stop_words='english')
tfidf_matrix = tfidf.fit_transform(df_books['Content'])

# Calculate cosine similarity between the books


cosine_sim = cosine_similarity(tfidf_matrix, tfidf_matrix)

# Function to get book recommendations based on a selected book title


def get_recommendations(title, cosine_sim=cosine_sim):
# Get the index of the selected book
idx = df_books.index[df_books['Title'] == title][0]

# Get pairwise similarity scores for all books with that book
sim_scores = list(enumerate(cosine_sim[idx]))
# Sort the books by similarity scores in descending order
sim_scores = sorted(sim_scores, key=lambda x: x[1], reverse=True)

# Get indices of the top 5 most similar books (excluding the selected book itself)
book_indices = [i[0] for i in sim_scores[1:6]]

# Return the titles of the top 5 recommended books


return df_books['Title'].iloc[book_indices]

# Test the recommender system


selected_book = 'The Great Gatsby'
recommendations = get_recommendations(selected_book)
print(f"Books recommended based on '{selected_book}':")
print(recommendations)

Output:
Practical-5
➢ Aim: To create a movie-based recommender system.
Source Code:

import pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity

# Sample movie dataset


data = {
'Title': [
'The Matrix', 'Inception', 'Interstellar', 'The Dark Knight',
'Pulp Fiction', 'The Shawshank Redemption', 'Fight Club',
'Forrest Gump', 'The Godfather', 'Gladiator'
],
'Genre': [
'Action, Sci-Fi', 'Action, Sci-Fi', 'Adventure, Drama, Sci-Fi',
'Action, Crime, Drama', 'Crime, Drama', 'Drama', 'Drama',
'Drama, Romance', 'Crime, Drama', 'Action, Adventure, Drama'
],
'Description': [
'A computer hacker learns about the true nature of reality and his role in it.',
'A thief who enters the dreams of others is given a chance at redemption.',
'Explorers travel through a wormhole in space to ensure humanity’s survival.',
'A vigilante faces challenges in a city plagued by crime.',
'Two hitmen embark on a series of adventures in Los Angeles.',
'Two imprisoned men bond over a number of years in a prison.',
'An office worker creates an underground fight club.',
'A man with a low IQ recounts the experiences of his life.',
'A powerful mafia family deals with betrayal and family loyalty.',
'A betrayed Roman general seeks revenge for the death of his family.'
]
}

# Create a DataFrame
df_movies = pd.DataFrame(data)

# Combine Genre and Description for a unified content feature


df_movies['Content'] = df_movies['Genre'] + ' ' + df_movies['Description']

# Vectorize the content feature using TF-IDF


tfidf = TfidfVectorizer(stop_words='english')
tfidf_matrix = tfidf.fit_transform(df_movies['Content'])

# Calculate cosine similarity between movies


cosine_sim = cosine_similarity(tfidf_matrix, tfidf_matrix)
# Function to get movie recommendations based on a selected movie title
def get_recommendations(title, cosine_sim=cosine_sim):
# Get the index of the selected movie
idx = df_movies.index[df_movies['Title'] == title][0]

# Get pairwise similarity scores for all movies with that movie
sim_scores = list(enumerate(cosine_sim[idx]))

# Sort the movies by similarity scores in descending order


sim_scores = sorted(sim_scores, key=lambda x: x[1], reverse=True)

# Get the indices of the top 5 most similar movies (excluding the selected movie itself)
movie_indices = [i[0] for i in sim_scores[1:6]]

# Return the titles of the top 5 recommended movies


return df_movies['Title'].iloc[movie_indices]

# Test the recommender system


selected_movie = 'The Matrix'
recommendations = get_recommendations(selected_movie)
print(f"Movies recommended based on '{selected_movie}':")
print(recommendations)

Output:
Practical-6
➢ Aim: Write a program for collaborative filtering using Python.
Source Code:

import numpy as np
import pandas as pd
from sklearn.metrics.pairwise import cosine_similarity

# Sample user-item rating matrix


ratings_data = {
'User1': [5, 4, 1, 0, 0],
'User2': [4, 0, 2, 3, 0],
'User3': [1, 2, 0, 5, 3],
'User4': [0, 3, 4, 0, 5],
'User5': [0, 0, 5, 4, 4]
}

# Convert data to DataFrame (users as columns, items as rows)


df_ratings = pd.DataFrame(ratings_data, index=['Item1', 'Item2', 'Item3', 'Item4', 'Item5'])

# Function to calculate cosine similarity between users


def calculate_similarity(matrix):
# Fill NaNs with 0s and convert to numpy array
matrix = matrix.fillna(0).T
similarity = cosine_similarity(matrix)
return pd.DataFrame(similarity, index=matrix.index, columns=matrix.index)

# Function to predict the rating for a specific user-item pair


def predict_rating(user, item, df_ratings, similarity_matrix):
# Get the ratings of other users for the item
ratings_for_item = df_ratings.loc[item]

# Get similarity scores for the target user with all other users
similarity_scores = similarity_matrix[user]

# Filter out users who haven't rated the item


non_zero_ratings = ratings_for_item[ratings_for_item > 0]
relevant_similarities = similarity_scores[non_zero_ratings.index]

if relevant_similarities.sum() == 0:
return 0

# Predict the rating using weighted average of similar users' ratings


predicted_rating = np.dot(non_zero_ratings, relevant_similarities) / relevant_similarities.sum()

return predicted_rating
# Calculate the user similarity matrix
user_similarity = calculate_similarity(df_ratings)

# Predict the rating for User1 for Item4 (which is missing in the data)
user = 'User1'
item = 'Item4'
predicted_rating = predict_rating(user, item, df_ratings, user_similarity)

print(f"Predicted rating for {user} on {item}: {predicted_rating:.2f}")

# Show the similarity matrix


print("\nUser Similarity Matrix:")
print(user_similarity)

Output:
Practical-7
➢ Aim: Write a program for SVD in Python for recommendation system.
Source Code:

import numpy as np
import pandas as pd
from numpy.linalg import svd

# Sample user-item interaction matrix (ratings)


data = {'User1': [5, 3, 0, 1],
'User2': [4, 0, 0, 1],
'User3': [1, 1, 0, 5],
'User4': [1, 0, 0, 4],
'User5': [0, 1, 5, 4]}

# Convert to a DataFrame
df = pd.DataFrame(data, index=['Item1', 'Item2', 'Item3', 'Item4'])

print("Original User-Item Matrix:\n", df)

# Convert the DataFrame to a NumPy array


matrix = df.values

# Perform Singular Value Decomposition (SVD)


U, sigma, Vt = svd(matrix, full_matrices=False)

# Convert sigma (which is a vector) into a diagonal matrix


sigma = np.diag(sigma)

# Reconstruct the original matrix


reconstructed_matrix = np.dot(np.dot(U, sigma), Vt)

print("\nReconstructed Matrix (after SVD):\n", reconstructed_matrix)

# Recommend items for a specific user (e.g., User 3)


user_idx = 2 # Index of User3
user_ratings = reconstructed_matrix[:, user_idx]

# Sort items based on predicted ratings


recommended_items = np.argsort(user_ratings)[::-1]

# Print recommendations
print("\nRecommendations for User3 based on SVD:")
for i in recommended_items:
print(f"Item {i+1}: {user_ratings[i]}")
Output:
Practical-8
➢ Aim: Write a program to design and implement Hybrid recommender system.
Source Code:

import pandas as pd
import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
from sklearn.neighbors import NearestNeighbors

# Collaborative Filtering using Nearest Neighbors


def collaborative_filtering(df_ratings, user_id, k=2):
# Convert the dataframe into a matrix for collaborative filtering
df_ratings_filled = df_ratings.fillna(0) # Fill missing values with 0
user_idx = df_ratings.columns.get_loc(user_id)

# Fit a Nearest Neighbors model to find similar users


knn = NearestNeighbors(metric='cosine', algorithm='brute')
knn.fit(df_ratings_filled.T)

# Get the nearest neighbors (users with similar rating patterns)


distances, indices = knn.kneighbors([df_ratings_filled.T[user_idx]], n_neighbors=k)

# Aggregate ratings of the neighbors


neighbor_ratings = df_ratings_filled.T.iloc[indices.flatten()].mean(axis=0)
return neighbor_ratings

# Content-Based Filtering using Item Profiles


def content_based_filtering(df_items, df_ratings, user_id):
# Create item profiles using TF-IDF on genres
tfidf = TfidfVectorizer(stop_words='english')
tfidf_matrix = tfidf.fit_transform(df_items['Genre'])
item_profiles = pd.DataFrame(tfidf_matrix.toarray(), index=df_items['Item'],
columns=tfidf.get_feature_names_out())

# Get items rated by the user


user_ratings = df_ratings[user_id].dropna()

# Calculate weighted average of item profiles for the user


user_profile = np.dot(user_ratings.values, item_profiles.loc[user_ratings.index]) /
np.sum(user_ratings)

# Calculate similarity between user's profile and all item profiles


similarities = cosine_similarity([user_profile], item_profiles)[0]
# Return the similarity scores for all items
return pd.Series(similarities, index=item_profiles.index)

# Hybrid Recommendation
def hybrid_recommendation(collab_scores, content_scores, alpha=0.5):
# Combine the two scores using weighted average
hybrid_scores = (alpha * collab_scores) + ((1 - alpha) * content_scores)

# Sort and recommend the top items


recommendations = hybrid_scores.sort_values(ascending=False)
return recommendations

# Sample dataset with user ratings (User-Item Matrix)


ratings_data = {
'User1': [5, np.nan, 3, np.nan, 4],
'User2': [4, np.nan, 2, 5, np.nan],
'User3': [np.nan, 3, 5, 2, np.nan],
'User4': [5, 4, np.nan, 3, np.nan],
'User5': [np.nan, 5, np.nan, 4, np.nan]
}
df_ratings = pd.DataFrame(ratings_data, index=['Item1', 'Item2', 'Item3', 'Item4', 'Item5'])

# Sample dataset for items (Item Profiling with genres)


items_data = {'Item': ['Item1', 'Item2', 'Item3', 'Item4', 'Item5'],
'Genre': ['Action', 'Drama', 'Action Comedy', 'Thriller', 'Comedy']}
df_items = pd.DataFrame(items_data)

# Get recommendations for User1


user_id = 'User1'

# Collaborative Filtering recommendations


collab_scores = collaborative_filtering(df_ratings, user_id)

# Content-Based Filtering recommendations


content_scores = content_based_filtering(df_items, df_ratings, user_id)

# Combine them using a hybrid approach


hybrid_scores = hybrid_recommendation(collab_scores, content_scores)

print(f"\nHybrid Recommendations for {user_id}:")


print(hybrid_scores)
Output:
Practical-9
➢ Aim: Write a program to apply matrix factorization techniques like SVD for
improving recommendation accuracy.

Source Code:

import numpy as np
import pandas as pd
from sklearn.metrics.pairwise import cosine_similarity
from scipy.sparse.linalg import svds

# Sample User-Item Ratings Matrix


ratings_data = {
'User1': [5, 0, 3, 0, 4],
'User2': [4, 0, 2, 5, 0],
'User3': [0, 3, 5, 2, 0],
'User4': [5, 4, 0, 3, 0],
'User5': [0, 5, 0, 4, 0]
}
df_ratings = pd.DataFrame(ratings_data, index=['Item1', 'Item2', 'Item3', 'Item4', 'Item5'])

# Convert the DataFrame to a NumPy matrix and fill NaNs with zeros
ratings_matrix = df_ratings.values

# Normalize the matrix by subtracting the mean rating for each user
user_ratings_mean = np.mean(ratings_matrix, axis=1)
ratings_matrix_normalized = ratings_matrix - user_ratings_mean.reshape(-1, 1)

# Perform SVD
U, sigma, Vt = svds(ratings_matrix_normalized, k=2) # k is the number of latent features
sigma = np.diag(sigma)

# Predict ratings by reconstructing the matrix


predicted_ratings = np.dot(np.dot(U, sigma), Vt) + user_ratings_mean.reshape(-1, 1)
predicted_ratings_df = pd.DataFrame(predicted_ratings, columns=df_ratings.columns,
index=df_ratings.index)

# Recommend items for a given user based on predicted ratings


def recommend_items(user, df_ratings, predicted_ratings_df, num_recommendations=3):
user_ratings = df_ratings[user]
user_predictions = predicted_ratings_df[user]

# Recommend items that the user hasn't rated yet, sorted by predicted rating
unrated_items = user_ratings[user_ratings == 0].index
recommendations =
user_predictions[unrated_items].sort_values(ascending=False).head(num_recommendations)

return recommendations
# Example: Get recommendations for User1
user = 'User1'
recommendations = recommend_items(user, df_ratings, predicted_ratings_df)
print(f"Top recommendations for {user}:")
print(recommendations)

Output:
Practical-10
➢ Aim: Write a program to show item profiling and user profiling in recommender
system.

Source Code:

import pandas as pd
import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
from sklearn.preprocessing import normalize

# Sample dataset with user ratings (User-Item Matrix)


ratings_data = {
'User1': [5, 0, 3, 0, 4],
'User2': [4, 0, 2, 5, 0],
'User3': [0, 3, 5, 2, 0],
'User4': [5, 4, 0, 3, 0],
'User5': [0, 5, 0, 4, 0]
}
df_ratings = pd.DataFrame(ratings_data, index=['Item1', 'Item2', 'Item3', 'Item4', 'Item5'])

# Sample dataset for items (Item Profiling with genres)


items_data = {'Item': ['Item1', 'Item2', 'Item3', 'Item4', 'Item5'],
'Genre': ['Action', 'Drama', 'Action Comedy', 'Thriller', 'Comedy']}
df_items = pd.DataFrame(items_data)

# Function to profile items using content (genres)


def item_profiling(df_items):
tfidf = TfidfVectorizer(stop_words='english')
tfidf_matrix = tfidf.fit_transform(df_items['Genre'])
item_profiles = pd.DataFrame(tfidf_matrix.toarray(), index=df_items['Item'],
columns=tfidf.get_feature_names_out())
return item_profiles

# Function to create user profiles based on their ratings

def user_profiling(df_ratings, item_profiles):


# User profile is the weighted average of item profiles, weighted by the user's ratings
user_profiles = {}
for user in df_ratings.columns:
user_ratings = df_ratings[user]
weighted_sum = np.dot(user_ratings, item_profiles)
user_profile = weighted_sum / np.sum(user_ratings) if np.sum(user_ratings) != 0 else
weighted_sum
user_profiles[user] = user_profile
return pd.DataFrame(user_profiles)
# Normalize item and user profiles to enhance similarity calculations
def normalize_profiles(profiles):
return pd.DataFrame(normalize(profiles, axis=1), index=profiles.index,
columns=profiles.columns)

# Create item profiles


item_profiles = item_profiling(df_items)
item_profiles = normalize_profiles(item_profiles)

# Create user profiles based on their interactions and the item profiles
user_profiles = user_profiling(df_ratings, item_profiles)
user_profiles = normalize_profiles(user_profiles.T).T

print("Item Profiles (Normalized):")


print(item_profiles)
print("\nUser Profiles (Normalized):")
print(user_profiles)

# Function to recommend items to a user based on profile similarity


def recommend_items(user, user_profiles, item_profiles, df_ratings):
user_profile = user_profiles[user].values
item_similarity = cosine_similarity([user_profile], item_profiles.values)[0]

# Recommend items the user hasn't rated yet, sorted by similarity score
user_ratings = df_ratings[user]
recommendations = [(item, score) for item, score, rating in zip(df_ratings.index, item_similarity,
user_ratings) if rating == 0]
recommendations = sorted(recommendations, key=lambda x: x[1], reverse=True)

return recommendations

# Recommend items for User1


user = 'User1'
recommendations = recommend_items(user, user_profiles, item_profiles, df_ratings)
print(f"\nRecommendations for {user}:")
for item, score in recommendations:
print(f"Item: {item}, Similarity Score: {score:.2f}")
Output:
Practical-11
➢ Aim: To calculate and interpret evaluation metrics such as RMSE, MAE,
Precision, Recall and F1- score for assessing recommender system.

Source Code:

import numpy as np
from sklearn.metrics import mean_absolute_error, mean_squared_error, precision_score,
recall_score, f1_score

# Sample ground truth ratings (actual) and predicted ratings (predicted)


# Assuming 1 = relevant, 0 = irrelevant
actual = np.array([1, 0, 1, 1, 0, 1, 0, 1, 0, 0])
predicted = np.array([1, 0, 1, 0, 0, 1, 0, 1, 1, 0])

# 1. RMSE (Root Mean Squared Error)


rmse = np.sqrt(mean_squared_error(actual, predicted))
print(f"RMSE: {rmse}")

# 2. MAE (Mean Absolute Error)


mae = mean_absolute_error(actual, predicted)
print(f"MAE: {mae}")

# 3. Precision
precision = precision_score(actual, predicted)
print(f"Precision: {precision}")

# 4. Recall
recall = recall_score(actual, predicted)
print(f"Recall: {recall}")

# 5. F1-Score
f1 = f1_score(actual, predicted)
print(f"F1-Score: {f1}")

Output:

You might also like