gpt4 book ai didi

apache-spark - GLM with Apache Spark 2.2.0 - Tweedie 系列默认链接值

转载 作者:行者123 更新时间:2023-12-04 05:05:35 24 4
gpt4 key购买 nike

我正在使用带有 python 的 spark 2.2.0。我试图弄清楚在 Tweedie 系列的情况下,Spark 在 GeneralizedLineraModel 中接受的 Link 函数的默认参数是什么。

当我查看文档时 https://spark.apache.org/docs/2.2.0/api/scala/index.html#org.apache.spark.ml.regression.GeneralizedLinearRegression

class pyspark.ml.regression.GeneralizedLinearRegression(self, labelCol="label", featuresCol="features", predictionCol="prediction", family="gaussian", link=None, fitIntercept=True, maxIter=25, tol=1e-6, regParam=0.0, weightCol=None, solver="irls", linkPredictionCol=None

似乎 family='tweedie' 时的默认值应该是 None 但是当我尝试这个时(通过使用类似的测试作为单元测试:https://github.com/apache/spark/pull/17146/files/fe1d3ae36314e385990f024bca94ab1e416476f2):

from pyspark.ml.linalg import Vectors
df = spark.createDataFrame([(1.0, Vectors.dense(0.0, 0.0)),\
(1.0, Vectors.dense(1.0, 2.0)),\
(2.0, Vectors.dense(0.0, 0.0)),\
(2.0, Vectors.dense(1.0, 1.0)),], ["label", "features"])
glr = GeneralizedLinearRegression(family="tweedie",variancePower=1.42,link=None)
model = glr.fit(df)
transformed = model.transform(df)

它引发了一个 空指针 Java 异常...

Py4JJavaError: An error occurred while calling o6739.w. : java.lang.NullPointerException ...

当我在模型初始化中删除 explicite link=None 时效果很好。

from pyspark.ml.linalg import Vectors
df = spark.createDataFrame([(1.0, Vectors.dense(0.0, 0.0)),\
(1.0, Vectors.dense(1.0, 2.0)),\
(2.0, Vectors.dense(0.0, 0.0)),\
(2.0, Vectors.dense(1.0, 1.0)),], ["label", "features"])
glr = GeneralizedLinearRegression(family="tweedie",variancePower=1.42)
model = glr.fit(df)
transformed = model.transform(df)

我希望能够传递一组标准的参数,例如

params={"family":"Onefamily","link":"OnelinkAccordingToFamily",..}

然后将 GLM 初始化为:

 glr = GeneralizedLinearRegression(family=params["family"],link=params['link]' ....)

所以它可以更标准并且适用于任何家庭和链接的情况。似乎在 family=Tweedie 的情况下链接值没有被忽略我应该使用什么默认值?我试过 link='' 或 link='None' 但它引发了“无效链接功能”。

最佳答案

要处理 GLR tweedie 系列,您需要定义通过“linkPower”参数指定的电源链接函数,并且您不应将 link 设置为 None 导致您遇到的异常。

这是一个如何使用它的例子:

df = spark.createDataFrame(
[(1.0, Vectors.dense(0.0, 0.0)),
(1.0, Vectors.dense(1.0, 2.0)),
(2.0, Vectors.dense(0.0, 0.0)),
(2.0, Vectors.dense(1.0, 1.0)), ], ["label", "features"])

# in this case the default link power applies
glr = GeneralizedLinearRegression(family="tweedie", variancePower=1.6)

model = glr.fit(df) # in this case the default link power applies

model2 = glr.setLinkPower(-1.0).fit(df)

附注:tweedie 系列中的默认链接功率是1 - variancePower

关于apache-spark - GLM with Apache Spark 2.2.0 - Tweedie 系列默认链接值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46927761/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com