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)[0] 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

##### 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.

##### Time Series Forecasting with LSTM Neural Network Python
Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series data.

##### 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.

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

##### 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.

##### Data Science Project on Wine Quality Prediction in R
In this R data science project, we will explore wine dataset to assess red wine quality. The objective of this data science project is to explore which chemical properties will influence the quality of red wines.

##### Customer Market Basket Analysis using Apriori and Fpgrowth algorithms
In this data science project, you will learn how to perform market basket analysis with the application of Apriori and FP growth algorithms based on the concept of association rule learning.

##### Build an Image Classifier for Plant Species Identification
In this machine learning project, we will use binary leaf images and extracted features, including shape, margin, and texture to accurately identify plant species using different benchmark classification techniques.

##### Zillow’s Home Value Prediction (Zestimate)
Data Science Project in R -Build a machine learning algorithm to predict the future sale prices of homes.

##### Build a Collaborative Filtering Recommender System in Python
Use the Amazon Reviews/Ratings dataset of 2 Million records to build a recommender system using memory-based collaborative filtering in Python.