Spark模型如何处理向量列?

在Spark中,方法如何处理向量组装列?例如,如果我有经度和纬度列,使用向量组装器将它们组装起来再放入模型中与直接(分别)放入模型中是否有区别?

示例1:

loc_assembler = VectorAssembler(inputCols=['long', 'lat'], outputCol='loc')vector_assembler = VectorAssembler(inputCols=['loc', 'feature1', 'feature2'], outputCol='features')lr = LinearRegression(maxIter=10, regParam=0.3, elasticNetParam=0.8)pipeline = Pipeline(stages=[loc_assembler, vector_assembler, lr])

示例2:

vector_assembler = VectorAssembler(inputCols=['long', 'lat', 'feature1', 'feature2'], outputCol='features')lr = LinearRegression(maxIter=10, regParam=0.3, elasticNetParam=0.8)pipeline = Pipeline(stages=[vector_assembler, lr])

有什么区别?哪一种更好?


回答:

不会有任何区别,因为在你的两个示例中,features列的最终形式是相同的,即在你的第一个示例中,loc向量会被分解回其各个组成部分。

这里有一个使用虚拟数据的简短演示(不包括线性回归部分,因为它对于此讨论来说是不必要的):

spark.version#  u'2.3.1'# dummy data:df = spark.createDataFrame([[0, 33.3, -17.5, 10., 0.2],                              [1, 40.4, -20.5, 12., 2.2],                              [2, 28., -23.9, -2., -1.7],                              [3, 29.5, -19.0, -0.5, -0.2],                              [4, 32.8, -18.84, 1.5, 1.8]                             ],                              ["id","lat", "long", "other", "label"])from pyspark.ml.feature import VectorAssemblerfrom pyspark.ml.pipeline import Pipelineloc_assembler = VectorAssembler(inputCols=['long', 'lat'], outputCol='loc')vector_assembler = VectorAssembler(inputCols=['loc', 'other'], outputCol='features')pipeline = Pipeline(stages=[loc_assembler, vector_assembler])model = pipeline.fit(df)model.transform(df).show()

结果是:

+---+----+------+-----+-----+-------------+-----------------+| id| lat|  long|other|label|          loc|         features|+---+----+------+-----+-----+-------------+-----------------+|  0|33.3| -17.5| 10.0|  0.2| [-17.5,33.3]|[-17.5,33.3,10.0]||  1|40.4| -20.5| 12.0|  2.2| [-20.5,40.4]|[-20.5,40.4,12.0]||  2|28.0| -23.9| -2.0| -1.7| [-23.9,28.0]|[-23.9,28.0,-2.0]||  3|29.5| -19.0| -0.5| -0.2| [-19.0,29.5]|[-19.0,29.5,-0.5]||  4|32.8|-18.84|  1.5|  1.8|[-18.84,32.8]|[-18.84,32.8,1.5]| +---+----+------+-----+-----+-------------+-----------------+

即,features列与你的第二个示例(此处未显示)基本相同,在第二个示例中,你没有使用中间组装的特征loc

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中创建了一个多类分类项目。该项目可以对…

发表回复

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