Personalized Movie Recommendation System

Author

Ozge Gundogan

Published

November 26, 2026

1. Introduction

In this assignment, I will build a personalized movie recommendation system using survey data. The dataset contains ratings from 10 users for 6 movies. Some ratings are missing because not every user has rated every movie.

In the previous assignment, I used the Global Baseline Estimate (GBE) method to predict missing ratings. However, this method did not provide personalized recommendations.

For this assignment, I will use Item-Based Collaborative Filtering to recommend movies based on each user’s preferences. I will also evaluate the model using RMSE and MAE to measure how accurately it predicts movie ratings.

2. Planned Approach

First, I will import the movie ratings dataset from GitHub and explore the data. I will check the number of users, movies, available ratings, and missing values.

Next, I will create a user-movie rating matrix and calculate movie similarities using Cosine Similarity. I will use these similarities and each user’s previous ratings to predict missing ratings and generate personalized recommendations.

Finally, I will divide the available ratings into 80% training data and 20% test data. I will build the model using the training data and compare its predictions with the actual ratings in the test data. I will use RMSE and MAE to evaluate the model’s performance.

3. Anticipated Challenges

One challenge is the small dataset, which contains only 10 users and 6 movies. This may make it difficult to identify strong similarities between movies. Another challenge is that many users gave high ratings to the movies. This may produce high similarity scores and make the predictions very close to each other.

4. Code Base

4.1 Load Required Packages

I will load the tidyverse package to import, clean, transform, and analyze the movie ratings data.

library(tidyverse)
Warning: package 'ggplot2' was built under R version 4.6.2
Warning: package 'tidyr' was built under R version 4.6.2
Warning: package 'readr' was built under R version 4.6.2
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.2.1     ✔ readr     2.2.0
✔ forcats   1.0.1     ✔ stringr   1.6.0
✔ ggplot2   4.0.4     ✔ tibble    3.3.1
✔ lubridate 1.9.5     ✔ tidyr     1.3.2
✔ purrr     1.2.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors

4.2 Load the Dataset

I will import the movie ratings dataset directly from GitHub using the read_delim function. The CSV file uses semicolons (;) to separate values. I will also identify missing ratings as NA and check the imported data.

url <- "https://raw.githubusercontent.com/OzgeG01/DATA607-Assignment/refs/heads/main/movie_ratings.csv"

movie_ratings <- read_delim(
  url,
  delim = ";",
  na = c("", "NA"),
  show_col_types = FALSE
)

movie_ratings
# A tibble: 60 × 4
   name   movie_id title     rating
   <chr>     <dbl> <chr>      <dbl>
 1 Deniz         1 Inception      5
 2 Koray         1 Inception      5
 3 Okan          1 Inception      4
 4 Pelin         1 Inception     NA
 5 Serpil        1 Inception      5
 6 Azra          1 Inception      4
 7 Derya         1 Inception      5
 8 Erkan         1 Inception     NA
 9 Can           1 Inception      5
10 Yeliz         1 Inception      5
# ℹ 50 more rows

4.3 Explore the Dataset

I will explore the dataset by checking the number of users, movies, available ratings, and missing ratings. This will help me understand the dataset before building the recommendation system.

movie_ratings %>%
  summarise(
    total_users = n_distinct(name),
    total_movies = n_distinct(movie_id),
    available_ratings = sum(!is.na(rating)),
    missing_ratings = sum(is.na(rating))
  )
# A tibble: 1 × 4
  total_users total_movies available_ratings missing_ratings
        <int>        <int>             <int>           <int>
1          10            6                46              14

The dataset contains 10 users and 6 movies, with 60 possible ratings. There are 46 available ratings and 14 missing ratings. These missing values represent movies that users have not rated. I will use the available ratings to build a personalized recommendation system and predict the missing ratings.

4.4 Create the User-Movie Rating Matrix

