python - 如何将 xgboost 集成到 Spark 中? (Python)

标签 python apache-spark pyspark xgboost

我正在尝试使用 XGBoost 对 hive 上的数据训练模型,数据太大,我无法将其转换为 pandas df,所以我必须将 XGBoost 与 Spark df 结合使用。 创建XGBoostEstimator时,出现错误:

TypeError: 'JavaPackage' object is not callable Exception AttributeError: "'NoneType' object has no attribute '_detach'" in ignored

我没有使用 xgboost for Spark 的经验,我尝试过一些在线教程,但没有成功。 我尝试转换为pandas df,但数据太大,我总是从Java包装器中得到OutOfMemoryException(我也尝试查找它,但该解决方案对我不起作用,提高了执行程序内存)。

我关注的最新教程是:

https://towardsdatascience.com/pyspark-and-xgboost-integration-tested-on-the-kaggle-titanic-dataset-4e75a568bdb

放弃 XGBoost 模块后,我开始使用 sparkxgb

spark = create_spark_session('shai', 'dna_pipeline')
# sparkxgboost files 
spark.sparkContext.addPyFile('resources/sparkxgb.zip')

def create_spark_session(username=None, app_name="pipeline"):
    if username is not None:
        os.environ['HADOOP_USER_NAME'] = username

    return SparkSession \
        .builder \
        .master("yarn") \
        .appName(app_name) \
        .config(...) \
        .config(...) \
        .getOrCreate()

def train():
    train_df = spark.table('dna.offline_features_train_full')
    test_df = spark.table('dna.offline_features_test_full')

    from sparkxgb import XGBoostEstimator

    vectorAssembler = VectorAssembler() \
        .setInputCols(train_df.columns) \
        .setOutputCol("features")

    # This is where the program fails
    xgboost = XGBoostEstimator(
        featuresCol="features",
        labelCol="label",
        predictionCol="prediction"
    )

    pipeline = Pipeline().setStages([xgboost])
    pipeline.fit(train_df)

完整的异常(exception)是:

Traceback (most recent call last):
  File "/home/elad/DNA/dna/dna/run.py", line 283, in <module>
    main()
  File "/home/elad/DNA/dna/dna/run.py", line 247, in main
    offline_model = train_model(True, home_dir=config['home_dir'], hdfs_client=client)
  File "/home/elad/DNA/dna/dna/run.py", line 222, in train_model
    model = train(offline_mode=offline, spark=spark)
  File "/home/elad/DNA/dna/dna/model/xgboost_train.py", line 285, in train
    predictionCol="prediction"
  File "/home/elad/.conda/envs/DNAenv/lib/python2.7/site-packages/pyspark/__init__.py", line 105, in wrapper
    return func(self, **kwargs)
  File "/tmp/spark-7781039b-6821-42be-96e0-ca4005107318/userFiles-70b3d1de-a78c-4fac-b252-2f99a6761b32/sparkxgb.zip/sparkxgb/xgboost.py", line 115, in __init__
  File "/home/elad/.conda/envs/DNAenv/lib/python2.7/site-packages/pyspark/ml/wrapper.py", line 63, in _new_java_obj
    return java_obj(*java_args)
TypeError: 'JavaPackage' object is not callable
Exception AttributeError: "'NoneType' object has no attribute '_detach'" in <bound method XGBoostEstimator.__del__ of XGBoostEstimator_4f54b37156fb0a113233> ignored

我不知道为什么会发生这个异常,也不知道如何将sparkxgb正确集成到我的代码中。

如果有帮助,我们将不胜感激。

谢谢

最佳答案

经过一天对该模块的调试,问题只是错误地提交了 jar。 我在本地下载了 jar 并使用 pyspark-submit 它们:

PYSPARK_SUBMIT_ARGS=--jars resources/xgboost4j-0.72.jar,resources/xgboost4j-spark-0.72.jar

这解决了问题。

关于python - 如何将 xgboost 集成到 Spark 中? (Python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57942208/

相关文章:

python - 保存我的 Apache Spark 管道的中间状态

python - 当 Sprite 超过 10 个时,如何处理多个 Sprite 碰撞?

python - Scikits-学习 : Use custom vocabulary together with Pipeline

python - 类型 = 类别的列上的数据框子集

python - 运行 python 单元测试时切换分支

python - PySpark 合并数据帧和计数值

python - 将列中的 String 转换为 ArrayType 并分解

java - 将文本文件中的分隔数据读取到不同的 RDD 中

python - 如何将 RDD 保存到单个 Parquet 文件?

运行 Spark 时 python 脚本卡在输入法上