photo credit: pixabay

Building A Recommender System With Implicit Feedback Datasets Using Alternating Least Squares

RecSys, ALS, Collaborative Filtering

The Data

online_retail_data.py
Figure 1
  • There are a lot of “CustomerID” were missing from the data, so we will have to remove those rows.
  • Group “CustomerID” and “StockCode” then sum the “Quantity”. So that we get each customer and each item interactions.
  • If “Quantity” = 0, we change to one.
  • Eliminate negative “Quantity”.
clean_retail_data.py
Table 1
quantity_hist.py
Figure 2
retail_count.py
Figure 3

Implicit Feedback

  • We will create numeric “customer_id and “item_id” columns.
  • Create two matrices, one for fitting the model (item-customer) and another one for recommendations (customer-item).
  • Initialize the Alternating Least Squares (ALS) recommendation model.
  • Fit the model using the sparse item-customer matrix.
  • We set the type of our matrix to double for the ALS function to run properly.
online_retail_ALS.py

Example of Recommendation — Finding the Similar Items

grouped_df.loc[grouped_df['item_id'] == 1319].head()
Table 2
  • Get the customer and item vectors from our trained model.
  • Calculate the vector norms.
  • Calculate the similarity score.
  • Get the top 10 items.
  • Create a list of item-score tuples of most similar items with this item.
similar_item.py
Figure 4

Example of Recommendation — Recommend Items to Customers

  • Get the purchase score from the sparse customer item matrix.
  • Add 1 to everything, so that items with no purchase yet become equal to 1.
  • Make items already purchased zero.
  • Get dot product of customer vector and all item vectors.
  • Scale this recommendation vector between 0 and 1.
  • Items already purchased have their recommendation multiplied by zero.
  • Sort the indices of the item into order of best recommendations.
  • Start empty list to store descriptions and scores.
  • Append descriptions and scores to the list.
  • Get the trained customer and item vectors. We convert them to csr matrices.
  • Create recommendations for customer with id 2.
RecSys_ALS_items.py
Figure 5
top20_purchase.py
Figure 6

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store