I will first create a user-movie rating table using the pivot_wider function. Each row will represent a user, and each column will represent a movie. The values will show the ratings given by users. Then, I will use the column_to_rownames function to set user names as row names and convert the table into a numeric matrix using the as.matrix function. Missing ratings will remain as NA. This matrix will help me calculate similarities between movies.

rating_matrix <- movie_ratings %>%
  select(name, title, rating) %>%
  pivot_wider(
    names_from = title,
    values_from = rating
  )

movie_matrix <- rating_matrix %>%
  column_to_rownames("name") %>%
  as.matrix()

movie_matrix
       Inception Interstellar Barbie Oppenheimer Parasite Inside Out
Deniz          5           NA      3           5        4          3
Koray          5            5      4           4        3         NA
Okan           4            5     NA           5        5          4
Pelin         NA            5      3           4        4         NA
Serpil         5            5     NA           5        5          3
Azra           4            5      4           4       NA          3
Derya          5            5      2          NA       NA          5
Erkan         NA            5     NA           4        5         NA
Can            5            5     NA           4        4          4
Yeliz          5            5      5           4        5         NA

The user-movie rating matrix contains 10 rows and 6 columns. Each row represents a user, and each column represents a movie. The matrix includes 46 available ratings and 14 missing ratings (NA). For example, Deniz rated five movies but did not rate Interstellar. Erkan rated only three movies, so three ratings are missing. These missing ratings will be predicted using the recommendation model. The matrix is now ready for calculating similarities between movies.

4.5 Create a Cosine Similarity Function

I will create a function to calculate Cosine Similarity between two movies. The function will compare ratings from users who rated both movies. It will ignore missing values (NA) and calculate a similarity score. If fewer than two users rated both movies, or if the denominator is zero, the function will return NA.

cosine_similarity <- function(x, y) {
  
  valid <- !is.na(x) & !is.na(y)
  
  x <- x[valid]
  y <- y[valid]
  
  if (length(x) < 2) {
    return(NA_real_)
  }
  
  denominator <- sqrt(sum(x^2)) * sqrt(sum(y^2))
  
  if (denominator == 0) {
    return(NA_real_)
  }
  
  sum(x * y) / denominator
}

4.6 Calculate Movie Similarities

I will use the cosine_similarity function to calculate similarities between all movies. The results will be stored in a similarity matrix, where each row and column represents a movie. I will round the similarity scores to three decimal places to make them easier to read.

movie_similarity <- outer(
  seq_len(ncol(movie_matrix)),
  seq_len(ncol(movie_matrix)),
  Vectorize(function(i, j) {
    cosine_similarity(movie_matrix[, i], movie_matrix[, j])
  })
)

rownames(movie_similarity) <- colnames(movie_matrix)
colnames(movie_similarity) <- colnames(movie_matrix)

round(movie_similarity, 3)
             Inception Interstellar Barbie Oppenheimer Parasite Inside Out
Inception        1.000        0.995  0.954       0.988    0.977      0.978
Interstellar     0.995        1.000  0.962       0.995    0.987      0.981
Barbie           0.954        0.962  1.000       0.967    0.978      0.878
Oppenheimer      0.988        0.995  0.967       1.000    0.987      0.982
Parasite         0.977        0.987  0.978       0.987    1.000      0.984
Inside Out       0.978        0.981  0.878       0.982    0.984      1.000

The similarity scores are generally high. Inception and Interstellar have a similarity score of 0.995, while Barbie and Inside Out have the lowest similarity score of 0.878. These results suggest that most movies have similar rating patterns. However, many users gave high ratings to the movies, which may explain the high similarity scores. The dataset is also small, and some movie pairs have only a few ratings in common. Therefore, the similarity scores may not always be reliable.

4.7 Predict Missing Ratings

I will create a function to predict missing movie ratings for each user. The function will use the user’s available ratings and the similarity scores between movies. It will calculate a weighted average, where movies with higher similarity scores have more influence on the predicted rating. The function will ignore missing ratings and exclude the movie being predicted. If there are no available ratings with valid similarity scores, the function will return NA.

