Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 29.4k
[SPARK-11057] [SQL] Add correlation and covariance matrices#9366
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -19,7 +19,8 @@ package org.apache.spark.sql.execution.stat | ||
| import org.apache.spark.Logging | ||
| import org.apache.spark.sql.{Row, Column, DataFrame} | ||
| import org.apache.spark.sql.catalyst.expressions.{GenericMutableRow, Cast} | ||
| import org.apache.spark.sql.catalyst.expressions.{GenericMutableRow, Cast, AttributeReference} | ||
| import scala.collection.mutable.ArrayBuffer | ||
| import org.apache.spark.sql.catalyst.plans.logical.LocalRelation | ||
| import org.apache.spark.sql.functions._ | ||
| import org.apache.spark.sql.types._ | ||
| @@ -33,6 +34,31 @@ private[sql] object StatFunctions extends Logging { | ||
| counts.Ck / math.sqrt(counts.MkX * counts.MkY) | ||
| } | ||
| /** Calculate the Pearson Correlation matrix for given DataFrame */ | ||
| private[sql] def pearsonCorrelation(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 correlation matrix by computing column-by-column correlations | ||
| for (i <- 0 to fieldNames.length - 1){ | ||
| for (j <- 0 to i){ | ||
| val corr = pearsonCorrelation(df, Seq(fieldNames(i), fieldNames(j))) | ||
| rows(i).setDouble(j + 1, corr) | ||
| rows(j).setDouble(i + 1, corr) | ||
| } | ||
| rows(i).setDouble(i + 1, 1.0) | ||
| } | ||
| new DataFrame(df.sqlContext, new LocalRelation(dfStructAttrs, rows)) | ||
| } | ||
| /** Helper class to simplify tracking and merging counts. */ | ||
| private class CovarianceCounter extends Serializable { | ||
| var xAvg = 0.0 // the mean of all examples seen so far in col1 | ||
| @@ -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))) | ||
Contributor 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. You can't assume all columns are of numeric type. Catch exception here and use null as value if exception happens? | ||
| rows(i).setDouble(j + 1, cov) | ||
| rows(j).setDouble(i + 1, cov) | ||
| } | ||
| } | ||
| new DataFrame(df.sqlContext, new LocalRelation(dfStructAttrs, rows)) | ||
| } | ||
| /** Generate a table of frequencies for the elements of two columns. */ | ||
| private[sql] def crossTabulate(df: DataFrame, col1: String, col2: String): DataFrame = { | ||
| val tableName = s"${col1}_$col2" | ||
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.
You can't assume all columns are of numeric type. Catch exception here and use null as value if exception happens?
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.
I'm not sure if showing null is valid. If not numeric then not showing anything, I think so ....