The R function caret::findCorrelation searches a correlation matrix and returns a vector of integers corresponding to variables which, if removed, would reduce pair-wise correlations among the remaining variables. Here is the R code for this function:
<SPAN class="" style="color: #101094; border: 0px; font-size: 13px;">function</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> </SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">(</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">x</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">,</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> cutoff </SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">=</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> </SPAN><SPAN class="" style="color: #7d2727; border: 0px; font-size: 13px;">0.9</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">,</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> verbose </SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">=</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> FALSE</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">,</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> names </SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">=</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> FALSE</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">,</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> exact </SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">=</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> ncol</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">(</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">x</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">)</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> </SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"><</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> </SPAN><SPAN class="" style="color: #7d2727; border: 0px; font-size: 13px;">100</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">)</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> </SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">{</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> </SPAN><SPAN class="" style="color: #101094; border: 0px; font-size: 13px;">if</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> </SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">(</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">names </SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">&</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> </SPAN><SPAN class="" style="color: #101094; border: 0px; font-size: 13px;">is</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">.</SPAN><SPAN class="" style="color: #101094; border: 0px; font-size: 13px;">null</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">(</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">colnames</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">(</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">x</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">)))</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> stop</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">(</SPAN><SPAN class="" style="color: #7d2727; border: 0px; font-size: 13px;">"'x' must have column names when `names = TRUE`"</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">)</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> </SPAN><SPAN class="" style="color: #101094; border: 0px; font-size: 13px;">out</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> </SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"><-</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> </SPAN><SPAN class="" style="color: #101094; border: 0px; font-size: 13px;">if</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> </SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">(</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">exact</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">)</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> findCorrelation_exact</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">(</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">x </SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">=</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> x</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">,</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> cutoff </SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">=</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> cutoff</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">,</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> verbose </SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">=</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> verbose</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">)</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> </SPAN><SPAN class="" style="color: #101094; border: 0px; font-size: 13px;">else</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> findCorrelation_fast</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">(</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">x </SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">=</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> x</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">,</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> cutoff </SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">=</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> cutoff</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">,</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> verbose </SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">=</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> verbose</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">)</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> </SPAN><SPAN class="" style="color: #101094; border: 0px; font-size: 13px;">out</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> </SPAN><SPAN class="" style="color: #101094; border: 0px; font-size: 13px;">if</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> </SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">(</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">names</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">)</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> </SPAN><SPAN class="" style="color: #101094; border: 0px; font-size: 13px;">out</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> </SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"><-</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> colnames</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">(</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">x</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">)[</SPAN><SPAN class="" style="color: #101094; border: 0px; font-size: 13px;">out</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">]</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> </SPAN><SPAN class="" style="color: #101094; border: 0px; font-size: 13px;">out</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"></SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">}</SPAN>And the function findCorrelation_fast, which is the one I am interested in (with optional arguments removed):
<SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">findCorrelation_fast </SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"><-</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> </SPAN><SPAN class="" style="color: #101094; border: 0px; font-size: 13px;">function</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">(</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">x</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">,</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> cutoff </SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">=</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> </SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">.</SPAN><SPAN class="" style="color: #7d2727; border: 0px; font-size: 13px;">90</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">)</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"></SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">{</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> </SPAN><SPAN class="" style="color: #101094; border: 0px; font-size: 13px;">if</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">(</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">any</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">(!</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">complete</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">.</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">cases</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">(</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">x</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">)))</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> stop</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">(</SPAN><SPAN class="" style="color: #7d2727; border: 0px; font-size: 13px;">"The correlation matrix has some missing values."</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">)</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> averageCorr </SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"><-</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> colMeans</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">(</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">abs</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">(</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">x</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">))</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> averageCorr </SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"><-</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> </SPAN><SPAN class="" style="color: #101094; border: 0px; font-size: 13px;">as</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">.</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">numeric</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">(</SPAN><SPAN class="" style="color: #101094; border: 0px; font-size: 13px;">as</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">.</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">factor</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">(</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">averageCorr</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">))</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> x</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">[</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">lower</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">.</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">tri</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">(</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">x</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">,</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> diag </SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">=</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> TRUE</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">)]</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> </SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"><-</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> NA combsAboveCutoff </SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"><-</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> which</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">(</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">abs</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">(</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">x</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">)</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> </SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">></SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> cutoff</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">)</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> colsToCheck </SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"><-</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> ceiling</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">(</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">combsAboveCutoff </SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">/</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> nrow</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">(</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">x</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">))</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> rowsToCheck </SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"><-</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> combsAboveCutoff </SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">%%</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> nrow</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">(</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">x</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">)</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> colsToDiscard </SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"><-</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> averageCorr</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">[</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">colsToCheck</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">]</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> </SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">></SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> averageCorr</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">[</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">rowsToCheck</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">]</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> rowsToDiscard </SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"><-</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> </SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">!</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">colsToDiscard deletecol </SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"><-</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> c</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">(</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">colsToCheck</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">[</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">colsToDiscard</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">],</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> rowsToCheck</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">[</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">rowsToDiscard</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">])</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> deletecol </SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"><-</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> unique</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">(</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">deletecol</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">)</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> deletecol</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">}</SPAN>I am writing a function that emulates the intent of this function in Python 3 with help from pandas. My implementation contains a nested for loop, which I understand is far from the most efficient way to achieve the desired result. The original R function does the job without any looping.
My two questions are:
- Based on my implementation below, is there a Pythonic way to replace the nested
for loop with a vectorised implementation? - Related to (1), the R function
findCorrelation_fast uses the line averageCorr <- as.numeric(as.factor(averageCorr)). This construction seems both very alien to me and also crucial to the success of the loopless R implementation. Can anyone shed any light on what this line is doing? My intuition tells me that it is being incredibly clever and leveraging some unique behaviour of R.
My Python implementation and an example of its usage:
<SPAN class="" style="color: #101094; border: 0px; font-size: 13px;">import</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> numpy </SPAN><SPAN class="" style="color: #101094; border: 0px; font-size: 13px;">as</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> np</SPAN><SPAN class="" style="color: #101094; border: 0px; font-size: 13px;">import</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> pandas </SPAN><SPAN class="" style="color: #101094; border: 0px; font-size: 13px;">as</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> pd </SPAN><SPAN class="" style="color: #858c93; border: 0px; font-size: 13px;"># calculate pair-wise correlations</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"></SPAN><SPAN class="" style="color: #101094; border: 0px; font-size: 13px;">def</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> findCorrelated</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">(</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">corrmat</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">,</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> cutoff </SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">=</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> </SPAN><SPAN class="" style="color: #7d2727; border: 0px; font-size: 13px;">0.8</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">):</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> </SPAN><SPAN class="" style="color: #858c93; border: 0px; font-size: 13px;">### search correlation matrix and identify pairs that if removed would reduce pair-wise correlations</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"></SPAN><SPAN class="" style="color: #858c93; border: 0px; font-size: 13px;"># args:</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> </SPAN><SPAN class="" style="color: #858c93; border: 0px; font-size: 13px;"># corrmat: a correlation matrix</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> </SPAN><SPAN class="" style="color: #858c93; border: 0px; font-size: 13px;"># cutoff: pairwise absolute correlation cutoff</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"></SPAN><SPAN class="" style="color: #858c93; border: 0px; font-size: 13px;"># returns:</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> </SPAN><SPAN class="" style="color: #858c93; border: 0px; font-size: 13px;"># variables to removed</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> </SPAN><SPAN class="" style="color: #101094; border: 0px; font-size: 13px;">if</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">(</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">len</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">(</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">corrmat</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">)</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> </SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">!=</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> len</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">(</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">corrmat</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">.</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">columns</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">))</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> </SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">:</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> </SPAN><SPAN class="" style="color: #101094; border: 0px; font-size: 13px;">return</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> </SPAN><SPAN class="" style="color: #7d2727; border: 0px; font-size: 13px;">'Correlation matrix is not square'</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> averageCorr </SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">=</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> corrmat</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">.</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">abs</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">().</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">mean</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">(</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">axis </SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">=</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> </SPAN><SPAN class="" style="color: #7d2727; border: 0px; font-size: 13px;">1</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">)</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> </SPAN><SPAN class="" style="color: #858c93; border: 0px; font-size: 13px;"># set lower triangle and diagonal of correlation matrix to NA</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> corrmat </SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">=</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> corrmat</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">.</SPAN><SPAN class="" style="color: #101094; border: 0px; font-size: 13px;">where</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">(</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">np</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">.</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">triu</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">(</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">np</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">.</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">ones</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">(</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">corrmat</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">.</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">shape</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">)).</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">astype</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">(</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">np</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">.</SPAN><SPAN class="" style="color: #101094; border: 0px; font-size: 13px;">bool</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">))</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> corrmat</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">.</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">values</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">[[</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">np</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">.</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">arange</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">(</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">len</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">(</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">corrmat</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">))]*</SPAN><SPAN class="" style="color: #7d2727; border: 0px; font-size: 13px;">2</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">]</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> </SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">=</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> </SPAN><SPAN class="" style="color: #101094; border: 0px; font-size: 13px;">None</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> </SPAN><SPAN class="" style="color: #858c93; border: 0px; font-size: 13px;"># where a pairwise correlation is greater than the cutoff value, check whether mean abs.corr of a or b is greater and cut it</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> to_delete </SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">=</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> list</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">()</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> </SPAN><SPAN class="" style="color: #101094; border: 0px; font-size: 13px;">for</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> col </SPAN><SPAN class="" style="color: #101094; border: 0px; font-size: 13px;">in</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> range</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">(</SPAN><SPAN class="" style="color: #7d2727; border: 0px; font-size: 13px;">0</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">,</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> len</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">(</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">corrmat</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">.</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">columns</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">)):</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> </SPAN><SPAN class="" style="color: #101094; border: 0px; font-size: 13px;">for</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> row </SPAN><SPAN class="" style="color: #101094; border: 0px; font-size: 13px;">in</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> range</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">(</SPAN><SPAN class="" style="color: #7d2727; border: 0px; font-size: 13px;">0</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">,</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> len</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">(</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">corrmat</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">)):</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> </SPAN><SPAN class="" style="color: #101094; border: 0px; font-size: 13px;">if</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">(</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">corrmat</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">.</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">iloc</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">[</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">row</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">,</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> col</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">]</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> </SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">></SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> cutoff</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">):</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> </SPAN><SPAN class="" style="color: #101094; border: 0px; font-size: 13px;">if</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">(</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">averageCorr</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">.</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">iloc</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">[</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">row</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">]</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> </SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">></SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> averageCorr</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">.</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">iloc</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">[</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">col</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">]):</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> to_delete</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">.</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">append</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">(</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">row</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">)</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> </SPAN><SPAN class="" style="color: #101094; border: 0px; font-size: 13px;">else</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">:</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> to_delete</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">.</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">append</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">(</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">col</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">)</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> to_delete </SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">=</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> list</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">(</SPAN><SPAN class="" style="color: #101094; border: 0px; font-size: 13px;">set</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">(</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">to_delete</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">))</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> </SPAN><SPAN class="" style="color: #101094; border: 0px; font-size: 13px;">return</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> to_delete </SPAN><SPAN class="" style="color: #858c93; border: 0px; font-size: 13px;"># generate some data</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">df </SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">=</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> pd</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">.</SPAN><SPAN class="" style="color: #2b91af; border: 0px; font-size: 13px;">DataFrame</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">(</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">np</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">.</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">random</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">.</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">randn</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">(</SPAN><SPAN class="" style="color: #7d2727; border: 0px; font-size: 13px;">50</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">,</SPAN><SPAN class="" style="color: #7d2727; border: 0px; font-size: 13px;">25</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">))</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"></SPAN><SPAN class="" style="color: #858c93; border: 0px; font-size: 13px;"># demonstrate usage of function </SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">removeCols </SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">=</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> findCorrelated</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">(</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">df</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">.</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">corr</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">(),</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> cutoff </SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">=</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> </SPAN><SPAN class="" style="color: #7d2727; border: 0px; font-size: 13px;">0.01</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">)</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> </SPAN><SPAN class="" style="color: #858c93; border: 0px; font-size: 13px;">#set v.low cutoff as data is uncorrelated</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"></SPAN><SPAN class="" style="color: #101094; border: 0px; font-size: 13px;">print</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">(</SPAN><SPAN class="" style="color: #7d2727; border: 0px; font-size: 13px;">'Columns to be removed:'</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">)</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"></SPAN><SPAN class="" style="color: #101094; border: 0px; font-size: 13px;">print</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">(</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">removeCols</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">)</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">uncorrelated </SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">=</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> df</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">.</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">drop</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">(</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">df</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">.</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">index</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">[</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">removeCols</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">],</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> axis </SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">=</SPAN><SPAN class="" style="color: #7d2727; border: 0px; font-size: 13px;">1</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">,</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> inplace </SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">=</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"> </SPAN><SPAN class="" style="color: #101094; border: 0px; font-size: 13px;">False</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">)</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"></SPAN><SPAN class="" style="color: #101094; border: 0px; font-size: 13px;">print</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">(</SPAN><SPAN class="" style="color: #7d2727; border: 0px; font-size: 13px;">'Uncorrelated variables:'</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">)</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"></SPAN><SPAN class="" style="color: #101094; border: 0px; font-size: 13px;">print</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">(</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">uncorrelated</SPAN><SPAN class="" style="color: #303336; border: 0px; font-size: 13px;">)
</SPAN>