当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python pyspark FMRegressor用法及代码示例


本文简要介绍 pyspark.ml.regression.FMRegressor 的用法。

用法:

class pyspark.ml.regression.FMRegressor(*, featuresCol='features', labelCol='label', predictionCol='prediction', factorSize=8, fitIntercept=True, fitLinear=True, regParam=0.0, miniBatchFraction=1.0, initStd=0.01, maxIter=100, stepSize=1.0, tol=1e-06, solver='adamW', seed=None)

用于回归的分解机器学习算法。

求解器支持:

  • gd(正常的小批量梯度下降)

  • adamW(默认)

3.0.0 版中的新函数。

例子

>>> from pyspark.ml.linalg import Vectors
>>> from pyspark.ml.regression import FMRegressor
>>> df = spark.createDataFrame([
...     (2.0, Vectors.dense(2.0)),
...     (1.0, Vectors.dense(1.0)),
...     (0.0, Vectors.sparse(1, [], []))], ["label", "features"])
>>>
>>> fm = FMRegressor(factorSize=2)
>>> fm.setSeed(16)
FMRegressor...
>>> model = fm.fit(df)
>>> model.getMaxIter()
100
>>> test0 = spark.createDataFrame([
...     (Vectors.dense(-2.0),),
...     (Vectors.dense(0.5),),
...     (Vectors.dense(1.0),),
...     (Vectors.dense(4.0),)], ["features"])
>>> model.transform(test0).show(10, False)
+--------+-------------------+
|features|prediction         |
+--------+-------------------+
|[-2.0]  |-1.9989237712341565|
|[0.5]   |0.4956682219523814 |
|[1.0]   |0.994586620589689  |
|[4.0]   |3.9880970124135344 |
+--------+-------------------+
...
>>> model.intercept
-0.0032501766849261557
>>> model.linear
DenseVector([0.9978])
>>> model.factors
DenseMatrix(1, 2, [0.0173, 0.0021], 1)
>>> model_path = temp_path + "/fm_model"
>>> model.save(model_path)
>>> model2 = FMRegressionModel.load(model_path)
>>> model2.intercept
-0.0032501766849261557
>>> model2.linear
DenseVector([0.9978])
>>> model2.factors
DenseMatrix(1, 2, [0.0173, 0.0021], 1)
>>> model.transform(test0).take(1) == model2.transform(test0).take(1)
True

相关用法


注:本文由纯净天空筛选整理自spark.apache.org大神的英文原创作品 pyspark.ml.regression.FMRegressor。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。