【问题标题】:TypeError: unsupported operand type(s) for +: 'map' and 'list' with PysparkTypeError:+ 不支持的操作数类型:Pyspark 的“map”和“list”
【发布时间】:2019-08-21 00:16:36
【问题描述】:

我正在查看 jupyter notebook 中的一个 pyspark 示例,以了解它是如何工作的。我遇到了一个无法找到帮助的问题。

所以,这里是加载 sparkContext 和 SQLContext 后的代码:

census_data =SQLCtx.read.load('/home/john/Downloads/census.csv',
                             format = "com.databricks.spark.csv",
                             header = "true",
                             inferSchema = "true")

#The data looks like this:
pd.DataFrame(census_data.take(3), columns = census_data.columns)

    age     workclass   fnlwgt  education   education_num   marital_status  occupation  relationship    race    sex     capital_gain    capital_loss    hours_per_week  native_country  income
0   39  State-gov   77516   Bachelors   13  Never-married   Adm-clerical    Not-in-family   White   Male    2174    0   40  United-States   <=50K
1   50  Self-emp-not-inc    83311   Bachelors   13  Married-civ-spouse  Exec-managerial     Husband     White   Male    0   0   13  United-States   <=50K
2   38  Private     215646  HS-grad     9   Divorced    Handlers-cleaners   Not-in-family   White   Male    0   0   40  United-States   <=50K

以下我尝试使用 OneHotEncoder 标记编码:

from pyspark.ml import Pipeline
from pyspark.ml.feature import OneHotEncoder, StringIndexer, VectorAssembler

categoricalColumns = ["workclass", "education", "marital_status", "occupation", "relationship", "race", "sex", "native_country"]
stages = []
for categoricalCol in categoricalColumns:
    #indexing with StringIndexer
    stringIndexer = StringIndexer(inputCol=categoricalCol,
                                 outputCol=categoricalCol+'Index')
    encoder = OneHotEncoder(inputCol=categoricalCol+'Index',
                           outputCol=categoricalCol+'classVec')
    #Add stages
    stages += [stringIndexer, encoder]

# Convert label into label indices using the StringIndexer
label_stringIdx = StringIndexer(inputCol = "income", outputCol = "label")
stages += [label_stringIdx]

这一切运行良好。当我尝试使用vectorAssembler 时,Python 会抛出一个错误:

# Transform all features into a vector using VectorAssembler
numericCols = ["age", "fnlwgt", "education_num", "capital_gain", "capital_loss", "hours_per_week"]
assemblerInputs = map(lambda c: c + "TypeError: unsupported operand type(s) for +: 'map' and 'list'", categoricalColumns) + numericCols
assembler = VectorAssembler(inputCols=assemblerInputs, outputCol="features")
stages += [assembler]

以及完整的追溯:

TypeError                                 Traceback (most recent call last)
<ipython-input-23-16c50b42e41c> in <module>
      1 # Transform all features into a vector using VectorAssembler
      2 numericCols = ["age", "fnlwgt", "education_num", "capital_gain", "capital_loss", "hours_per_week"]
----> 3 assemblerInputs = map(lambda c: c + "classVec", categoricalColumns) + numericCols
      4 assembler = VectorAssembler(inputCols=assemblerInputs, outputCol="features")
      5 stages += [assembler]

TypeError: unsupported operand type(s) for +: 'map' and 'list'

所以我猜我不能将列表对象与 lambda 函数一起使用?我希望有人知道如何处理这个问题。谢谢!

【问题讨论】:

  • 好吧我发现这个不行:y = lambda x: x + [2,5,7]y(2)#get same type error。但这确实有效:y([3,4,5]) 虽然我希望我知道如何将这些知识应用于我的问题。
  • 查看 spark ml 中的 RFormula API,它提供了一种非常简洁的方法来进行索引、热编码和组装

标签: python-3.x pyspark apache-spark-sql apache-spark-ml


【解决方案1】:

map() 在 Python 3 中返回一个映射。因此,将其转换为列表。

assemblerInputs = list(map(lambda c: c + "classVec", categoricalColumns)) + numericCols

这应该可行。

【讨论】:

    猜你喜欢
    • 2014-12-28
    • 2012-12-12
    • 2016-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多