Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save jcheong0428/ab958eec3fc5a61f618e955ba8d22e49 to your computer and use it in GitHub Desktop.

Select an option

Save jcheong0428/ab958eec3fc5a61f618e955ba8d22e49 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Code comparing align_clusters_groups and scipy.optimize.linear_sum_assignment\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"ExecuteTime": {
"end_time": "2019-06-22T04:09:11.753045Z",
"start_time": "2019-06-22T04:09:11.449902Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Max Correlations using align_clusters_groups\n",
"Iter 0 0.263\n",
"Iter 1 0.291\n",
"Iter 2 0.235\n",
"Iter 3 0.267\n",
"Iter 4 0.255\n",
"Iter 5 0.29\n",
"Iter 6 0.278\n",
"Iter 7 0.281\n",
"Iter 8 0.266\n",
"Iter 9 0.268\n",
"\n",
"\n",
"Max Correlations using scipy.optimize.linear_sum_assignment\n",
"Iter 0 0.312\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/jinhyuncheong/Dropbox/Packages/fnl_tools/fnl_tools/stats.py:190: FutureWarning: '.reindex_axis' is deprecated and will be removed in a future version. Use '.reindex' instead.\n",
" group2_new = group2_new.reindex_axis(sorted(group2_new.columns), axis=1)\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Iter 1 0.312\n",
"Iter 2 0.312\n",
"Iter 3 0.312\n",
"Iter 4 0.312\n",
"Iter 5 0.312\n",
"Iter 6 0.312\n",
"Iter 7 0.312\n",
"Iter 8 0.312\n",
"Iter 9 0.312\n"
]
}
],
"source": [
"cols = 20\n",
"times = 30\n",
"iters = 10\n",
"import numpy as np, pandas as pd\n",
"np.random.seed(100)\n",
"# s1 & s2 are the two groups of states to be aligned together.\n",
"s1 = pd.DataFrame(np.random.rand(times,cols),columns=['s1_'+str(i) for i in range(cols)])\n",
"s2 = pd.DataFrame(np.random.rand(times,cols),columns=['s2_'+str(i) for i in range(cols)])\n",
"\n",
"# Compute Max Correlation with align_clusters_groups\n",
"print(\"Max Correlations using align_clusters_groups\")\n",
"from fnl_tools.stats import align_clusters_groups\n",
"for i in range(iters):\n",
" # Run Align Clusters with s1 and column-shuffled s2\n",
" (group2_new, remapped_columns) = align_clusters_groups(s1, s2[np.random.permutation(s2.columns)])\n",
" # Horizontal stack s1 and reordered s2\n",
" srms = pd.concat([s1,group2_new], axis=1)\n",
" srms_corr = srms.corr()\n",
" # Max correlation is the diagonal of the upper right quadrant. \n",
" print(f\"Iter {i}\", np.round(np.mean(np.diag(srms_corr.iloc[:20, 20:])),3))\n",
"print('\\n')\n",
"# Compute Max Correlation with scipy.optimize.linear_sum_assignment.\n",
"print(\"Max Correlations using scipy.optimize.linear_sum_assignment\")\n",
"import scipy\n",
"for i in range(iters):\n",
" # Horizontal stack s1 and s2 \n",
" srms = pd.concat([s1,s2[np.random.permutation(s2.columns)]], axis=1)\n",
" # Get the \"cost\" matrix by getting correlations of upper right quadrant.\n",
" srms_corr = srms.corr().iloc[:20, 20:]\n",
" # Align using Hungarian algorithm with correlation matrix as \"cost\" by inverting the correlation. \n",
" row_ind, col_ind = scipy.optimize.linear_sum_assignment(-1*srms_corr)\n",
" # Max correlation is the diagonal of the upper right quadrant.\n",
" print(f\"Iter {i}\",np.round(np.mean(np.diag(srms_corr.iloc[row_ind,col_ind])),3))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.5"
},
"toc": {
"nav_menu": {
"height": "45px",
"width": "252px"
},
"navigate_menu": true,
"number_sections": true,
"sideBar": true,
"threshold": 4,
"toc_cell": false,
"toc_section_display": "block",
"toc_window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment