我是 Spark 新手。我想制作一个稀疏矩阵,专门用于推荐引擎的用户 ID 项目 ID 矩阵。我知道如何在 python 中做到这一点。如何在 PySpark 中做到这一点?这是我在矩阵中的做法。桌子现在看起来像这样。
Session ID| Item ID | Rating
1 2 1
1 3 5
import numpy as np
data=df[['session_id','item_id','rating']].values
data
rows, row_pos = np.unique(data[:, 0], return_inverse=True)
cols, col_pos = np.unique(data[:, 1], return_inverse=True)
pivot_table = np.zeros((len(rows), len(cols)), dtype=data.dtype)
pivot_table[row_pos, col_pos] = data[:, 2]
像那样:
from pyspark.mllib.linalg.distributed import CoordinateMatrix, MatrixEntry
# Create an RDD of (row, col, value) triples
coordinates = sc.parallelize([(1, 2, 1), (1, 3, 5)])
matrix = CoordinateMatrix(coordinates.map(lambda coords: MatrixEntry(*coords)))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)