Understanding Basic Recommendation Systems using Python
We always tend to imagine e-commerce when we talk about recommendation systems but its more than that, the main idea behind is to make an effective decision -making by pairing two or more things together. I tried to understand how the system works by collecting some public data and build a basic recommendation system.
The goal of a recommender system is to make recommendations to a consumer about a product or services. Their purpose is to identify a relevant item/s a consumer might have purchased or viewed and make suggestions. Let’s start building the system by collecting data which is the first ingredient.
DATA
To make sure the system doesn’t get too much complicated, I will be building a simple content-based filtering system which is sort of if you like this item you may also like this. There is another type of system which is collaborative filtering which can become complicated and requires a lot of user generated data but they are the best recommendation system out there. For the basic understanding, I am using shopping item datasets which is a simple example of content based filtering.
When a user interacts with the item on e-commerce website he may view too many items and sometime one item may get many get too many views and this creates a sparse dataset for the recommendation system (basic ones) and makes it imbalance and to prevent this we filtered such values from the datasets and clean it. We then split the datasets in training and testing.
CODING
When I started building the recommender, I started thinking about different ways to solve the item pairing problem. There are two approaches, the first approach predicts an actual rating for a user-item pair and recommends the highest rated item for that user. The second approach predicts a ranking of items and recommends the highest ranked item for a user. The first approach is preferred (for basic understanding only) because it is evaluated using standard metrics AUC (Area Under Curve) and RMSE (Root Mean Squared Error).
[ Metrics only for explanation and not used for building my recommender. AUC computes the area under the ROC (Receiver Operating Characteristic) curve for classification problems, and a larger the number better the recommender. The ROC curve plots the true positive rate against the false positive rate. We want a classifier that correctly identifies as many positive instances as are available, with a very low percentage of negative instances incorrectly classified as positive. The ROC curve of a perfect classifier will go straight up the y-axis (true positive rate) and then along the x-axis (false positive rate). A classifier with no power will sit on the x=y diagonal. RMSE computes how far the predicted value is from the actual value. ]
Implementation
Before implementation, there are few terms you need to understand in basic content-based recommender. TF- Term Frequency and IDF- Inverse Document Frequency. TF- Frequency of words or items in a document/ page. TF-IDF is used with a log to mainly negate the effect of repetitive words or items that make the recommender imbalanced and parse. As I mentioned previously, we can remove such data. The data I used is a sample dataset of outdoor clothing and products from Patagonia. The data looks like this and you can download it here.
Here the datasets contain two columns id-description and we are going to use the concept of TF-IDF with NLP (Natural Language Processing) to parse through the descriptions, identify distinct phrases in each item’s description, and then find ‘recommended’ products based on those phrases. You can learn more about TD-IF here. Here we are going to use the weighted multiplication of TD-IDF and then use that score to recommend similar item.
Get Deep Vaghani’s stories in your inbox
Join Medium for free to get updates from this writer.
Let’s start coding by importing required modules and uploading datasets
Now, we will use TfidVectorizer to extract our feature. Learn more about TfidVectorizer here.
To compute the prediction we will use cosine similarities matrix to generate a score which will then be used to get the recommended item. It is equal to the dot product of the two vectors divided by the product of the length of both the vectors.
The final part of the code is to pick an item in the dataset and the engine will fetch similar items based on the score and recommend the top number of items the user wants to be recommended.
The output looks like this.
CONCLUSION
Here we can see data makes all the difference. If data is poor system will be weak and because of the sparsity problem in data the basic recommendation system does not work well. To make the better system we need a dense matrix so the system can catch all the signals about the item/product.
So the question is how well these traditional systems work in real life? Well, the system which is built on simple matrix and neighborhood approaches and trained on basic user interaction is not going to provide great recommendations. Nowadays with the availability of more computing power and better machine understanding, we can train the systems with complex data and they work better on new datasets and this will keep making recommenders better and better for good.
Cheers!







