cancel
Showing results for 
Search instead for 
Did you mean: 
Community Discussions
Connect with fellow community members to discuss general topics related to the Databricks platform, industry trends, and best practices. Share experiences, ask questions, and foster collaboration within the community.
cancel
Showing results for 
Search instead for 
Did you mean: 

Does databricks  support spearman rank correlation

chand20025
New Contributor

  while working on spark scala. I tried working on code using pearson correaltion it works but when I  tried to work on spearman rank correlation it displayed error like 

Currently only the calculation of the Pearson Correlation coefficient is supported.

Screenshot (21).png

 

1 REPLY 1

Kaniz_Fatma
Community Manager
Community Manager

Hi @chand20025

It seems that Databricks currently only supports the calculation of the Pearson Correlation coefficient and not the Spearman rank correlation. If you’re specifically looking to calculate Spearman rank correlation, you might need to explore alternative approaches or libraries.

One possible workaround is to use a common table expression (CTE) to compute the rank and then apply the corr() function on the result. Here’s an example in SQL:

WITH ranked_data AS (
    SELECT
        col1,
        col2,
        RANK() OVER (ORDER BY col1) AS rank_col1,
        RANK() OVER (ORDER BY col2) AS rank_col2
    FROM your_table
)
SELECT corr(rank_col1, rank_col2) AS spearman_correlation
FROM ranked_data;

This approach calculates the Spearman rank correlation by first ranking the data and then applying the correlation function. 

If you encounter any further issues or need additional assistance, feel free to ask! 😊

Join 100K+ Data Experts: Register Now & Grow with Us!

Excited to expand your horizons with us? Click here to Register and begin your journey to success!

Already a member? Login and join your local regional user group! If there isn’t one near you, fill out this form and we’ll create one for you to join!