Github user sun-rui commented on a diff in the pull request:
https://github.com/apache/spark/pull/9366#discussion_r44898137
--- Diff:
sql/core/src/main/scala/org/apache/spark/sql/execution/stat/StatFunctions.scala
---
@@ -102,6 +128,34 @@ private[sql] object StatFunctions extends Logging {
counts.cov
}
+ /**
+ * Calculate the covariance of two numerical columns of a DataFrame.
+ * @param df The DataFrame
+ * @return the covariance matrix.
+ */
+ private[sql] def calculateCov(df: DataFrame): DataFrame = {
+ val fieldNames = df.schema.fieldNames
+ val dfStructAttrs = ArrayBuffer[AttributeReference](
+ AttributeReference("FieldName", StringType, true)())
+ val rows = fieldNames.map{fname => val countsRow = new
GenericMutableRow(fieldNames.length + 1)
+ countsRow.update(0, UTF8String.fromString(fname))
+ countsRow
+ }.toSeq
+ // generates field types of the output DataFrame
+ for(field <- fieldNames) dfStructAttrs += AttributeReference(field,
DoubleType, true)()
+
+ // fills the covariance matrix by computing column-by-column
covariances
+ for (i <- 0 to fieldNames.length-1){
+ for (j <- 0 to i){
+ val cov = calculateCov(df, Seq(fieldNames(i), fieldNames(j)))
--- End diff --
You can't assume all columns are of numeric type. Catch exception here and
use null as value if exception happens?
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]