0

I have Dense Vector, I would like to convert vector into string (to save CSV) and convert string back to Dense Vector when load.

More detail

val dense_vec = Vectors.dense(1.0, 2.0, 3.0)
dense_vec: org.apache.spark.mllib.linalg.Vector = [1.0,2.0,3.0]

val str_dense_vec = dense_vec.toString
str_dense_vec: String = [1.0,2.0,3.0]

I want to convert str_dense_vec as type String into org.apache.spark.mllib.linalg.Vector

1 Answer 1

1

You can create Double Array from String, then use dense method of org.apache.spark.mllib.linalg.Vector.

Vectors.dense(str_dense_vec.drop(1).dropRight(1).split(',').map(_.toDouble))

link api.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.