How to determine Spearmans correlation in Python?
DATA MUNGING DATA CLEANING PYTHON MACHINE LEARNING RECIPES PANDAS CHEATSHEET     ALL TAGS

# How to determine Spearmans correlation in Python?

This recipe helps you determine Spearmans correlation in Python

0

## Recipe Objective

Spearman"s correlation is very important statical data that we need many times. We can calculate it manually but it takes time.

So this is the recipe on how we can determine Spearman"s correlation in Python

## Step 1 - Importing Library

``` import matplotlib.pyplot as plt import scipy.stats import pandas as pd import random import seaborn as sns ```

We have imported stats, seaborn and pandas which is needed.

## Step 2 - Creating a dataframe

We have created a empty dataframe and then added rows to it with random numbers. ``` df = pd.DataFrame() df["x"] = random.sample(range(1, 100), 75) df["y"] = random.sample(range(1, 100), 75) print(); print(df.head()) ```

## Step 3 - Calculating Spearman"s correlation coefficient

We hawe defined a function with differnt steps that we will see.

• We have calculated rank of x and y and passed it in the function scipy.stats.spearmanr().
• ``` xranks = pd.Series(xs).rank() yranks = pd.Series(ys).rank() return scipy.stats.spearmanr(xranks, yranks) ```
• We have printed the result as well as the x and y values.
• ``` result = spearmans_rank_correlation(df.x, df.y) print() print("spearmans_rank_correlation is: ", result) ```

## Ploting Regression Plot

We are ploting regression plot with the fit. ``` sns.lmplot("x", "y", data=df, fit_reg=True) plt.show() ```

```   x   y
0  90  79
1  50  14
2  47  52
3  74  67
4  54  33

spearmans_rank_correlation is:  0.21755334281650068

```

#### Relevant Projects

##### PySpark Tutorial - Learn to use Apache Spark with Python
PySpark Project-Get a handle on using Python with Spark through this hands-on data processing spark python tutorial.

##### Credit Card Fraud Detection as a Classification Problem
In this data science project, we will predict the credit card fraud in the transactional dataset using some of the predictive models.

##### Predict Credit Default | Give Me Some Credit Kaggle
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.

##### Learn to prepare data for your next machine learning project
Text data requires special preparation before you can start using it for any machine learning project.In this ML project, you will learn about applying Machine Learning models to create classifiers and learn how to make sense of textual data.

##### Machine Learning or Predictive Models in IoT - Energy Prediction Use Case
In this machine learning and IoT project, we are going to test out the experimental data using various predictive models and train the models and break the energy usage.

##### Perform Time series modelling using Facebook Prophet
In this project, we are going to talk about Time Series Forecasting to predict the electricity requirement for a particular house using Prophet.

##### Customer Churn Prediction Analysis using Ensemble Techniques
In this machine learning churn project, we implement a churn prediction model in python using ensemble techniques.

##### Forecast Inventory demand using historical sales data in R
In this machine learning project, you will develop a machine learning model to accurately forecast inventory demand based on historical sales data.

##### Resume parsing with Machine learning - NLP with Python OCR and Spacy
In this machine learning resume parser example we use the popular Spacy NLP python library for OCR and text classification.

##### Data Science Project in Python on BigMart Sales Prediction
The goal of this data science project is to build a predictive model and find out the sales of each product at a given Big Mart store.