如何在PySpark管道阶段中处理字符串索引器和独热编码器

针对以下代码遇到此错误:

stage_string = [StringIndexer(inputCol=c, outputCol=c + "_string_encoded")  for c in categorical_columns]stage_one_hot = [OneHotEncoder(inputCol=c + "_string_encoded", outputCol=c + "_one_hot") for c in categorical_columns]assembler = VectorAssembler(inputCols=feature_list, outputCol="features")rf = RandomForestClassifier(labelCol="output", featuresCol="features")pipeline = Pipeline(stages=[stage_string,stage_one_hot,assembler, rf]) pipeline.fit(df)
Cannot recognize a pipeline stage of type <class 'list'>.Traceback (most recent call last):  File "/usr/lib/spark/python/lib/pyspark.zip/pyspark/ml/base.py", line 132, in fit    return self._fit(dataset)  File "/usr/lib/spark/python/lib/pyspark.zip/pyspark/ml/pipeline.py", line 97, in _fit    "Cannot recognize a pipeline stage of type %s." % type(stage))TypeError: Cannot recognize a pipeline stage of type <class 'list'>.

回答:

问题出在这一语句上pipeline = Pipeline(stages=[stage_string,stage_one_hot,assembler, rf]),其中stage_stringstage_one_hotPipelineStage的列表,而assemblerrf是单个的管道阶段。

请按以下方式修改您的语句:

stages = stage_string + stage_one_hot + [assembler, rf]pipeline = Pipeline(stages=stages)

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注