我正在通过jupyter笔记本中的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函数的列表对象?我希望有人知道如何处理这个问题。谢谢!
map()
在Python 3中返回一个地图。因此,将其转换为列表。
assemblerInputs = list(map(lambda c: c + "classVec", categoricalColumns)) + numericCols
这应该工作。