This article shows how to implement product recommendations using the collaborative filtering algorithm.
Background information
You can use data mining techniques like collaborative filtering to discover similar hidden relationships between products and increase sales.
Collaborative filtering is an algorithm based on association rules. For example, in a shopping scenario, if User A and User B have both purchased Item A and Item B, we can assume they have similar tastes. If User A then buys Item C but User B has not, we can recommend Item C to User B. This is a classic example of user-based collaborative filtering, where recommendations are based on user similarity.
This pipeline first analyzes user shopping behavior before July to discover product associations. It then uses these associations to recommend products to users in July or later and evaluate the results. For example, if a user bought Item A before July, and Item A is strongly associated with Item B, the pipeline recommends Item B to that user and checks if the recommendation is a hit.
The data in this pipeline is fictional and for demonstration purposes only.
Note the following about this pipeline:
-
This article demonstrates the use of collaborative filtering for a specific business scenario. It does not address key factors for shopping behavior recommendations, such as time series data.
-
This pipeline considers only the association between products, not the attributes of the recommended products. For example, a mobile phone is a low-frequency purchase. If a user buys a phone in June, they are unlikely to buy another one in July. This factor is not considered.
-
We recommend using association-rule-based recommendations as a supplementary method within a recommendation system. To improve recommendation accuracy, train a model using machine learning algorithms.
Dataset
The dataset for this pipeline is provided by the Tianchi Big Data Competition. It is divided into two parts based on time: purchase behavior before July, and purchase behavior in July or later. The following table describes the fields.
|
Parameter |
Description |
Type |
Description |
|
user_id |
User ID |
STRING |
The ID of the user who performed the action. |
|
item_id |
Item ID |
STRING |
The ID of the item involved in the action. |
|
active_type |
Behavior type |
STRING |
|
|
active_date |
Behavior time |
STRING |
The time when the behavior occurred. |
The following is a sample of the raw data. In this dataset, a single user_id, such as 10944750, can have multiple records with different item_id values. The active_type can be a value from 0 to 3, and the active_date is in a "Month Day" format (for example, May 2 or June 7).
Procedure
-
Go to the Machine Learning Designer page.
-
Log on to the PAI console.
-
In the left-side navigation pane, click Workspaces. On the Workspaces page, click the name of the workspace that you want to manage.
-
In the left-side navigation pane, choose .
-
-
Create the pipeline.
-
On the Designer page, click the Preset Templates tab.
-
In the Recommendation Algorithm - Product Recommendation section, click Create.
-
In the Create Pipeline dialog box, configure the parameters. You can use the default settings for all parameters.
The Data Storage parameter specifies an OSS Bucket path to store temporary data and models generated during the pipeline run.
-
Click Confirm.
It takes about 10 seconds to create the pipeline.
-
In the pipeline list, find the "Recommendation Algorithm - Product Recommendation" pipeline and click Open.
-
The system automatically builds the pipeline on the canvas based on the preset template, as shown in the following figure.

Area
Description
①
Generates a recommendation list based on association rules. This section uses shopping behavior data from before July as the data source. First, an SQL script retrieves user purchase data. Then, the etrec component calculates the most similar items for each item to identify which products users might purchase together.
After the pipeline runs, you can right-click the etrec component and choose to view the product associations. The output fields are as follows:
-
itemid: The target item.
-
similarity: Contains a colon-separated value. The number to the left of the colon (:) is the ID of a related item, and the number to the right is the association score.
②
Processes the actual shopping behavior from July onwards. This pipeline uses a simple recommendation rule: if a user purchased Item A before July and Item A is strongly associated with Item B, Item B is recommended to that user.
③
Calculates the number of recommendations and hits. The Stat Summary-1 component shows the recommendation list generated from the pre-July behavior, and the Stat Summary-2 component shows the successful recommendations (hits).
-
-
-
Run the pipeline and view the results.
-
Click the Run icon (
) at the top of the canvas. -
After the pipeline finishes running, right-click the Stat Summary-1 component on the canvas and choose to view the generated recommendation list.
-
Right-click the Stat Summary-2 component on the canvas and choose View Data > Whole Table Statistics Output to view the successful recommendations.
For more information about the fields in the Stat Summary component, see Stat Summary.
-