How to determine Pearsons correlation in Python?

How to determine Pearsons correlation in Python?

How to determine Pearsons correlation in Python?

This recipe helps you determine Pearsons correlation in Python

In [2]:
def Snippet_120():
    print(format('How to determine Pearson\'s correlation in Python','*^82'))

    import warnings

    # load libraries
    import matplotlib.pyplot as plt
    import statistics as stats
    import pandas as pd
    import random
    import seaborn as sns

    # Create empty dataframe
    df = pd.DataFrame()
    # Add columns
    df['x'] = random.sample(range(1, 100), 75)
    df['y'] = random.sample(range(1, 100), 75)

    # View first few rows of data
    print(); print(df.head())

    # Calculate Pearson’s Correlation Coefficient
    def pearson(x,y):
        # Create n, the number of observations in the data
        n = len(x)
        # Create lists to store the standard scores
        standard_score_x = []; standard_score_y = [];
        # Calculate the mean of x
        mean_x = stats.mean(x)
        # Calculate the standard deviation of x
        standard_deviation_x = stats.stdev(x)
        # Calculate the mean of y
        mean_y = stats.mean(y)
        # Calculate the standard deviation of y
        standard_deviation_y = stats.stdev(y)
        # For each observation in x
        for observation in x:
            # Calculate the standard score of x
            standard_score_x.append((observation - mean_x)/standard_deviation_x)
        # For each observation in y
        for observation in y:
            # Calculate the standard score of y
            standard_score_y.append((observation - mean_y)/standard_deviation_y)
        # Multiple the standard scores together, sum them, then divide by n-1, return that value
        return (sum([i*j for i,j in zip(standard_score_x, standard_score_y)]))/(n-1)

    # Show Pearson's Correlation Coefficient
    result = pearson(df.x, df.y)
    print("Pearson\'s correlation coefficient is: ", result)
    sns.lmplot('x', 'y', data=df, fit_reg=True)

*****************How to determine Pearson's correlation in Python*****************

    x   y
0  69  99
1  56  30
2  64  62
3  58   8
4  14  64

Pearson's correlation coefficient is:  0.3810462941506265
In [ ]:

Relevant Projects

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.

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.

Solving Multiple Classification use cases Using H2O
In this project, we are going to talk about H2O and functionality in terms of building Machine Learning models.

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.

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.

Loan Eligibility Prediction using Gradient Boosting Classifier
This data science in python project predicts if a loan should be given to an applicant or not. We predict if the customer is eligible for loan based on several factors like credit score and past history.

Machine Learning project for Retail Price Optimization
In this machine learning pricing project, we implement a retail price optimization algorithm using regression trees. This is one of the first steps to building a dynamic pricing model.

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.

Deep Learning with Keras in R to Predict Customer Churn
In this deep learning project, we will predict customer churn using Artificial Neural Networks and learn how to model an ANN in R with the keras deep learning package.

Predict Employee Computer Access Needs in Python
Data Science Project in Python- Given his or her job role, predict employee access needs using amazon employee database.