predict_rating <- function(user_index, movie_index) {
  
  user_ratings <- movie_matrix[user_index, ]
  
  similarities <- movie_similarity[movie_index, ]
  
  valid <- !is.na(user_ratings) &
    !is.na(similarities) &
    seq_along(user_ratings) != movie_index
  
  if (!any(valid)) {
    return(NA_real_)
  }
  
  predicted <- sum(
    user_ratings[valid] * similarities[valid]
  ) / sum(abs(similarities[valid]))
  
  return(predicted)
}

4.8 Generate Personalized Recommendations

I will use the predict_rating function to estimate missing ratings for each user. I will first identify the movies that each user has not rated. Then, I will calculate predicted ratings using the user’s previous ratings and movie similarities. Finally, I will create a table showing the recommended movies and their predicted ratings, sorted from highest to lowest for each user.

recommendations <- expand_grid(
  user_index = seq_len(nrow(movie_matrix)),
  movie_index = seq_len(ncol(movie_matrix))
) %>%
  filter(
    is.na(movie_matrix[
      cbind(user_index, movie_index)
    ])
  ) %>%
  mutate(
    name = rating_matrix$name[user_index],
    title = colnames(movie_matrix)[movie_index],
    predicted_rating = map2_dbl(
      user_index,
      movie_index,
      predict_rating
    )
  ) %>%
  select(name, title, predicted_rating) %>%
  arrange(name, desc(predicted_rating))

recommendations
# A tibble: 14 × 3
   name   title        predicted_rating
   <chr>  <chr>                   <dbl>
 1 Azra   Parasite                 4.00
 2 Can    Barbie                   4.40
 3 Deniz  Interstellar             4.01
 4 Derya  Oppenheimer              4.26
 5 Derya  Parasite                 4.25
 6 Erkan  Barbie                   4.67
 7 Erkan  Inside Out               4.67
 8 Erkan  Inception                4.67
 9 Koray  Inside Out               4.20
10 Okan   Barbie                   4.61
11 Pelin  Inside Out               4.03
12 Pelin  Inception                4.01
13 Serpil Barbie                   4.63
14 Yeliz  Inside Out               4.80

The recommendation system generated 14 predicted ratings for movies that users have not rated yet. For example, Barbie has a predicted rating of 4.67 for Erkan and 4.61 for Okan. For Derya, Oppenheimer has a slightly higher predicted rating (4.26) than Parasite (4.25). Inside Out has the highest predicted rating of 4.80 for Yeliz. These results show that the model can generate personalized predictions based on each user’s previous ratings. However, some predictions are very close because the movies have high similarity scores. The small dataset may also limit the differences between recommendations.

4.9 Evaluate the Recommendation Model

I will split the available ratings into training and test datasets. I will use approximately 80% of the ratings for training and 20% for testing. The training data will be used to build the recommendation model, while the test data will be used to evaluate its performance. This will help me measure how accurately the model predicts ratings that were not used during training.

set.seed(123)

available_ratings <- movie_ratings %>%
  filter(!is.na(rating))

test_indices <- sample(
  seq_len(nrow(available_ratings)),
  size = round(0.2 * nrow(available_ratings))
)

test_data <- available_ratings[test_indices, ]

train_data <- available_ratings[-test_indices, ]

cat("Training ratings:", nrow(train_data), "\n")
Training ratings: 37 
cat("Test ratings:", nrow(test_data), "\n")
Test ratings: 9 

The dataset contains 46 available ratings. After splitting the data, 37 ratings were used for training, and 9 ratings were used for testing. The training data will be used to build the recommendation model, and the test data will be used to compare predicted ratings with actual ratings. Since the test dataset contains only 9 ratings, the evaluation results may be affected by the small sample size.

4.10 Create the Training Rating Matrix

