Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
All examples under spark scala - String interpolation has implemented
  • Loading branch information
chetkhatri committed Dec 26, 2017
commit 8d729fa028c594523cbfdce4c5b315357330a88a
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ object BinaryClassificationMetricsExample {

// AUPRC
val auPRC = metrics.areaUnderPR
println("Area under precision-recall curve = " + auPRC)
println(s"Area under precision-recall curve = $auPRC")

// Compute thresholds used in ROC and PR curves
val thresholds = precision.map(_._1)
Expand All @@ -96,7 +96,7 @@ object BinaryClassificationMetricsExample {

// AUROC
val auROC = metrics.areaUnderROC
println("Area under ROC = " + auROC)
println(s"Area under ROC = $auROC")
// $example off$
sc.stop()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ object DecisionTreeClassificationExample {
(point.label, prediction)
}
val testErr = labelAndPreds.filter(r => r._1 != r._2).count().toDouble / testData.count()
println("Test Error = " + testErr)
println("Learned classification tree model:\n" + model.toDebugString)
println(s"Test Error = $testErr")
println(s"Learned classification tree model:\n ${model.toDebugString}")

// Save and load model
model.save(sc, "target/tmp/myDecisionTreeClassificationModel")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ object DecisionTreeRegressionExample {
(point.label, prediction)
}
val testMSE = labelsAndPredictions.map{ case (v, p) => math.pow(v - p, 2) }.mean()
println("Test Mean Squared Error = " + testMSE)
println("Learned regression tree model:\n" + model.toDebugString)
println(s"Test Mean Squared Error = $testMSE")
println(s"Learned regression tree model:\n ${model.toDebugString}")

// Save and load model
model.save(sc, "target/tmp/myDecisionTreeRegressionModel")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ object FPGrowthExample {
println(s"Number of frequent itemsets: ${model.freqItemsets.count()}")

model.freqItemsets.collect().foreach { itemset =>
println(itemset.items.mkString("[", ",", "]") + ", " + itemset.freq)
println(s"${itemset.items.mkString("[", ",", "]")}, ${itemset.freq}")
}

sc.stop()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ object GradientBoostingClassificationExample {
(point.label, prediction)
}
val testErr = labelAndPreds.filter(r => r._1 != r._2).count.toDouble / testData.count()
println("Test Error = " + testErr)
println("Learned classification GBT model:\n" + model.toDebugString)
println(s"Test Error = $testErr")
println(s"Learned classification GBT model:\n ${model.toDebugString}")

// Save and load model
model.save(sc, "target/tmp/myGradientBoostingClassificationModel")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ object GradientBoostingRegressionExample {
(point.label, prediction)
}
val testMSE = labelsAndPredictions.map{ case(v, p) => math.pow((v - p), 2)}.mean()
println("Test Mean Squared Error = " + testMSE)
println("Learned regression GBT model:\n" + model.toDebugString)
println(s"Test Mean Squared Error = $testMSE")
println(s"Learned regression GBT model:\n ${model.toDebugString}")

// Save and load model
model.save(sc, "target/tmp/myGradientBoostingRegressionModel")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ object HypothesisTestingExample {
// against the label.
val featureTestResults: Array[ChiSqTestResult] = Statistics.chiSqTest(obs)
featureTestResults.zipWithIndex.foreach { case (k, v) =>
println("Column " + (v + 1).toString + ":")
println(s"Column ${(v + 1).toString} :")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.toString is redundant here and elsewhere with interpolation. I think that should be simplified.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@srowen Thanks , Changes addressed

println(k)
} // summary of the test
// $example off$
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ object IsotonicRegressionExample {

// Calculate mean squared error between predicted and real labels.
val meanSquaredError = predictionAndLabel.map { case (p, l) => math.pow((p - l), 2) }.mean()
println("Mean Squared Error = " + meanSquaredError)
println(s"Mean Squared Error = $meanSquaredError")

// Save and load model
model.save(sc, "target/tmp/myIsotonicRegressionModel")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ object KMeansExample {

// Evaluate clustering by computing Within Set Sum of Squared Errors
val WSSSE = clusters.computeCost(parsedData)
println("Within Set Sum of Squared Errors = " + WSSSE)
println(s"Within Set Sum of Squared Errors = $WSSSE")

// Save and load model
clusters.save(sc, "target/org/apache/spark/KMeansExample/KMeansModel")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ object LBFGSExample {

println("Loss of each step in training process")
loss.foreach(println)
println("Area under ROC = " + auROC)
println(s"Area under ROC = $auROC")
// $example off$

sc.stop()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ object LatentDirichletAllocationExample {
val ldaModel = new LDA().setK(3).run(corpus)

// Output topics. Each is a distribution over words (matching word count vectors)
println("Learned topics (as distributions over vocab of " + ldaModel.vocabSize + " words):")
println(s"Learned topics (as distributions over vocab of ${ldaModel.vocabSize} words):")
val topics = ldaModel.topicsMatrix
for (topic <- Range(0, 3)) {
print("Topic " + topic + ":")
for (word <- Range(0, ldaModel.vocabSize)) { print(" " + topics(word, topic)); }
print(s"Topic $topic :")
for (word <- Range(0, ldaModel.vocabSize)) { print(s" ${topics(word, topic)}") }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Go ahead and put the print on a new line (I know it wasn't before)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@srowen Thanks for suggestion, it has been addressed.

println()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ object LinearRegressionWithSGDExample {
(point.label, prediction)
}
val MSE = valuesAndPreds.map{ case(v, p) => math.pow((v - p), 2) }.mean()
println("training Mean Squared Error = " + MSE)
println(s"training Mean Squared Error $MSE")

// Save and load model
model.save(sc, "target/tmp/scalaLinearRegressionWithSGDModel")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ object PCAExample {
val MSE = valuesAndPreds.map { case (v, p) => math.pow((v - p), 2) }.mean()
val MSE_pca = valuesAndPreds_pca.map { case (v, p) => math.pow((v - p), 2) }.mean()

println("Mean Squared Error = " + MSE)
println("PCA Mean Squared Error = " + MSE_pca)
println(s"Mean Squared Error = $MSE")
println(s"PCA Mean Squared Error = $MSE_pca")
// $example off$

sc.stop()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ object PMMLModelExportExample {
val clusters = KMeans.train(parsedData, numClusters, numIterations)

// Export to PMML to a String in PMML format
println("PMML Model:\n" + clusters.toPMML)
println(s"PMML Model:\n ${clusters.toPMML}")

// Export the model to a local file in PMML format
clusters.toPMML("/tmp/kmeans.xml")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ object PrefixSpanExample {
val model = prefixSpan.run(sequences)
model.freqSequences.collect().foreach { freqSequence =>
println(
freqSequence.sequence.map(_.mkString("[", ", ", "]")).mkString("[", ", ", "]") +
", " + freqSequence.freq)
s"${freqSequence.sequence.map(_.mkString("[", ", ", "]")).mkString("[", ", ", "]")}," +
s" ${freqSequence.freq}")
}
// $example off$

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ object RandomForestClassificationExample {
(point.label, prediction)
}
val testErr = labelAndPreds.filter(r => r._1 != r._2).count.toDouble / testData.count()
println("Test Error = " + testErr)
println("Learned classification forest model:\n" + model.toDebugString)
println(s"Test Error = $testErr")
println(s"Learned classification forest model:\n ${model.toDebugString}")

// Save and load model
model.save(sc, "target/tmp/myRandomForestClassificationModel")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ object RandomForestRegressionExample {
(point.label, prediction)
}
val testMSE = labelsAndPredictions.map{ case(v, p) => math.pow((v - p), 2)}.mean()
println("Test Mean Squared Error = " + testMSE)
println("Learned regression forest model:\n" + model.toDebugString)
println(s"Test Mean Squared Error = $testMSE")
println(s"Learned regression forest model:\n ${model.toDebugString}")

// Save and load model
model.save(sc, "target/tmp/myRandomForestRegressionModel")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ object RecommendationExample {
val err = (r1 - r2)
err * err
}.mean()
println("Mean Squared Error = " + MSE)
println(s"Mean Squared Error = $MSE")

// Save and load model
model.save(sc, "target/tmp/myCollaborativeFilter")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ object SVMWithSGDExample {
val metrics = new BinaryClassificationMetrics(scoreAndLabels)
val auROC = metrics.areaUnderROC()

println("Area under ROC = " + auROC)
println(s"Area under ROC = $auROC")

// Save and load model
model.save(sc, "target/tmp/scalaSVMWithSGDModel")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,13 @@ object SimpleFPGrowth {
val model = fpg.run(transactions)

model.freqItemsets.collect().foreach { itemset =>
println(itemset.items.mkString("[", ",", "]") + ", " + itemset.freq)
println(s"${itemset.items.mkString("[", ",", "]")},${itemset.freq}")
}

val minConfidence = 0.8
model.generateAssociationRules(minConfidence).collect().foreach { rule =>
println(
rule.antecedent.mkString("[", ",", "]")
+ " => " + rule.consequent .mkString("[", ",", "]")
+ ", " + rule.confidence)
println(s"${rule.antecedent.mkString("[", ",", "]")}=> " +
s"${rule.consequent .mkString("[", ",", "]")},${rule.confidence}")
}
// $example off$

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ object StratifiedSamplingExample {
val exactSample = data.sampleByKeyExact(withReplacement = false, fractions = fractions)
// $example off$

println("approxSample size is " + approxSample.collect().size.toString)
println(s"approxSample size is ${approxSample.collect().size.toString}")
approxSample.collect().foreach(println)

println("exactSample its size is " + exactSample.collect().size.toString)
println(s"exactSample its size is ${exactSample.collect().size.toString}")
exactSample.collect().foreach(println)

sc.stop()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ object TallSkinnyPCA {
// Compute principal components.
val pc = mat.computePrincipalComponents(mat.numCols().toInt)

println("Principal components are:\n" + pc)
println(s"Principal components are:\n $pc")

sc.stop()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ object TallSkinnySVD {
// Compute SVD.
val svd = mat.computeSVD(mat.numCols().toInt)

println("Singular values are " + svd.s)
println(s"Singular values are ${svd.s}")

sc.stop()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ class CustomReceiver(host: String, port: Int)
var socket: Socket = null
var userInput: String = null
try {
logInfo("Connecting to " + host + ":" + port)
logInfo(s"Connecting to $host $port")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: we could make the string consistent with the one two lines below by adding a colon

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, addressed

socket = new Socket(host, port)
logInfo("Connected to " + host + ":" + port)
logInfo(s"Connected to $host : $port")
val reader = new BufferedReader(
new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8))
userInput = reader.readLine()
Expand All @@ -98,7 +98,7 @@ class CustomReceiver(host: String, port: Int)
restart("Trying to connect again")
} catch {
case e: java.net.ConnectException =>
restart("Error connecting to " + host + ":" + port, e)
restart(s"Error connecting to $host : $port", e)
case t: Throwable =>
restart("Error receiving data", t)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ object RawNetworkGrep {
ssc.rawSocketStream[String](host, port, StorageLevel.MEMORY_ONLY_SER_2)).toArray
val union = ssc.union(rawStreams)
union.filter(_.contains("the")).count().foreachRDD(r =>
println("Grep count: " + r.collect().mkString))
println(s"Grep count: ${r.collect().mkString}"))
ssc.start()
ssc.awaitTermination()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,18 +130,18 @@ object RecoverableNetworkWordCount {
true
}
}.collect().mkString("[", ", ", "]")
val output = "Counts at time " + time + " " + counts
val output = s"Counts at time $time $counts"
println(output)
println("Dropped " + droppedWordsCounter.value + " word(s) totally")
println("Appending to " + outputFile.getAbsolutePath)
println(s"Dropped ${droppedWordsCounter.value} word(s) totally")
println(s"Appending to ${outputFile.getAbsolutePath}")
Files.append(output + "\n", outputFile, Charset.defaultCharset())
}
ssc
}

def main(args: Array[String]) {
if (args.length != 4) {
System.err.println("Your arguments were " + args.mkString("[", ", ", "]"))
System.err.println(s"Your arguments were ${args.mkString("[", ", ", "]")}")
System.err.println(
"""
|Usage: RecoverableNetworkWordCount <hostname> <port> <checkpoint-directory>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ object PageViewGenerator {
val viewsPerSecond = args(1).toFloat
val sleepDelayMs = (1000.0 / viewsPerSecond).toInt
val listener = new ServerSocket(port)
println("Listening on port: " + port)
println(s"Listening on port: $port")

while (true) {
val socket = listener.accept()
new Thread() {
override def run(): Unit = {
println("Got client connected from: " + socket.getInetAddress)
println(s"Got client connected from: ${socket.getInetAddress}")
val out = new PrintWriter(socket.getOutputStream(), true)

while (true) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ object PageViewStream {
.foreachRDD((rdd, time) => rdd.join(userList)
.map(_._2._2)
.take(10)
.foreach(u => println("Saw user %s at time %s".format(u, time))))
case _ => println("Invalid metric entered: " + metric)
.foreach(u => println(s"Saw user $u at time $time")))
case _ => println(s"Invalid metric entered: $metric")
}

ssc.start()
Expand Down