【DSW Gallery】Alternating Least Squares Algorithm to Solve Recommendation Related Problems

Introduction: ALS (Alternating Lease Square) Alternating Least Squares is a model-based collaborative filtering algorithm, which is used to factorize the scoring matrix and then predict the user's score on the item. It infers the preferences of each user and recommends suitable products to users by observing the ratings given by all users to products.

Use directly

Please open Alternating Least Squares Algorithm to solve recommendation-related problems, and click "Open in DSW" in the upper right corner.

ALS Movie Recommendations

1: ALS (Alternating Lease Square) Alternating Least Squares is a model-based collaborative filtering algorithm, which is used to factorize the scoring matrix and then predict the user's score on the item. It infers the preferences of each user and recommends suitable products to users by observing the ratings given by all users to products.
2: This article will introduce how to use Alink's ALS algorithm to recommend movies in pai-dsw.
Operating environment requirements

1. PyAlink has been installed by default in the official image of PAI-DSW, and the memory requirement is 4G and above.
2. The content of this Notebook can be run and viewed directly without any other files.
from pyalink.alink import *
useLocalEnv(2) # set parallelism to 2
Use one of the following commands to start using PyAlink:
- useLocalEnv(parallelism, flinkHome=None, config=None): run PyAlink scripts locally.
- useRemoteEnv(host, port, parallelism, flinkHome=None, localIp="localhost", config=None): run PyAlink scripts on a Flink cluster.
- getMLEnv(): run PyAlink scripts as PyFlink scripts, support 'flink run -py xxx.py'.
Call resetEnv() to reset environment and switch to another.
JVM listening on 127.0.0.1:58422

Scale to Larger Data
In this example, we use useLocalEnv to run the Alink job locally (that is, in the DSW container), and use multi-threading to simulate distributed computing.
For larger-scale data, use usePAIEnv to submit jobs to large-scale clusters, and use help(usePAIEnv) to view the detailed usage.
prepare data
Movielens data source: http://files.grouplens.org/datasets/movielens/ml-100k/
The Als algorithm requires three columns of input data (userId, itemId, ratings), that is, the user ID, product ID, and the user's rating information for the product.

Train the ALS model

Algorithm related documents: https://www.yuque.com/pinshu/alink_doc/alstrainbatchop
The Als algorithm obtains the vector representation corresponding to each userId and itemId by decomposing the input triplet information into a matrix.

Scoring using the ALS model
Algorithm related documents: https://www.yuque.com/pinshu/alink_doc/alsraterecommender
Use the Als model to score a given (userId, itemId) pair, which is usually used to judge how much a user likes a certain movie.

Recommending Movies to Users Using an ALS Model
Algorithm related documents: https://www.yuque.com/pinshu/alink_doc/fmitemsperuserrecommender
Recommend related itemId list for a given userId. It is usually used to recommend several movies that a user may like.

Find similar movies to a movie using the ALS model
Algorithm related documents: https://www.yuque.com/pinshu/alink_doc/alssimilaritemsrecommender
Recommend a related itemId list for a given itemId. It is usually used to recommend several movies that he may like for a user who has watched a certain movie.

Recommend similar users to users using ALS model
Algorithm related documents: https://www.yuque.com/pinshu/alink_doc/alssimilar
Recommend a related userId list for a given userId. It is usually used to recommend user groups with similar interests to the user, which is convenient for everyone to communicate.

Recommend potential users for movies using an ALS model
Algorithm related documents: https://www.yuque.com/pinshu/alink_doc/alsusersperitemrecommender
Recommend a related itemId list for a given itemId. It is usually used to recommend possible movie viewers for a certain movie, and is used for new movie promotion recommendations.

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00

phone Contact Us