<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>question Re: How to convert a DataFrame to a Vector.dense in scala in Archives of Support Questions (Read Only)</title>
    <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/How-to-convert-a-DataFrame-to-a-Vector-dense-in-scala/m-p/148297#M28346</link>
    <description>&lt;P&gt;Perfect Solution.&lt;/P&gt;</description>
    <pubDate>Sat, 19 Nov 2016 09:29:25 GMT</pubDate>
    <dc:creator>dqzhangchn</dc:creator>
    <dc:date>2016-11-19T09:29:25Z</dc:date>
    <item>
      <title>How to convert a DataFrame to a Vector.dense in scala</title>
      <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/How-to-convert-a-DataFrame-to-a-Vector-dense-in-scala/m-p/148295#M28344</link>
      <description>&lt;P&gt;How to convert a DataFrame to a Vector.dense in scala&lt;/P&gt;&lt;P&gt;import org.apache.spark.mllib.linalg.Vectors
import org.apache.spark.sql.functions.{concat, lit}&lt;/P&gt;&lt;P&gt;val f = bh.select($"GL20".alias("LABEL"), concat($"SYMBOL", lit(":"), $"DATE").alias("ID"), $"TIR", $"UO", $"ROC20" )
f.show(3)&lt;/P&gt;&lt;P&gt;+---------+------------------+--------+--------+--------+
|  LABEL|  ID|  TIR|  UO|  ROC20|
+---------+------------------+--------+--------+--------+
|-5.452071|DJI.IDX:2010-04-20|73.26948|65.55433|  3.0704|
|-5.065461|DJT.IDX:2010-04-20|78.73316|68.14407|6.275064|
|-6.747381|NDX.IDX:2010-04-20|77.02333|68.68713|3.796183|
+---------+------------------+--------+--------+--------+&lt;/P&gt;&lt;P&gt;I want a new dataFrame in the format from the bh dataFrame above. &lt;/P&gt;&lt;P&gt;+------------------+--------------------+
|  ID|  FEATURES|
+------------------+--------------------+
|DJI.IDX:2010-04-20|[73.26948,65.5543...|
|DJT.IDX:2010-04-20|[78.73316,68.1440...|
|NDX.IDX:2010-04-20|[77.02333,68.6871...|
+------------------+--------------------+&lt;/P&gt;&lt;P&gt;If I hard code the values I can produce the above results but I need to get in programmatically from the bh dataFrame.&lt;/P&gt;&lt;P&gt;import org.apache.spark.ml.clustering.KMeans
import org.apache.spark.mllib.linalg.Vectors&lt;/P&gt;&lt;P&gt;// Crates a DataFrame
val df = sqlContext.createDataFrame(Seq(
  ("DJI.IDX:2010-04-20", Vectors.dense(73.26948, 65.55433, 3.0704)),
  ("DJT.IDX:2010-04-20", Vectors.dense(78.73316, 68.14407, 6.275064)),
  ("NDX.IDX:2010-04-20", Vectors.dense(77.02333, 68.68713, 3.796183))
)).toDF("ID", "FEATURES")&lt;/P&gt;&lt;P&gt;df.show()&lt;/P&gt;</description>
      <pubDate>Tue, 21 Apr 2026 13:30:21 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Archives-of-Support-Questions/How-to-convert-a-DataFrame-to-a-Vector-dense-in-scala/m-p/148295#M28344</guid>
      <dc:creator>wcarroll</dc:creator>
      <dc:date>2026-04-21T13:30:21Z</dc:date>
    </item>
    <item>
      <title>Re: How to convert a DataFrame to a Vector.dense in scala</title>
      <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/How-to-convert-a-DataFrame-to-a-Vector-dense-in-scala/m-p/148296#M28345</link>
      <description>&lt;P&gt;Adding answer in case others need this. I used the VectorAssembler. &lt;/P&gt;&lt;P&gt;&lt;A href="http://spark.apache.org/docs/latest/ml-features.html#vectorassembler" target="_blank"&gt;http://spark.apache.org/docs/latest/ml-features.html#vectorassembler&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;val assembler = new VectorAssembler().setInputCols(Array("TIR", "UO", "ROC20" )).setOutputCol("FEATURES")
val vd = assembler.transform(f)&lt;/P&gt;</description>
      <pubDate>Tue, 17 May 2016 17:10:38 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Archives-of-Support-Questions/How-to-convert-a-DataFrame-to-a-Vector-dense-in-scala/m-p/148296#M28345</guid>
      <dc:creator>wcarroll</dc:creator>
      <dc:date>2016-05-17T17:10:38Z</dc:date>
    </item>
    <item>
      <title>Re: How to convert a DataFrame to a Vector.dense in scala</title>
      <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/How-to-convert-a-DataFrame-to-a-Vector-dense-in-scala/m-p/148297#M28346</link>
      <description>&lt;P&gt;Perfect Solution.&lt;/P&gt;</description>
      <pubDate>Sat, 19 Nov 2016 09:29:25 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Archives-of-Support-Questions/How-to-convert-a-DataFrame-to-a-Vector-dense-in-scala/m-p/148297#M28346</guid>
      <dc:creator>dqzhangchn</dc:creator>
      <dc:date>2016-11-19T09:29:25Z</dc:date>
    </item>
  </channel>
</rss>

