The core of this recommender is a content-based filtering approach. It works as follows:
-
Data Loading: A movie dataset is loaded from a CSV file.
-
Feature Selection: Key features (Title, Genre, Director, Actors, Plot) are selected for the recommendation process.
-
Data Preprocessing:
-
The Actors column is cleaned to include only the first three names, with commas removed.
-
All selected features are combined into a single "bag of words" for each movie.
-
Missing data (NaN values) are handled by replacing them with empty strings.
-
-
Vectorization: CountVectorizer is used to transform the combined text data into a matrix of token counts. This process creates a numerical representation of the movie features.
-
Similarity Calculation: Cosine similarity is calculated between all movies to determine how alike they are. This metric provides a score between 0 and 1, where 1 indicates perfect similarity.
-
Recommendation: The system takes a movie title as input, finds its index, and then returns the movies with the highest similarity scores.
-
Python 3.x
-
pandas
-
numpy
-
scikit-learn
You can install the required libraries using pip:
pip install pandas numpy scikit-learn
-
Save the code as a Python file (e.g., recommender.py).
-
Run the script from your terminal:
python recommender.py
- The script will load the data, preprocess it, and compute the similarity matrix. You can then use the recommendation_system() function to get recommendations for a given movie title.
Here are some key parts of the code:
import pandas as pd
from sklearn.metrics.pairwise import cosine_similarity
from sklearn.feature_extraction.text import CountVectorizer
Load the dataset
df = pd.read_csv('https://query.data.world/s/uikepcpffyo2nhig52xxeevdialfl7')
Select relevant features
df = df[['Title', 'Genre', 'Director', 'Actors', 'Plot']]
Clean up actor names and handle missing values
df['Actors'] = df['Actors'].map(lambda x: x.split(',')[:3] if isinstance(x, str) else [''])
df['Genre'] = df['Genre'].map(lambda x: x.split(',') if isinstance(x, str) else [''])
df['Director'] = df['Director'].map(lambda x: [x] if isinstance(x, str) else [''])
df['Plot'] = df['Plot'].fillna('')
def create_soup(x): return ' '.join(x['Genre']) + ' ' + ' '.join(x['Director']) + ' ' + ' '.join(x['Actors']) + ' ' + x['Plot']
df['soup'] = df.apply(create_soup, axis=1)
Create a CountVectorizer object and fit the data
count = CountVectorizer(stop_words='english')
count_matrix = count.fit_transform(df['soup'])
Compute the cosine similarity matrix
cosine_sim = cosine_similarity(count_matrix, count_matrix)
def recommendation_system(movie_title):
Find the index of the movie
movie_index = df[df['Title'] == movie_title].index[0]
Get a list of similarity scores for that movie
similarity_scores = list(enumerate(cosine_sim[movie_index]))
Sort the scores in descending order
sorted_scores = sorted(similarity_scores, key=lambda x: x[1], reverse=True)
Get the top 10 movies (excluding itself)
top_10_movies = sorted_scores[1:11]
Print the recommendations
print(f"Recommendations for {movie_title}:")
for i, (index, score) in enumerate(top_10_movies):
print(f"{i+1}. {df.iloc[index]['Title']} (Score: {score:.4f})")
recommendation_system('The Dark Knight')