我有一个包含(String,SparseVector)值元组的RDD,我想使用该RDD创建一个DataFrame。为了获得大多数ml算法库所需的模式(label:string,features:vector)DataFrame。
我知道这是可行的,因为HashingTF ml库在给定DataFrame的特征列时输出向量。
所以我的问题是,我是否可以将(String,SparseVector)的RDD转换为(String,vector)的DataFrame。我尝试使用通常的
temp_df = sqlContext.createDataFrame(temp_rdd, StructType([
StructField("label", DoubleType(), False),
StructField("tokens", ArrayType(StringType()), False)
]))
#assumming there is an RDD (double,array(strings))
hashingTF = HashingTF(numFeatures=COMBINATIONS, inputCol="tokens", outputCol="features")
ndf = hashingTF.transform(temp_df)
ndf.printSchema()
#outputs
#root
#|-- label: double (nullable = false)
#|-- tokens: array (nullable = false)
#| |-- element: string (containsNull = true)
#|-- features: vector (nullable = true)
所以我的问题是,我是否可以将(String,SparseVector)的RDD转换为(String,vector)的DataFrame。我尝试使用通常的
sqlContext.createDataFrame
,但没有DataType符合我的需求。df = sqlContext.createDataFrame(rdd,StructType([
StructField("label" , StringType(),True),
StructField("features" , ?Type(),True)
]))