|
| 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