Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
658 views
in Technique[技术] by (71.8m points)

row to vector in scala spark

I have a row that is obtained in the following way:

val row_name = df.collect()(i)

how can I convert this row to a type vector so that it can be passed to fromML() as follows ?

val vector_name=org.apache.spark.mllib.linalg.Vectors.fromML(row_name)

thanks in advance!

question from:https://stackoverflow.com/questions/65882176/row-to-vector-in-scala-spark

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use vectorAssembler:

import org.apache.spark.ml.linalg.Vector
import org.apache.spark.mllib.linalg.Vectors
import org.apache.spark.ml.feature.VectorAssembler

val df = Seq((1,2),(3,4)).toDF("col1","col2")

val va = new VectorAssembler().setInputCols(Array("col1","col2")).setOutputCol("vector")

val row0 = va.transform(df).select("vector").collect()(0).getAs[Vector](0)

val vector0 = Vectors.fromML(row0)
// vector0: org.apache.spark.mllib.linalg.Vector = [1.0,2.0]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...