Я рассматриваю пример pyspark в блокноте jupyter, чтобы понять, как он работает.Я столкнулся с проблемой, по которой не могу найти справку.
Итак, вот код после загрузки 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'
Поэтому я предполагаю, что не могу использовать объект списка слямбда-функция?Я надеюсь, что у кого-то есть идея, как с этим справиться.Спасибо!