Skip to content

Instantly share code, notes, and snippets.

@miroli
Created March 23, 2017 16:36
Show Gist options
  • Select an option

  • Save miroli/1e2327bcd9a71f8cc7127ec39a08e050 to your computer and use it in GitHub Desktop.

Select an option

Save miroli/1e2327bcd9a71f8cc7127ec39a08e050 to your computer and use it in GitHub Desktop.
correlation
def correlation(df, v1, v2):
"""
Calculates the correlation between two
variables in a pandas dataframe.
"""
# Get mean of each column
mean1, mean2 = df[v1].mean(), df[v2].mean()
# Get squared deviations from mean for each column
squared_deviation1 = [(x - mean1)**2 for x in df[v1]]
squared_deviation2 = [(x - mean2)**2 for x in df[v2]]
# Get the variance of each column
var1 = sum(squared_deviation1) / (len(df) - 1)
var2 = sum(squared_deviation2) / (len(df) - 1)
# Get the standard deviation of each column
std1, std2 = np.sqrt(var1), np.sqrt(var2)
# Calculate Z scores of all values, i.e.
# distance from mean in terms of standard deviations
z1 = ((df[v1] - mean1) / std1).copy()
z2 = ((df[v2] - mean2) / std2).copy()
# Calculate the correlation coefficient!
return sum(z1 * z2) / (len(df) - 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment