What is correlation and how to find correlation using python

When two sets of data are strongly linked together we say they have a High Correlation.

Correlation is Positive when the values increase together, and
Correlation is Negative when one value decreases as the other increases

In common usage it most often refers to how close two variables are to having a linear relationship with each other. Here is sample values and shape for correlation



Pearson’s correlation coefficient

This is most commonly used correlation coefficient

The population correlation coefficient ρX,Y between two random variables X and Y with expected values μX and μY and standard deviations σX and σY is defined as

Pearson’s correlation coefficient using Python

When calculated using scipy, it returns pearson’s correlation coefficient and 2-tailed p-value

When calculated using numpy, it returns The correlation coefficient matrix of the variables.

Spearman’s rank correlation coefficient

Spearman’s rank correlation coefficient or Spearman’s rho, named after Charles Spearman and often denoted by the Greek letter rho. The Spearman correlation coefficient is defined as the Pearson correlation coefficient between the ranked variables.


Spearman’s rank correlation coefficient


Kendall rank correlation coefficient

the Kendall rank correlation coefficient, commonly referred to as Kendall’s tau coefficient (after the Greek letter τ), is a statistic used to measure the ordinal association between two measured quantities. A tau test is a non-parametric hypothesis test for statistical dependence based on the tau coefficient.


Python code for calculating Person’s, Spearman’s and Kendall’s coefficient.

Correlation can have a value:

  • 1 is a perfect positive correlation
  • 0 is no correlation (the values don’t seem linked at all)
  • -1 is a perfect negative correlation

Important points to be noted

  • Correlation is not causation
  • Person’s coefficient works only if there is linear relationship between two variables.

How to Find Mean, Median and Mode Using Python

Before calculating mean, median and mode, let us look at types of data and characteristics of the data. At a very high level data can be classified as categorical and quantitative data. Both can be further classified as below

Difference Order Similar Interval Meaningful Zero
Categorical Nominal (Cities) Yes
Categorical Ordinal (Temp.) Yes Yes
Quantitative Interval Yes Yes Yes
Quantitative Ration Yes Yes Yes Yes

Now all of these types of data do not have all characteritics

Mode Median Mean
Nominal Yes
Ordinal Yes
interval Yes Yes Yes
ratio Yes Yes Yes


Mean is nothing but average. It can be calculated in python or by using numpy


Middle value of observation when ordered from low to high


Mots commonly occurring observation



Resources for Learning Statistics

Various online resources (online sources, text books) are freely available on internet.

There are so many online courses that you might get overwhelmed bu the sheer numbers.


What is Linear Regression

Linear regression attempts to model the relationship between two variables by fitting a linear equation to observed data. One variable is considered to be an explanatory variable, and the other is considered to be a dependent variable. The case of one explanatory variable is called simple linear regression. For more than one explanatory variable, the process is called multiple linear regression.

Linear regression models are often fitted using the least squares approach.

If there appears to be no association between the proposed explanatory and dependent variables (i.e., the scatterplot does not indicate any increasing or decreasing trends), then fitting a linear regression model to the data probably will not provide a useful model. A valuable numerical measure of association between two variables is the correlation coefficient, which is a value between -1 and 1 indicating the strength of the association of the observed data for the two variables.

There are many names for a regression’s dependent variable. It may be called an outcome variable, criterion variable, endogenous variable, or regressand. The independent variables can be called exogenous variables, predictor variables, or regressors.


Linear regression using python

Following are the ways to do linear regression using python

  1. statsmodels
  2. scikit-learn
  3. scipy

Linear Regression using statsmodels

Here is sample code

and here is the output

Linear Regression using scikit-learn

Here is the code

and output of this code is as below

Linear Regression using scipy

Sample code

and output


If you look at code, it seems finding linear regression using scipy is shortest and easiest to understand.




What is Correlation ?

Correlation is  used to indicate dependence or association is any statistical relationship, whether causal or not, between two random variables or bivariate data. It is a measure of relationship between two mathematical variables or measured data values, which includes the Pearson correlation coefficient as a special case.Correlation is any of a broad class of statistical relationships involving dependence, though in common usage it most often refers to how close two variables are to having a linear relationship with each other.

