【问题标题】:XGBoost Spark One Model Per Worker IntegrationXGBoost Spark 每个工人集成一个模型
【发布时间】:2019-10-28 17:11:41
【问题描述】:

正在尝试使用此笔记本https://databricks-prod-cloudfront.cloud.databricks.com/public/4027ec902e239c93eaaa8714f173bcfc/1526931011080774/3624187670661048/6320440561800420/latest.html

使用 spark 版本 2.4.3 和 xgboost 0.90

尝试执行时不断收到此错误ValueError: bad input shape () ...

features = inputTrainingDF.select("features").collect()
lables = inputTrainingDF.select("label").collect()

X = np.asarray(map(lambda v: v[0].toArray(), features))
Y = np.asarray(map(lambda v: v[0], lables))

xgbClassifier = xgb.XGBClassifier(max_depth=3, seed=18238, objective='binary:logistic')

model = xgbClassifier.fit(X, Y)
ValueError: bad input shape () 

def trainXGbModel(partitionKey, labelAndFeatures):
  X = np.asarray(map(lambda v: v[1].toArray(), labelAndFeatures))
  Y = np.asarray(map(lambda v: v[0], labelAndFeatures))
  xgbClassifier = xgb.XGBClassifier(max_depth=3, seed=18238, objective='binary:logistic' )
  model =  xgbClassifier.fit(X, Y)
  return [partitionKey, model]

xgbModels = inputTrainingDF\
.select("education", "label", "features")\
.rdd\
.map(lambda row: [row[0], [row[1], row[2]]])\
.groupByKey()\
.map(lambda v: trainXGbModel(v[0], list(v[1])))

xgbModels.take(1)
ValueError: bad input shape ()

您可以在笔记本中看到它适用于发布它的人。我的猜测是它与 XY np.asarray() 映射有关,因为逻辑只是试图将标签和特征映射到函数,但形状是空的。使用此代码可以正常工作

pandasDF = inputTrainingDF.toPandas()
series = pandasDF['features'].apply(lambda x : np.array(x.toArray())).as_matrix().reshape(-1,1)
features = np.apply_along_axis(lambda x : x[0], 1, series)
target = pandasDF['label'].values
xgbClassifier = xgb.XGBClassifier(max_depth=3, seed=18238, objective='binary:logistic' )
model = xgbClassifier.fit(features, target)

但是想集成到原始函数调用中并了解为什么原始笔记本不起作用。非常感谢您多加一双眼睛来解决这个问题!

【问题讨论】:

    标签: apache-spark pyspark apache-spark-mllib xgboost apache-spark-ml


    【解决方案1】:

    您可能正在使用python3。问题是在 python3 map 函数返回一个迭代器对象,而不是一个集合。修复此示例所需要做的就是更改 map -> list(map(...)):

    def trainXGbModel(partitionKey, labelAndFeatures):
      X = np.asarray(list(map(lambda v: v[1].toArray(), labelAndFeatures)))
      Y = np.asarray(list(map(lambda v: v[0], labelAndFeatures)))
    

    或者您可以使用np.fromiter 将可迭代对象转换为numpy 数组。

    【讨论】:

      猜你喜欢
      • 2020-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-21
      • 2021-07-15
      • 1970-01-01
      • 2017-02-22
      • 1970-01-01
      相关资源
      最近更新 更多