I will create a new user-movie rating matrix using only the training data. I will use the pivot_wider function to organize the ratings and convert the table into a numeric matrix. The test ratings will not be included in this matrix because I want to evaluate the model using unseen data. Missing ratings will remain as NA.

train_matrix <- train_data %>%
  select(name, title, rating) %>%
  pivot_wider(
    names_from = title,
    values_from = rating
  ) %>%
  right_join(
    rating_matrix %>% select(name),
    by = "name"
  ) %>%
  select(name, all_of(colnames(movie_matrix)))

train_movie_matrix <- train_matrix %>%
  select(-name) %>%
  as.matrix()

rownames(train_movie_matrix) <- train_matrix$name

train_movie_matrix
       Inception Interstellar Barbie Oppenheimer Parasite Inside Out
Deniz          5           NA      3           5        4          3
Koray          5            5      4          NA        3         NA
Serpil         5            5     NA           5       NA          3
Azra           4            5      4           4       NA         NA
Derya          5           NA      2          NA       NA          5
Can            5            5     NA          NA        4          4
Yeliz          5            5      5           4        5         NA
Okan          NA            5     NA          NA        5         NA
Pelin         NA            5      3           4        4         NA
Erkan         NA           NA     NA           4        5         NA

The training matrix contains 10 users and 6 movies, with 37 available ratings. The 9 ratings selected for testing are now shown as NA in the training matrix. For example, Can’s rating for Oppenheimer and Derya’s rating for Interstellar are not included in the training data because they were selected for testing. This matrix will be used to calculate movie similarities without using the test ratings.

4.11 Calculate Training Movie Similarities

I will calculate movie similarities again using only the training data. I will use the cosine_similarity function created earlier to compare the movies. The test ratings will not be included in these calculations. This will help me evaluate the model using data it has not seen before.

train_similarity <- outer(
  seq_len(ncol(train_movie_matrix)),
  seq_len(ncol(train_movie_matrix)),
  Vectorize(function(i, j) {
    cosine_similarity(
      train_movie_matrix[, i],
      train_movie_matrix[, j]
    )
  })
)

rownames(train_similarity) <- colnames(train_movie_matrix)
colnames(train_similarity) <- colnames(train_movie_matrix)

round(train_similarity, 3)
             Inception Interstellar Barbie Oppenheimer Parasite Inside Out
Inception        1.000        0.997  0.954       0.996    0.985      0.976
Interstellar     0.997        1.000  0.985       0.995    0.984      0.990
Barbie           0.954        0.985  1.000       0.960    0.978      0.904
Oppenheimer      0.996        0.995  0.960       1.000    0.982      1.000
Parasite         0.985        0.984  0.978       0.982    1.000      0.990
Inside Out       0.976        0.990  0.904       1.000    0.990      1.000

The similarity scores are generally high. Inception and Interstellar have a similarity score of 0.997, while Oppenheimer and Inside Out have a similarity score of 1.000. Barbie and Inside Out have the lowest similarity score of 0.904. Some similarity scores are different from the previous similarity matrix because the test ratings were removed. However, the dataset is small, and some movie pairs have only a few ratings in common. Therefore, high similarity scores may not always mean that the movies have strong rating patterns.

4.12 Predict Ratings for Test Data

I will create a new function to predict ratings for the test data. The function will use each user’s available training ratings and the movie similarities calculated from the training data. It will calculate a weighted average to predict the rating of a selected movie. The test ratings will not be used in the prediction. If the user or movie is not found, or if there are no valid ratings for the prediction, the function will return NA.

predict_test_rating <- function(user_name, movie_title) {
  
  user_index <- match(user_name, rownames(train_movie_matrix))
  movie_index <- match(movie_title, colnames(train_movie_matrix))
  
  if (is.na(user_index) || is.na(movie_index)) {
    return(NA_real_)
  }
  
  user_ratings <- train_movie_matrix[user_index, ]
  similarities <- train_similarity[movie_index, ]
  
  valid <- !is.na(user_ratings) &
    !is.na(similarities) &
    seq_along(user_ratings) != movie_index
  
  if (!any(valid)) {
    return(NA_real_)
  }
  
  sum(user_ratings[valid] * similarities[valid]) /
    sum(abs(similarities[valid]))
}

