本文简要介绍
pyspark.mllib.clustering.PowerIterationClusteringModel
的用法。用法:
class pyspark.mllib.clustering.PowerIterationClusteringModel(java_model)
模型由
PowerIterationClustering
制作。1.5.0 版中的新函数。
例子:
>>> import math >>> def genCircle(r, n): ... points = [] ... for i in range(0, n): ... theta = 2.0 * math.pi * i / n ... points.append((r * math.cos(theta), r * math.sin(theta))) ... return points >>> def sim(x, y): ... dist2 = (x[0] - y[0]) * (x[0] - y[0]) + (x[1] - y[1]) * (x[1] - y[1]) ... return math.exp(-dist2 / 2.0) >>> r1 = 1.0 >>> n1 = 10 >>> r2 = 4.0 >>> n2 = 40 >>> n = n1 + n2 >>> points = genCircle(r1, n1) + genCircle(r2, n2) >>> similarities = [(i, j, sim(points[i], points[j])) for i in range(1, n) for j in range(0, i)] >>> rdd = sc.parallelize(similarities, 2) >>> model = PowerIterationClustering.train(rdd, 2, 40) >>> model.k 2 >>> result = sorted(model.assignments().collect(), key=lambda x: x.id) >>> result[0].cluster == result[1].cluster == result[2].cluster == result[3].cluster True >>> result[4].cluster == result[5].cluster == result[6].cluster == result[7].cluster True >>> import os, tempfile >>> path = tempfile.mkdtemp() >>> model.save(sc, path) >>> sameModel = PowerIterationClusteringModel.load(sc, path) >>> sameModel.k 2 >>> result = sorted(model.assignments().collect(), key=lambda x: x.id) >>> result[0].cluster == result[1].cluster == result[2].cluster == result[3].cluster True >>> result[4].cluster == result[5].cluster == result[6].cluster == result[7].cluster True >>> from shutil import rmtree >>> try: ... rmtree(path) ... except OSError: ... pass
相关用法
- Python pyspark PowerIterationClustering用法及代码示例
- Python pyspark PolynomialExpansion用法及代码示例
- Python pyspark PandasCogroupedOps.applyInPandas用法及代码示例
- Python pyspark PCA用法及代码示例
- Python pyspark PrefixSpanModel用法及代码示例
- Python pyspark PrefixSpan用法及代码示例
- Python pyspark ParamGridBuilder用法及代码示例
- Python pyspark create_map用法及代码示例
- Python pyspark date_add用法及代码示例
- Python pyspark DataFrame.to_latex用法及代码示例
- Python pyspark DataStreamReader.schema用法及代码示例
- Python pyspark MultiIndex.size用法及代码示例
- Python pyspark arrays_overlap用法及代码示例
- Python pyspark Series.asof用法及代码示例
- Python pyspark DataFrame.align用法及代码示例
- Python pyspark Index.is_monotonic_decreasing用法及代码示例
- Python pyspark IsotonicRegression用法及代码示例
- Python pyspark DataFrame.plot.bar用法及代码示例
- Python pyspark DataFrame.to_delta用法及代码示例
- Python pyspark element_at用法及代码示例
- Python pyspark explode用法及代码示例
- Python pyspark MultiIndex.hasnans用法及代码示例
- Python pyspark Series.to_frame用法及代码示例
- Python pyspark DataFrame.quantile用法及代码示例
- Python pyspark Column.withField用法及代码示例
注:本文由纯净天空筛选整理自spark.apache.org大神的英文原创作品 pyspark.mllib.clustering.PowerIterationClusteringModel。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。