-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-14340][EXAMPLE][DOC] Update Examples and User Guide for ml.BisectingKMeans #11844
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| 0 1:0.0 2:0.0 3:0.0 | ||
| 1 1:0.1 2:0.1 3:0.1 | ||
| 2 1:0.2 2:0.2 3:0.2 | ||
| 3 1:9.0 2:9.0 3:9.0 | ||
| 4 1:9.1 2:9.1 3:9.1 | ||
| 5 1:9.2 2:9.2 3:9.2 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,15 +18,14 @@ | |
| from __future__ import print_function | ||
|
|
||
| # $example on$ | ||
| from pyspark.ml.clustering import BisectingKMeans, BisectingKMeansModel | ||
| from pyspark.mllib.linalg import VectorUDT, _convert_to_vector, Vectors | ||
| from pyspark.mllib.linalg import Vectors | ||
| from pyspark.sql.types import Row | ||
| from pyspark.ml.clustering import BisectingKMeans | ||
| # $example off$ | ||
| from pyspark.sql import SparkSession | ||
|
|
||
| """ | ||
| A simple example demonstrating a bisecting k-means clustering. | ||
| An example demonstrating bisecting k-means clustering. | ||
| Run with: | ||
| bin/spark-submit examples/src/main/python/ml/bisecting_k_means_example.py | ||
| """ | ||
|
|
||
| if __name__ == "__main__": | ||
|
|
@@ -36,21 +35,20 @@ | |
| .getOrCreate() | ||
|
|
||
| # $example on$ | ||
| data = spark.read.text("data/mllib/kmeans_data.txt").rdd | ||
| parsed = data\ | ||
| .map(lambda row: Row(features=Vectors.dense([float(x) for x in row.value.split(' ')]))) | ||
| training = spark.createDataFrame(parsed) | ||
| # Loads data. | ||
| dataset = spark.read.format("libsvm").load("data/mllib/sample_kmeans_data.txt") | ||
|
|
||
| kmeans = BisectingKMeans().setK(2).setSeed(1).setFeaturesCol("features") | ||
| # Trains a bisecting k-means model. | ||
| bkm = BisectingKMeans().setK(2).setSeed(1) | ||
| model = bkm.fit(dataset) | ||
|
|
||
| model = kmeans.fit(training) | ||
| # Evaluate clustering. | ||
| cost = model.computeCost(dataset) | ||
| print("Within Set Sum of Squared Errors = " + str(cost)) | ||
|
|
||
| # Evaluate clustering | ||
| cost = model.computeCost(training) | ||
| print("Bisecting K-means Cost = " + str(cost)) | ||
|
|
||
| centers = model.clusterCenters() | ||
| # Shows the result. | ||
| print("Cluster Centers: ") | ||
| centers = model.clusterCenters() | ||
|
||
| for center in centers: | ||
| print(center) | ||
| # $example off$ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.spark.examples.ml | ||
|
|
||
| // scalastyle:off println | ||
|
|
||
| // $example on$ | ||
| import org.apache.spark.ml.clustering.BisectingKMeans | ||
| // $example off$ | ||
| import org.apache.spark.sql.SparkSession | ||
|
|
||
| /** | ||
| * An example demonstrating bisecting k-means clustering. | ||
| * Run with | ||
| * {{{ | ||
| * bin/run-example ml.BisectingKMeansExample | ||
| * }}} | ||
| */ | ||
| object BisectingKMeansExample { | ||
|
|
||
| def main(args: Array[String]): Unit = { | ||
| // Creates a SparkSession | ||
| val spark = SparkSession | ||
| .builder | ||
| .appName("BisectingKMeansExample") | ||
| .getOrCreate() | ||
|
|
||
| // $example on$ | ||
| // Loads data. | ||
| val dataset = spark.read.format("libsvm").load("data/mllib/sample_kmeans_data.txt") | ||
|
|
||
| // Trains a bisecting k-means model. | ||
| val bkm = new BisectingKMeans().setK(2).setSeed(1) | ||
| val model = bkm.fit(dataset) | ||
|
|
||
| // Evaluate clustering. | ||
| val cost = model.computeCost(dataset) | ||
| println(s"Within Set Sum of Squared Errors = $cost") | ||
|
|
||
| // Shows the result. | ||
| println("Cluster Centers: ") | ||
| val centers = model.clusterCenters | ||
| centers.foreach(println) | ||
| // $example off$ | ||
|
|
||
| spark.stop() | ||
| } | ||
| } | ||
| // scalastyle:on println | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to add the "run-with" instruction to the main doc string, e.g.