๐ Book Recommendation Engine using K-Nearest Neighbors
A machine learning-based book recommendation system that uses collaborative filtering and K-Nearest Neighbors (KNN) algorithm to suggest similar books based on user ratings.
Quick Overview
Problem: Recommend relevant books using large-scale, sparse user rating data, where traditional rule-based methods fail to capture user preference patterns.
Solution: Built a collaborative filtering recommendation engine using K-Nearest Neighbors with cosine distance, leveraging a userโbook rating matrix and sparse representations to identify similar books based on shared rating behavior.
Impact: Successfully generated meaningful book recommendations with similarity scores using 1.1 million ratings, demonstrating applied knowledge of recommender systems, distance-based learning, and data preprocessing for real-world scale datasets.
Completed Project:
๐ง Technologies Used
Python 3.x
NumPy - Numerical computations
Pandas - Data manipulation and analysis
Scikit-learn - Machine learning (KNN algorithm)
SciPy - Sparse matrix operations
Matplotlib - Data visualization (optional)
๐ Dataset
Book-Crossings Dataset:
1.1 million ratings (scale 1-10)
270,000 books
90,000 users
Source: The dataset is automatically downloaded in the notebook from FreeCodeCamp.
๐ How It Works
1. Data Preprocessing
Load book and rating data from CSV files
Filter out sparse data:
Remove users with fewer than 200 ratings
Remove books with fewer than 100 ratings
This ensures statistical significance in recommendations
2. Create User-Book Matrix
3. Train KNN Model
Uses cosine distance metric to measure similarity
Finds the 5 nearest neighbors (most similar books)
Algorithm: Brute force (most accurate for high-dimensional data)
4. Generate Recommendations
The system compares rating patterns (book "fingerprints") to find similar books.
๐ป Usage
Basic Function Call
Expected Output
Output Format:
First element: Input book title
Second element: List of 5 recommended books with their distances
Lower distance = More similar books
Distance ranges from 0 (identical) to 1 (completely different)
๐งฎ Algorithm Details
K-Nearest Neighbors (KNN)
Algorithm Type: Lazy learning (instance-based)
Distance Metric: Cosine distance
K Value: 6 (returns 6 neighbors, skip first as it's the input book itself)
Search Method: Brute force
Why Cosine Distance?
Cosine distance measures the angle between rating vectors, making it ideal for comparing user preferences regardless of rating scale differences.
๐ Key Features
โ
Collaborative filtering based on user ratings
โ
Handles sparse data efficiently using sparse matrices
โ
Statistical significance through data filtering
โ
Fast recommendations using optimized KNN
โ
Returns books with similarity scores
๐ Understanding the Results
Distance Interpretation:
0.0 - 0.3: Very similar books0.3 - 0.6: Moderately similar books0.6 - 0.8: Somewhat similar books0.8 - 1.0: Different booksLower distances indicate stronger recommendations!
๐ Code Structure
๐งช Testing
The notebook includes a test function that validates:
Correct book title returned
5 recommendations provided
Recommended books match expected titles
Distance values within acceptable range (ยฑ0.05)
๐ Example Recommendations
Input: "The Queen of the Damned (Vampire Chronicles (Paperback))"
Output:
Catch 22 (0.79)
The Witching Hour (0.74)
Interview with the Vampire (0.73)
The Tale of the Body Thief (0.54)
The Vampire Lestat (0.52)
The system successfully identifies other books in the Vampire Chronicles series and similar fiction!
๐ Learning Outcomes
This project demonstrates:
Collaborative Filtering: Recommending items based on similar user preferences
Dimensionality Reduction: Filtering sparse data for better performance
Distance Metrics: Using cosine similarity for recommendation systems
Data Preprocessing: Handling real-world messy data
Matrix Operations: Working with sparse matrices efficiently
๐ Resources
๐ค Author
Created as part of the FreeCodeCamp Machine Learning with Python certification.
๐ License
This project is open source and available for educational purposes.
Note: This is a learning project demonstrating collaborative filtering and KNN algorithms for recommendation systems. For production use, consider additional optimizations and error handling.
1 like ยท 14 views ยท Dec 27, 2025
Developed a K-Nearest Neighbors book recommendation engine using user ratings.