Spark Pipeline框架允许以可重现的方式创建用于机器学习或其他应用程序的转换管道。但是,在创建数据框时,我希望能够执行探索性分析。
在我的情况下,我有约100列,其中80列是字符串,并且需要进行热编码:
from pyspark.ml.feature import OneHotEncoderEstimator, StringIndexer,VectorAssembler
from pyspark.ml import Pipeline
from pyspark.ml.classification import LogisticRegression
from pyspark.ml.classification import LogisticRegressionModel
#cols_to_one_hot_encode_2 is a list of columns that need to be one hot encoded
#cols_to_keep_as_is are columns that are **note** one hot encoded
cols_to_one_hot_encode_3=[i+"_hot" for i in cols_to_one_hot_encode_2]
encoder= OneHotEncoderEstimator(inputCols=cols_to_one_hot_encode_2,
outputCols=cols_to_one_hot_encode_3,dropLast=False)
#assemble pipeline
vectorAssembler = VectorAssembler().setInputCols(cols_to_keep_as_is+cols_to_one_hot_encode_3).setOutputCol("features")
all_stages=indexers
all_stages.append(encoder)
all_stages.append(vectorAssembler)
transformationPipeline=Pipeline(stages=all_stages)
fittedPipeline=transformationPipeline.fit(df_3)
dataset = fittedPipeline.transform(df_3)
#now pass to logistic regression
selectedcols = ["response_variable","features"] #+df_3.columns
dataset_2= dataset.select(selectedcols)
# Create initial LogisticRegression model
lr = LogisticRegression(labelCol="response_variable", featuresCol="features", maxIter=10,elasticNetParam=1)
# Train model with Training Data
lrModel = lr.fit(dataset_2)
当我查看数据集_2 display(dataset_2)
时,它会打印:
response_variable features
0 [0,6508,[1,4,53,155,166,186,205,242,2104,6225,6498],[8220,1,1,1,1,1,1,1,1,1,1]]
0 [0,6508,[1,3,53,155,165,185,207,243,2104,6225,6498],[8220,1,1,1,1,1,1,1,1,1,1]]
0 [0,6508,[1,2,53,158,170,185,206,241,2104,6225,6498],[8222,1,1,1,1,1,1,1,1,1,1]]
0 [0,6508,[1,3,53,156,168,185,205,240,2104,6225,6498],[8222,1,1,1,1,1,1,1,1,1,1]]
0 [0,6508,[1,2,53,155,166,185,205,240,2104,6225,6498],[8223,1,1,1,1,1,1,1,1,1,1]]
进行特征探索完全没有用。请注意,一键编码器将我的特征从100列爆炸到6508。
我的问题
我如何查看管道在后台创建的数据框? 该数据框应具有6058个特征并具有相应的行数,例如: 例如,我想要类似的东西:
response_variable feature_1_hot_1 feature_1_hot_2 feature_1_hot_3 ... (6505 more columns)
0 1 1 0
etc.
非重复
不是How to split Vector into columns - using PySpark的重复项 这就是在问如何基于分隔符进行文字字符串拆分。管道完成的转换不是简单的字符串拆分。参见Using Spark ML Pipelines just for Transformations
答案 0 :(得分:1)
我如何查看管道在后台创建的数据框?
没有这样的隐藏结构。 Spark ML Pipelines
围绕VectorUDT
列和元数据构建,以丰富结构。没有中间结构可容纳扩展的列,并且如果存在的话,它将无法扩展(Spark无法处理此处生成的宽而密集的数据,并且当列数达到数万时查询计划程序会阻塞)给定当前的实现方式。