本文简要介绍
pyspark.mllib.recommendation.MatrixFactorizationModel
的用法。用法:
class pyspark.mllib.recommendation.MatrixFactorizationModel(java_model)
通过正则化交替最小二乘训练的矩阵分解模型。
0.9.0 版中的新函数。
例子:
>>> r1 = (1, 1, 1.0) >>> r2 = (1, 2, 2.0) >>> r3 = (2, 1, 2.0) >>> ratings = sc.parallelize([r1, r2, r3]) >>> model = ALS.trainImplicit(ratings, 1, seed=10) >>> model.predict(2, 2) 0.4...
>>> testset = sc.parallelize([(1, 2), (1, 1)]) >>> model = ALS.train(ratings, 2, seed=0) >>> model.predictAll(testset).collect() [Rating(user=1, product=1, rating=1.0...), Rating(user=1, product=2, rating=1.9...)]
>>> model = ALS.train(ratings, 4, seed=10) >>> model.userFeatures().collect() [(1, array('d', [...])), (2, array('d', [...]))]
>>> model.recommendUsers(1, 2) [Rating(user=2, product=1, rating=1.9...), Rating(user=1, product=1, rating=1.0...)] >>> model.recommendProducts(1, 2) [Rating(user=1, product=2, rating=1.9...), Rating(user=1, product=1, rating=1.0...)] >>> model.rank 4
>>> first_user = model.userFeatures().take(1)[0] >>> latents = first_user[1] >>> len(latents) 4
>>> model.productFeatures().collect() [(1, array('d', [...])), (2, array('d', [...]))]
>>> first_product = model.productFeatures().take(1)[0] >>> latents = first_product[1] >>> len(latents) 4
>>> products_for_users = model.recommendProductsForUsers(1).collect() >>> len(products_for_users) 2 >>> products_for_users[0] (1, (Rating(user=1, product=2, rating=...),))
>>> users_for_products = model.recommendUsersForProducts(1).collect() >>> len(users_for_products) 2 >>> users_for_products[0] (1, (Rating(user=2, product=1, rating=...),))
>>> model = ALS.train(ratings, 1, nonnegative=True, seed=123456789) >>> model.predict(2, 2) 3.73...
>>> df = sqlContext.createDataFrame([Rating(1, 1, 1.0), Rating(1, 2, 2.0), Rating(2, 1, 2.0)]) >>> model = ALS.train(df, 1, nonnegative=True, seed=123456789) >>> model.predict(2, 2) 3.73...
>>> model = ALS.trainImplicit(ratings, 1, nonnegative=True, seed=123456789) >>> model.predict(2, 2) 0.4...
>>> import os, tempfile >>> path = tempfile.mkdtemp() >>> model.save(sc, path) >>> sameModel = MatrixFactorizationModel.load(sc, path) >>> sameModel.predict(2, 2) 0.4... >>> sameModel.predictAll(testset).collect() [Rating(... >>> from shutil import rmtree >>> try: ... rmtree(path) ... except OSError: ... pass
相关用法
- Python pyspark MaxAbsScaler用法及代码示例
- Python pyspark MapType用法及代码示例
- Python pyspark MultiIndex.size用法及代码示例
- Python pyspark MultiIndex.hasnans用法及代码示例
- Python pyspark MultiIndex.to_numpy用法及代码示例
- Python pyspark MultiIndex.levshape用法及代码示例
- Python pyspark MinHashLSH用法及代码示例
- Python pyspark MultiIndex.max用法及代码示例
- Python pyspark MultiIndex.drop用法及代码示例
- Python pyspark MultiIndex.min用法及代码示例
- Python pyspark MultiIndex.unique用法及代码示例
- Python pyspark MultiIndex.rename用法及代码示例
- Python pyspark MultiIndex.value_counts用法及代码示例
- Python pyspark MultiIndex.values用法及代码示例
- Python pyspark MultiIndex.difference用法及代码示例
- Python pyspark MultiIndex.sort_values用法及代码示例
- Python pyspark MLUtils.loadLibSVMFile用法及代码示例
- Python pyspark MultiIndex.spark.transform用法及代码示例
- Python pyspark MultiIndex.T用法及代码示例
- Python pyspark MultiIndex用法及代码示例
- Python pyspark MultiIndex.ndim用法及代码示例
- Python pyspark MulticlassClassificationEvaluator用法及代码示例
- Python pyspark MultiIndex.copy用法及代码示例
- Python pyspark MultiIndex.to_frame用法及代码示例
- Python pyspark MultiIndex.shape用法及代码示例
注:本文由纯净天空筛选整理自spark.apache.org大神的英文原创作品 pyspark.mllib.recommendation.MatrixFactorizationModel。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。