Skip to content

Commit 9a5bbe0

Browse files
Alainsrowen
authored andcommitted
[MINOR] [MLLIB] Refactor toString method in MLLIB
1. predict(predict.toString) has already output prefix “predict” thus it’s duplicated to print ", predict = " again 2. there are some extra spaces Author: Alain <[email protected]> Closes apache#5687 from AiHe/tree-node-issue-2 and squashes the following commits: 9862b9a [Alain] Pass scala coding style checking 44ba947 [Alain] Minor][MLLIB] Format toString method in MLLIB bdc402f [Alain] [Minor][MLLIB] Fix a formatting bug in toString method in Node 426eee7 [Alain] [Minor][MLLIB] Fix a formatting bug in toString method in Node.scala
1 parent f5473c2 commit 9a5bbe0

File tree

6 files changed

+9
-12
lines changed

6 files changed

+9
-12
lines changed

mllib/src/main/scala/org/apache/spark/mllib/linalg/Vectors.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ class SparseVector(
526526
s" ${values.size} values.")
527527

528528
override def toString: String =
529-
"(%s,%s,%s)".format(size, indices.mkString("[", ",", "]"), values.mkString("[", ",", "]"))
529+
s"($size,${indices.mkString("[", ",", "]")},${values.mkString("[", ",", "]")})"
530530

531531
override def toArray: Array[Double] = {
532532
val data = new Array[Double](size)

mllib/src/main/scala/org/apache/spark/mllib/regression/LabeledPoint.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import org.apache.spark.SparkException
3232
@BeanInfo
3333
case class LabeledPoint(label: Double, features: Vector) {
3434
override def toString: String = {
35-
"(%s,%s)".format(label, features)
35+
s"($label,$features)"
3636
}
3737
}
3838

mllib/src/main/scala/org/apache/spark/mllib/tree/model/InformationGainStats.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ class InformationGainStats(
3939
val rightPredict: Predict) extends Serializable {
4040

4141
override def toString: String = {
42-
"gain = %f, impurity = %f, left impurity = %f, right impurity = %f"
43-
.format(gain, impurity, leftImpurity, rightImpurity)
42+
s"gain = $gain, impurity = $impurity, left impurity = $leftImpurity, " +
43+
s"right impurity = $rightImpurity"
4444
}
4545

4646
override def equals(o: Any): Boolean = o match {

mllib/src/main/scala/org/apache/spark/mllib/tree/model/Node.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ class Node (
5151
var stats: Option[InformationGainStats]) extends Serializable with Logging {
5252

5353
override def toString: String = {
54-
"id = " + id + ", isLeaf = " + isLeaf + ", predict = " + predict + ", " +
55-
"impurity = " + impurity + ", split = " + split + ", stats = " + stats
54+
s"id = $id, isLeaf = $isLeaf, predict = $predict, impurity = $impurity, " +
55+
s"split = $split, stats = $stats"
5656
}
5757

5858
/**

mllib/src/main/scala/org/apache/spark/mllib/tree/model/Predict.scala

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ class Predict(
2929
val predict: Double,
3030
val prob: Double = 0.0) extends Serializable {
3131

32-
override def toString: String = {
33-
"predict = %f, prob = %f".format(predict, prob)
34-
}
32+
override def toString: String = s"$predict (prob = $prob)"
3533

3634
override def equals(other: Any): Boolean = {
3735
other match {

mllib/src/main/scala/org/apache/spark/mllib/tree/model/Split.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ case class Split(
3939
categories: List[Double]) {
4040

4141
override def toString: String = {
42-
"Feature = " + feature + ", threshold = " + threshold + ", featureType = " + featureType +
43-
", categories = " + categories
42+
s"Feature = $feature, threshold = $threshold, featureType = $featureType, " +
43+
s"categories = $categories"
4444
}
4545
}
4646

@@ -68,4 +68,3 @@ private[tree] class DummyHighSplit(feature: Int, featureType: FeatureType)
6868
*/
6969
private[tree] class DummyCategoricalSplit(feature: Int, featureType: FeatureType)
7070
extends Split(feature, Double.MaxValue, featureType, List())
71-

0 commit comments

Comments
 (0)