Skip to content

Commit f7d87b1

Browse files
huaxingaosrowen
authored andcommitted
[SPARK-25997][ML] add Python example code for Power Iteration Clustering in spark.ml
## What changes were proposed in this pull request? Add python example for Power Iteration Clustering in spark.ml ## How was this patch tested? Manually tested Closes apache#22996 from huaxingao/spark-25997. Authored-by: Huaxin Gao <[email protected]> Signed-off-by: Sean Owen <[email protected]>
1 parent 0fe9c14 commit f7d87b1

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

docs/ml-clustering.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,12 @@ Refer to the [Java API docs](api/java/org/apache/spark/ml/clustering/PowerIterat
298298
{% include_example java/org/apache/spark/examples/ml/JavaPowerIterationClusteringExample.java %}
299299
</div>
300300

301+
<div data-lang="python" markdown="1">
302+
Refer to the [Python API docs](api/python/pyspark.ml.html#pyspark.ml.clustering.PowerIterationClustering) for more details.
303+
304+
{% include_example python/ml/power_iteration_clustering_example.py %}
305+
</div>
306+
301307
<div data-lang="r" markdown="1">
302308

303309
Refer to the [R API docs](api/R/spark.powerIterationClustering.html) for more details.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#
2+
# Licensed to the Apache Software Foundation (ASF) under one or more
3+
# contributor license agreements. See the NOTICE file distributed with
4+
# this work for additional information regarding copyright ownership.
5+
# The ASF licenses this file to You under the Apache License, Version 2.0
6+
# (the "License"); you may not use this file except in compliance with
7+
# the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
18+
"""
19+
An example demonstrating PowerIterationClustering.
20+
Run with:
21+
bin/spark-submit examples/src/main/python/ml/power_iteration_clustering_example.py
22+
"""
23+
# $example on$
24+
from pyspark.ml.clustering import PowerIterationClustering
25+
# $example off$
26+
from pyspark.sql import SparkSession
27+
28+
if __name__ == "__main__":
29+
spark = SparkSession\
30+
.builder\
31+
.appName("PowerIterationClusteringExample")\
32+
.getOrCreate()
33+
34+
# $example on$
35+
df = spark.createDataFrame([
36+
(0, 1, 1.0),
37+
(0, 2, 1.0),
38+
(1, 2, 1.0),
39+
(3, 4, 1.0),
40+
(4, 0, 0.1)
41+
], ["src", "dst", "weight"])
42+
43+
pic = PowerIterationClustering(k=2, maxIter=20, initMode="degree", weightCol="weight")
44+
45+
# Shows the cluster assignment
46+
pic.assignClusters(df).show()
47+
# $example off$
48+
49+
spark.stop()

0 commit comments

Comments
 (0)