4.13 Generate Predictions for Test Data

I will use the predict_test_rating function to predict the ratings in the test dataset. Then, I will create a table showing the actual ratings and predicted ratings for each user and movie. This will help me compare the predictions with the actual ratings and understand how well the model performs.

test_predictions <- test_data %>%
  mutate(
    predicted_rating = map2_dbl(
      name,
      title,
      predict_test_rating
    )
  ) %>%
  select(name, title, rating, predicted_rating)

test_predictions
# A tibble: 9 × 4
  name   title        rating predicted_rating
  <chr>  <chr>         <dbl>            <dbl>
1 Can    Oppenheimer       4             4.50
2 Erkan  Interstellar      5             4.50
3 Derya  Interstellar      5             4.01
4 Okan   Inception         4             5   
5 Okan   Inside Out        4             5   
6 Serpil Parasite          5             4.50
7 Azra   Inside Out        3             4.26
8 Koray  Oppenheimer       4             4.26
9 Okan   Oppenheimer       5             5   

The model generated predictions for all 9 ratings in the test dataset. Some predicted ratings were close to the actual ratings, while others had larger differences. For example, the model predicted a rating of 4.50 for Can’s Oppenheimer rating, while the actual rating was 4. For Okan’s Oppenheimer rating, both the predicted and actual ratings were 5. However, the model predicted 4.26 for Azra’s Inside Out rating, while the actual rating was 3. This shows that the model does not always predict ratings accurately. In the next section, I will calculate RMSE and MAE to measure the overall prediction errors.

4.14 Evaluate Model Performance (RMSE and MAE)

I will evaluate the model using Root Mean Squared Error (RMSE) and Mean Absolute Error (MAE). These metrics measure the differences between actual and predicted ratings. MAE measures the average absolute error, while RMSE gives more weight to larger errors. Lower values indicate better predictions.

model_evaluation <- test_predictions %>%
  summarise(
    RMSE = sqrt(mean((rating - predicted_rating)^2)),
    MAE = mean(abs(rating - predicted_rating))
  )

model_evaluation
# A tibble: 1 × 2
   RMSE   MAE
  <dbl> <dbl>
1 0.774 0.668

The model achieved an RMSE of 0.774 and an MAE of 0.668 on the test dataset. The MAE shows that the predicted ratings differ from the actual ratings by about 0.67 points on average. The RMSE is slightly higher because it gives more weight to larger errors. These results show that the model can predict movie ratings, but some predictions have larger errors. However, the evaluation has some limitations. The dataset contains only 10 users and 6 movies, and only 9 ratings were used for testing. Also, the evaluation uses only one train-test split. Therefore, the results may change if a different split is used.

5. Conclusion

In this assignment, I built a personalized movie recommendation system using Item-Based Collaborative Filtering and Cosine Similarity.

The dataset contained ratings from 10 users for 6 movies. The model used each user’s previous ratings and movie similarities to predict missing ratings. It generated 14 personalized predictions for movies that users had not rated.

I evaluated the model using an 80/20 train-test split. The RMSE was 0.774, and the MAE was 0.668. These results show that the model can predict movie ratings, but some predictions were not close to the actual ratings.

One limitation is the small dataset. Also, most movies had high similarity scores, which may have made the predictions very similar. The evaluation was based on only 9 test ratings, so the results may not be reliable for other datasets.

In the future, I could improve the recommendation system by using more users, more movies, and other recommendation algorithms.

6. Citation

I used ChatGPT (GPT-6), developed by OpenAI, to help me understand some of the functions I used in this assignment and to check my work.

OpenAI. (2026). ChatGPT (GPT-6) [Large language model]. [https://chatgpt.com/ Accessed November 28, 2026].