Skip to main content

Introduction

xgboost for pyspark

python3.5 spark2.4.x xgboost0.9

python install

pip install PyXGBoost

code examples:

from pyspark.sql import SparkSession
from PyXGBoost import PyXGBoostClassifier, PyXGBoostClassificationModel
spark = SparkSession \
    .builder \
    .appName("pyspark xgboost") \
    .getOrCreate()

df=spark.read.csv("src/main/resources/iris.csv",schema="sepal_length double, sepal_width double, petal_length double,petal_width double,label int")
df=df.fillna(0)
#same as xgboost param map
params0 = {
    "objective" :"binary:logistic"
    , "eta" : 0.01
    , "max_depth" : 6
    , "min_child_weight" : 50
    , "colsample_bytree" : 0.5
    , "silent" : 0
    , "seed" : 12345
}

xgb=PyXGBoostClassifier(params0)

xgb.set_num_round(11) \
    .set_num_workers(11)

feature_names=["sepal_length","sepal_width","petal_length","petal_width"]
xgbModel=xgb.train(df,feature_names, "label")
xgbModel.saveOverwrite("hdfs://xxxx")
#xgbModel.write().overwrite().save("hdfs://xxxx")
xgbModel=PyXGBoostClassificationModel.load("hdfs://xxxx")
result_df=xgbModel.transform(df,feature_names)

download jar

https://github.com/303844828/PyXGBoost/blob/master/src/main/build/pyspark-xgboost-1.0-SNAPSHOT.jar

submit

spark-submit --master yarn-cluster --num-executors 100 \
--jars pyspark-xgboost-1.0-SNAPSHOT.jar  \
--py-files pyspark-xgboost-1.0-SNAPSHOT.jar \
--files test.py

简介

pyspark版本的xgboost

首先执行:

pip install PyXGBoost

代码示例:

from pyspark.sql import SparkSession
from PyXGBoost import PyXGBoostClassifier, PyXGBoostClassificationModel
spark = SparkSession \
    .builder \
    .appName("pyspark xgboost") \
    .getOrCreate()

df=spark.read.csv("src/main/resources/iris.csv",schema="sepal_length double, sepal_width double, petal_length double,petal_width double,label int")
df=df.fillna(0)
#same as xgboost param map
params0 = {
    "objective" :"binary:logistic"
    , "eta" : 0.01
    , "max_depth" : 6
    , "min_child_weight" : 50
    , "colsample_bytree" : 0.5
    , "silent" : 0
    , "seed" : 12345
}

xgb=PyXGBoostClassifier(params0)

xgb.set_num_round(11) \
    .set_num_workers(11)

feature_names=["sepal_length","sepal_width","petal_length","petal_width"]
xgbModel=xgb.train(df,feature_names, "label")
xgbModel.saveOverwrite("hdfs://xxxx")
#xgbModel.write().overwrite().save("hdfs://xxxx")
xgbModel=PyXGBoostClassificationModel.load("hdfs://xxxx")
result_df=xgbModel.transform(df,feature_names)

提交

命令需要在两个地方带上jar包,示例:

spark-submit --master yarn-cluster --num-executors 100 \
--jars pyspark-xgboost-1.0-SNAPSHOT.jar  \
--py-files pyspark-xgboost-1.0-SNAPSHOT.jar \
--files test.py

Metadata

Release files for PyXGBoost 1.0.9

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for PyXGBoost 1.0.9
File Size Uploaded Provenance Actions
PyXGBoost-1.0.9.tar.gz 2.9 kB Without provenance Details for PyXGBoost-1.0.9.tar.gz

Release files / PyXGBoost-1.0.9.tar.gz

Download URL PyXGBoost-1.0.9.tar.gz
Size 2.9 kB
Tags Source
SHA-256 checksum
How to use checksums
6dae7802cd9c9e10685b83be9c873edcf8c698957da029c7a306c1d0f2901d5c
BLAKE2b-256 checksum
How to use checksums
1e8b74902abb027a6ffb43a7483ed803d7ab8438497e6df9012ccca8a42b942f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/2.0.0 pkginfo/1.4.2 requests/2.22.0 setuptools/39.1.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.5

Security for PyXGBoost 1.0.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page