Skip to content

Instantly share code, notes, and snippets.

@cplaisier
Forked from mkcook/Correlation Matrix Practice
Last active January 14, 2019 15:14
Show Gist options
  • Select an option

  • Save cplaisier/881933b6d4fa086ea87b7259ae2629a2 to your computer and use it in GitHub Desktop.

Select an option

Save cplaisier/881933b6d4fa086ea87b7259ae2629a2 to your computer and use it in GitHub Desktop.
import pandas as pd # May not need this or may have already done this but so you know where pd comes from
#### Two possible solutions
##############################################################################
### 1. Calculate all possible values which is what you are doing currently ###
##############################################################################
corrDF_R = pd.DataFrame(index=df.index, columns=df.index)
corrDF_p = pd.DataFrame(index=df.index, columns=df.index)
for i in corrDF_p.index:
for j in corrDF_p.index:
if not i==j:
R_new, p_new = stats.pearsonr(df.loc[i], df.loc[j])
corrDF_R[i][j] = R_new
corrDF_p[i][j] = p_new
# Then filter the network edges using these complete matrices
newTFRegNet = {}
for TFreg in oldTFRegNet:
for TFtarg in oldTFRegNet[TFreg]:
if abs(corrDF_R[TFreg][TFtarg])>0.3 and corrDF_p[TFreg][TFtarg]<=0.05:
if not TFreg in newTFRegNet:
newTFRegNet[TFreg] = []
newTFRegNet[TFreg].append(TFtarg)
###################################################################################
### 2. Calculate correlations for only the edges in the network TFreg -> TFtarg ###
###################################################################################
newTFRegNet = {}
for TFreg in oldTFRegNet:
for TFtarg in oldTFRegNet[TFreg]:
R_new, p_new = stats.pearsonr(df.loc[TFreg], df.loc[TFtarg])
if abs(R_new)>=0.3 and p_new<=0.05:
if not TFreg in newTFRegNet:
newTFRegNet[TFreg] = []
newTFRegNet[TFreg].append(TFtarg)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment