Search code examples
python-3.xpysparkapache-spark-sqlapache-spark-ml

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


I am going through a pyspark example in a jupyter notebook to get a feel for how it works. I have run into a problem I cannot find help on.

So, here is the code after loading sparkContext and 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

Following I try to label encode with 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]

All this runs fine. When I try to use the vectorAssembler, Python throws an error:

# 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]

And the full traceback:

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'

So I am guessing I can't use a list object with the lambda function? I hope somebody has an idea as to how to handle this. Thanks!


Solution

  • map() returns a map in Python 3. Hence, transform it to list.

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

    This should work.