You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Azure Databricks运行Spark代码报错:LogisticRegression构造函数未被白名单

Fixing py4j.security.Py4JSecurityException for LogisticRegression in Azure Databricks

Hey there, let's break down why you're hitting this error and how to fix it quickly.

What's causing this?

The error Constructor public org.apache.spark.ml.classification.LogisticRegression(java.lang.String) is not whitelisted happens because you're trying to initialize LogisticRegression with a string argument (like a custom name/ID) directly, and Databricks' security sandbox blocks this specific constructor call. Spark MLlib's standard API doesn't recommend this initialization pattern anyway—this constructor isn't part of the public, supported interface for Python users.

The Fix: Use Spark ML's Standard Initialization Pattern

Instead of passing a string directly to the LogisticRegression constructor, use one of these supported approaches:

Option 1: No-arg constructor + setter methods

This follows Spark ML's pipeline-style API, which is the most explicit way to configure your model:

from pyspark.ml.classification import LogisticRegression

# Initialize with no arguments, then set parameters using setters
lr = LogisticRegression()
lr = lr.setLabelCol("label") \
       .setFeaturesCol("features") \
       .setRegParam(0.01)  # Add any other model parameters you need

Option 2: Keyword arguments during initialization

For simpler setups, you can pass parameters directly using keyword arguments for cleaner code:

from pyspark.ml.classification import LogisticRegression

lr = LogisticRegression(
    labelCol="label",
    featuresCol="features",
    regParam=0.01
)

Extra Checks

If you're still running into issues after adjusting the code:

  • Verify your Databricks Runtime version: Older runtimes might have stricter whitelisting—upgrading to a recent LTS version (like 13.x or higher) can resolve edge cases.
  • Double-check your import: Make sure you're using pyspark.ml.classification.LogisticRegression (not the deprecated mllib version of the class).

内容的提问来源于stack exchange,提问作者lidong

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 04:42:27