基于余弦相似度的项目项推荐

问题描述 投票:0回答:1

作为我正在构建的推荐系统的一部分,我想基于余弦相似性实现项目项推荐。理想情况下,我想计算由2048个特征的DenseVector表示的100万个项目的余弦相似度,以便将前n个最相似的项目放到给定的项目中。

我的问题是,我遇到的解决方案在我的数据集上表现不佳。

我试过了 :

这是使用columnSimilarities()的解决方案

import pyspark
from pyspark.sql import SparkSession
from pyspark.ml.feature import PCA
from pyspark.mllib.linalg.distributed import IndexedRow, IndexedRowMatrix
from pyspark.sql.functions import row_number

new_df = url_rdd.zip(vector_rdd.map(lambda x:Vectors.dense(x))).toDF(schema=['url','features'])

# PCA
pca = PCA(k=1024, inputCol="features", outputCol="pca_features")
pca_model = pca.fit(new_df)
pca_df = pca_model.transform(new_df)

# Indexing my dataframe
pca_df.createOrReplaceTempView('pca_df')
indexed_df = spark.sql('select row_number() over (order by url) - 1 as id, * from pca_df')

# Computing Cosine Similarity
mat = IndexedRowMatrix(indexed_df.select("id", "pca_features").rdd.map(lambda row: IndexedRow(row.id, row.pca_features.toArray()))).toBlockMatrix().transpose().toIndexedRowMatrix()
cos_mat = mat.columnSimilarities()

在pyspark上有更好的解决方案来计算余弦相似度并获得前n个最相似的项目吗?

python apache-spark pyspark cosine-similarity recommender-systems
1个回答
2
投票

考虑缓存new_df,因为你要至少两次(一次适合模型,另一次转换数据)。

另外,不要忘记可以传递给columnSimilarities方法的可选阈值。

© www.soinside.com 2019 - 2024. All rights reserved.