The strength of the linear association between two variables is quantified by the correlation coefficient.

Formula for correlation is as below



  • The correlation coefficient always takes a value between -1 and 1,
  • Value of 1 or -1 indicating perfect correlation (all points would lie along a straight line in this case).
  • A correlation value close to 0 indicates no association between the variables.The closer the value of r to 0 the greater the variation around the line of best fit.
  • A positive correlation indicates a positive association between the variables (increasing values in one variable correspond to increasing values in the other variable),
  • while a negative correlation indicates a negative association between the variables (increasing values is one variable correspond to decreasing values in the other variable).
Correlation values for different datasets

The square of the correlation coefficient, r², is a useful value in linear regression. This value represents the fraction of the variation in one variable that may be explained by the other variable. Thus, if a correlation of 0.8 is observed between two variables (say, height and weight, for example), then a linear regression model attempting to explain either variable in terms of the other variable will account for 64% of the variability in the data1

the least-squares regression line will always pass through the means of x and y, the regression line may be entirely described by the means, standard deviations, and correlation of the two variables under investigation.

Pearson correlation coefficient

Pearsons correlation coefficient is a measure of the linear correlation between two variables X and Y. It has a value between +1 and −1 2.t is obtained by dividing the covariance of the two variables by the product of their standard deviations.

Formula for Pearson Correlation Coefficient

Rank correlation coefficients

Spearman’s rank correlation coefficient

The Spearman correlation coefficient is defined as the Pearson correlation coefficient between the ranked variables.3


Kendall rank correlation coefficient

the Kendall correlation between two variables will be high when observations have a similar (or identical for a correlation of 1) rank (i.e. relative position label of the observations within the variable: 1st, 2nd, 3rd, etc.) between the two variables, and low when observations have a dissimilar (or fully different for a correlation of -1) rank between the two variables.4

Goodman and Kruskal’s gamma

Goodman and Kruskal’s gamma is a measure of rank correlation, i.e., the similarity of the orderings of the data when ranked by each of the quantities. 5

You can find his report here

Now let us try to calculate these correlations using python, you can find code below

output is as below:

Pearson_correlation_coefficient. wikipedia. https://en.wikipedia.org. Accessed July 14, 2018.
Spearman’s rank correlation coefficient. wikipedia. https://en.wikipedia.org. Accessed July 14, 2018.
Kendall_rank_correlation_coefficient. wikipedia. https://en.wikipedia.org/. Accessed July 14, 2018.
Goodman and Kruskal’s gamma. wikipedia. https://en.wikipedia.org/wiki/Goodman_and_Kruskal%27s_gamma. Accessed July 14, 2018.

What is Regression and Types of Regression

2Regression is a set of statistical processes for estimating the relationships among variables. It includes many techniques for modeling and analyzing several variables, when the focus is on the relationship between a dependent variable (target) and one or more independent variables (or ‘predictors’).

Regression analysis helps one understand how the typical value of the dependent variable (or ‘criterion variable’) changes when any one of the independent variables is varied, while the other independent variables are held fixed.


It is used in variety of places such as forecasting, time series analysis etc. across industries.

  • Regression analysis is used characterize the variation of the dependent variable around the prediction of the regression function using a probability distribution
  • A function of the independent variables called the regression function is to be estimated
  • Regression analysis can be used to infer causal relationships between the independent and dependent variables. However this can lead to illusions or false relationships, so caution is advisable as correlation does not prove causation.

Types of regression:

  1. Linear Regression
    1. Simple Linear Regression
    2. multiple linear regression.
  2. Logistic Regression
    1. Simple Logistic Regression
    2. Multiple Logistic Regression
  3. Polynomial Regression
  4. Stepwise Regression
  5. Ridge Regression
  6. Lasso Regression
  7. ElasticNet Regression1
Ray S. 7 Types of Regression Techniques you should know! analyticsvidhya. https://www.analyticsvidhya.com. Accessed July 14, 2018.
Regression_analysis. wikipedia. https://en.wikipedia.org/wiki/Regression_analysis. Accessed July 14, 2018.