本文整理汇总了Python中pyspark.ml.clustering.KMeans.transform方法的典型用法代码示例。如果您正苦于以下问题:Python KMeans.transform方法的具体用法?Python KMeans.transform怎么用?Python KMeans.transform使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyspark.ml.clustering.KMeans
的用法示例。
在下文中一共展示了KMeans.transform方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: jobs
# 需要导入模块: from pyspark.ml.clustering import KMeans [as 别名]
# 或者: from pyspark.ml.clustering.KMeans import transform [as 别名]
# MAGIC
# MAGIC **Note:** This command multiple spark jobs (one job per iteration in the KMeans algorithm). You will see the progress bar starting over and over again.
# COMMAND ----------
from pyspark.ml.clustering import KMeans
model = KMeans().setK(2).fit(trainingData)
# COMMAND ----------
# MAGIC %md To see the result of our clustering, we produce a scatter plot matrix that shows interaction between input variables and learned clusters. To get that we apply the model on the original data and pick four columns: `prediction` and the original features (`duration`, `tempo`, and `loudness`).
# COMMAND ----------
transformed = model.transform(trainingData).select("duration", "tempo", "loudness", "prediction")
# COMMAND ----------
# MAGIC %md To comfortably visualize the data we produce a random sample.
# MAGIC Remember the `display()` function? We can use it to produce a nicely rendered table of transformed DataFrame.
# COMMAND ----------
display(transformed.sample(False, fraction=0.005))
# COMMAND ----------
# MAGIC %md To generate a scatter plot matrix, click on the plot button bellow the table and select `scatter`. That will transform your table to a scatter plot matrix. It automatically picks all numeric columns as values. To include predicted clusters, click on `Plot Options` and drag `prediction` to the list of Keys. You will get the following plot. On the diagonal panels you see the PDF of marginal distribution of each variable. Non-diagonal panels show a scatter plot between variables of the two variables of the row and column. For example the top right panel shows the scatter plot between duration and loudness. Each point is colored according to the cluster it is assigned to.
# COMMAND ----------