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?

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

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.

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

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.

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.

Natural language processing Chatbot application using NLTK for text classification
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.

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.

Ecommerce product reviews - Pairwise ranking and sentiment analysis
This project analyzes a dataset containing ecommerce product reviews. The goal is to use machine learning models to perform sentiment analysis on product reviews and rank them based on relevance. Reviews play a key role in product recommendation systems.

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 - Instacart Market Basket Analysis
Data Science Project - Build a recommendation engine which will predict the products to be purchased by an Instacart consumer again.

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.