pyspark實現ALS矩陣分解演算法

2021-09-26 02:55:03 字數 4669 閱讀 3497

準備工作

推薦演算法例項

開啟jupyter notebook,新建乙個python3 notebook:

import os

import sys

# 動態載入pyspark目錄

spark_home = os.environ.get(

'spark_home'

)sys.path.insert(

0,os.path.join(spark_home,

'python'))

sys.path.insert(

0,os.path.join(spark_home,

'python/lib/py4j-0.10.7-src.zip'))

# exec(open(os.path.join(spark_home, 'python/pyspark/shell.py')).read())

# 建立例項

from pyspark import sparkcontext

sc = sparkcontext(

'local'

,'als'

)# 讀取u.data

# 預設是從hdfs上讀取檔案,因此加上file:,表示本地路徑

user_data = sc.textfile(

'file:/usr/local/test/u.data'

)user_data.first(

)'196\t242\t3\t881250949'

# 4項對應user_id、item_id 、rating和timestamp,以\t作為分隔符

# 時間戳用不上,擷取前三項

rate = user_data.

map(

lambda x: x.split(

"\t")[

0:3]

)rate.first()[

'196'

,'242'

,'3'

]# 轉換成rating格式

from pyspark.mllib.recommendation import rating

rate_data = rate.

map(

lambda x: rating(

int(x[0]

),int(x[1]

),int(x[2]

)))rate_data.first(

)rating(user=

196, product=

242, rating=

3.0)

# 模型訓練

from pyspark.mllib.recommendation import als

# checkpoint機制

sc.setcheckpointdir(

'checkpoint/'

)als.checkpointinterval =

2# 設定矩陣的分解維度為20,最大迭代次數為5,正則化係數為0.02

model = als.train(ratings=rate_data, rank=

20, iterations=

5, lambda_=

0.02

)# **使用者666都電影666的評分

model.predict(

666,

666)

1.6793250024079442

# **使用者666最喜歡的前十部電影

model.recommendproducts(

666,10)

[rating(user=

666, product=

1131

, rating=

5.103325669008839),

rating(user=

666, product=

262, rating=

5.037359302350613),

rating(user=

666, product=

242, rating=

5.029183432693649),

rating(user=

666, product=

302, rating=

4.951169172263912),

rating(user=

666, product=

874, rating=

4.930831851890693),

rating(user=

666, product=

268, rating=

4.918230254487493),

rating(user=

666, product=

246, rating=

4.886179007705294),

rating(user=

666, product=

1449

, rating=

4.8740748227569455),

rating(user=

666, product=

900, rating=

4.870815539606349),

rating(user=

666, product=

269, rating=

4.870557667136832)]

# **每個使用者最喜歡的三部電影

model.recommendproductsforusers(3)

.collect()[

(451

,(rating(user=

451, product=

350, rating=

5.669488103740991),

rating(user=

451, product=

1294

, rating=

5.5195576378824915),

rating(user=

451, product=

260, rating=

5.225247353937471))

),(454

,(rating(user=

454, product=

1129

, rating=

4.471292220516824),

rating(user=

454, product=

512, rating=

4.423658991387322),

rating(user=

454, product=

102, rating=

4.397846063233699))

),(147

,(rating(user=

147, product=

344, rating=

6.150454318304409),

rating(user=

147, product=

1038

, rating=

5.899384353951334),

rating(user=

147, product=

533, rating=

5.643529079524351))

),..

.]# **每部電影給分最高的三個使用者

model.recommendusersforproducts(3)

.collect()[

(1084

,(rating(user=

341, product=

1084

, rating=

6.714810319516666),

rating(user=

675, product=

1084

, rating=

6.4562103138149896),

rating(user=

511, product=

1084

, rating=

6.283989191188922))

),(1410

,(rating(user=

93, product=

1410

, rating=

5.288591420885184),

rating(user=

487, product=

1410

, rating=

4.858704979801361),

rating(user=

493, product=

1410

, rating=

4.5118411059921755))

),(667

,(rating(user=

614, product=

667, rating=

5.417582096167826),

rating(user=

762, product=

667, rating=

5.1406747758074065),

rating(user=

475, product=

667, rating=

4.989878412498716))

),..

.]

參考資料

用spark學習矩陣分解推薦演算法

推薦系統ALS矩陣分解

思想類似線性回歸做 大致如下 定義乙個 模型 數學公式 然後確定乙個損失函式,將已有資料作為訓練集,不斷迭代來最小化損失函式的值,最終確定引數,把引數套到 模型中做 矩陣分解的 模型是 損失函式是 我們就是要最小化損失函式,從而求得引數q和p。矩陣分解模型的物理意義 我們希望學習到乙個p代表user...

混淆矩陣(Confusion Matrix)分析

content confusionmatrix example talbe ofconfusion preference confusion matrix 在機器學習領域,混淆矩陣 confusion matrix 又稱為可能性 或是錯誤矩陣。它是一種特定的矩陣用來呈現演算法效能的視覺化效果,通常是...

混淆矩陣(Confusion Matrix)分析

content confusionmatrix example talbe ofconfusion preference confusion matrix 在機器學習領域,混淆矩陣 confusion matrix 又稱為可能性 或是錯誤矩陣。它是一種特定的矩陣用來呈現演算法效能的視覺化效果,通常是...