a feature transformer that merges multiple columns into a vector column

Solutions on MaxInterview for a feature transformer that merges multiple columns into a vector column by the best coders in the world

showing results for - "a feature transformer that merges multiple columns into a vector column"
Lina
20 Mar 2018
1# A feature transformer that merges multiple columns into a vector column
2
3df = spark.createDataFrame([(1, 0, 3)], ["a", "b", "c"])
4vecAssembler = VectorAssembler(inputCols=[
5  "a", "b", "c"], outputCol="features")
6vecAssembler.transform(df).head().features
7# DenseVector([1.0, 0.0, 3.0])
8vecAssembler.setParams(outputCol="freqs").transform(df).head().freqs
9# DenseVector([1.0, 0.0, 3.0])
10params = {vecAssembler.inputCols: [
11  "b", "a"], vecAssembler.outputCol: "vector"}
12vecAssembler.transform(df, params).head().vector
13# DenseVector([0.0, 1.0])
14vectorAssemblerPath = temp_path + "/vector-assembler"
15vecAssembler.save(vectorAssemblerPath)
16loadedAssembler = VectorAssembler.load(vectorAssemblerPath)
17loadedAssembler.transform(df).head().freqs == vecAssembler.transform(
18  df).head().freqs
19# True