Skip to content

Commit 1c66cdd

Browse files
committed
Wed Dec 16 00:47:33 PST 2015
1 parent dcb5a40 commit 1c66cdd

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

mllib/src/main/scala/org/apache/spark/ml/feature/VectorAssembler.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,12 @@ class VectorAssembler(override val uid: String)
7070
val group = AttributeGroup.fromStructField(field)
7171
if (group.attributes.isDefined) {
7272
// If attributes are defined, copy them with updated names.
73-
group.attributes.get.map { attr =>
73+
group.attributes.get.zipWithIndex.map { case (attr, i) =>
7474
if (attr.name.isDefined) {
7575
// TODO: Define a rigorous naming scheme.
7676
attr.withName(c + "_" + attr.name.get)
7777
} else {
78-
attr
78+
attr.withName(c + "_" + i)
7979
}
8080
}
8181
} else {

mllib/src/test/scala/org/apache/spark/ml/feature/RFormulaSuite.scala

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,28 @@ class RFormulaSuite extends SparkFunSuite with MLlibTestSparkContext {
159159
assert(attrs === expectedAttrs)
160160
}
161161

162+
test("vector attribute generation with unnamed input attrs") {
163+
val formula = new RFormula().setFormula("id ~ vec2")
164+
val base = sqlContext.createDataFrame(
165+
Seq((1, Vectors.dense(0.0, 1.0)), (2, Vectors.dense(1.0, 2.0)))
166+
).toDF("id", "vec")
167+
val metadata = new AttributeGroup(
168+
"vec2",
169+
Array[Attribute](
170+
NumericAttribute.defaultAttr,
171+
NumericAttribute.defaultAttr)).toMetadata
172+
val original = base.select(base.col("id"), base.col("vec").as("vec2", metadata))
173+
val model = formula.fit(original)
174+
val result = model.transform(original)
175+
val attrs = AttributeGroup.fromStructField(result.schema("features"))
176+
val expectedAttrs = new AttributeGroup(
177+
"features",
178+
Array[Attribute](
179+
new NumericAttribute(Some("vec2_0"), Some(1)),
180+
new NumericAttribute(Some("vec2_1"), Some(2))))
181+
assert(attrs === expectedAttrs)
182+
}
183+
162184
test("numeric interaction") {
163185
val formula = new RFormula().setFormula("a ~ b:c:d")
164186
val original = sqlContext.createDataFrame(

0 commit comments

Comments
 (0)