-
Notifications
You must be signed in to change notification settings - Fork 28.3k
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
[Minor][ML] Refactor clustering summary. #15555
Changes from 1 commit
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 |
---|---|---|
|
@@ -365,33 +365,20 @@ object GaussianMixture extends DefaultParamsReadable[GaussianMixture] { | |
@Since("2.0.0") | ||
@Experimental | ||
class GaussianMixtureSummary private[clustering] ( | ||
@Since("2.0.0") @transient val predictions: DataFrame, | ||
@Since("2.0.0") val predictionCol: String, | ||
@Since("2.0.0") val probabilityCol: String, | ||
@Since("2.0.0") val featuresCol: String, | ||
@Since("2.0.0") val k: Int) extends Serializable { | ||
|
||
/** | ||
* Cluster centers of the transformed data. | ||
*/ | ||
@Since("2.0.0") | ||
@transient lazy val cluster: DataFrame = predictions.select(predictionCol) | ||
predictions: DataFrame, | ||
predictionCol: String, | ||
val probabilityCol: String, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could do a Since tag here |
||
featuresCol: String, | ||
k: Int) | ||
extends ClusteringSummary ( | ||
predictions, | ||
predictionCol, | ||
featuresCol, | ||
k) { | ||
|
||
/** | ||
* Probability of each cluster. | ||
*/ | ||
@Since("2.0.0") | ||
@transient lazy val probability: DataFrame = predictions.select(probabilityCol) | ||
|
||
/** | ||
* Size of (number of data points in) each cluster. | ||
*/ | ||
@Since("2.0.0") | ||
lazy val clusterSizes: Array[Long] = { | ||
val sizes = Array.fill[Long](k)(0) | ||
cluster.groupBy(predictionCol).count().select(predictionCol, "count").collect().foreach { | ||
case Row(cluster: Int, count: Long) => sizes(cluster) = count | ||
} | ||
sizes | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -354,21 +354,41 @@ object KMeans extends DefaultParamsReadable[KMeans] { | |
@Since("2.0.0") | ||
@Experimental | ||
class KMeansSummary private[clustering] ( | ||
@Since("2.0.0") @transient val predictions: DataFrame, | ||
@Since("2.0.0") val predictionCol: String, | ||
@Since("2.0.0") val featuresCol: String, | ||
@Since("2.0.0") val k: Int) extends Serializable { | ||
predictions: DataFrame, | ||
predictionCol: String, | ||
featuresCol: String, | ||
k: Int) | ||
extends ClusteringSummary ( | ||
predictions, | ||
predictionCol, | ||
featuresCol, | ||
k | ||
) | ||
|
||
/** | ||
* :: Experimental :: | ||
* Summary of clustering. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "Summary of clustering algorithms." ? |
||
* | ||
* @param predictions [[DataFrame]] produced by model.transform() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Add periods for each line |
||
* @param predictionCol Name for column of predicted clusters in `predictions` | ||
* @param featuresCol Name for column of features in `predictions` | ||
* @param k Number of clusters | ||
*/ | ||
@Experimental | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what about adding There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not entirely certain on the official policy for the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm also ambivalent about this, the reason behind my change is that some classes such as |
||
class ClusteringSummary private[clustering] ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this is generic to clustering, how about putting it in a new file? |
||
@transient val predictions: DataFrame, | ||
val predictionCol: String, | ||
val featuresCol: String, | ||
val k: Int) extends Serializable { | ||
|
||
/** | ||
* Cluster centers of the transformed data. | ||
*/ | ||
@Since("2.0.0") | ||
@transient lazy val cluster: DataFrame = predictions.select(predictionCol) | ||
|
||
/** | ||
* Size of (number of data points in) each cluster. | ||
*/ | ||
@Since("2.0.0") | ||
lazy val clusterSizes: Array[Long] = { | ||
val sizes = Array.fill[Long](k)(0) | ||
cluster.groupBy(predictionCol).count().select(predictionCol, "count").collect().foreach { | ||
|
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.
minor: this can fit on one line like:
k: Int) extends ClusteringSummary(..., ..., ...)