Last active
March 23, 2018 22:36
-
-
Save braunagn/6aab5c7a4e4a20aca8013a1870eecfac to your computer and use it in GitHub Desktop.
Potential answer for SO question: https://stackoverflow.com/questions/49453942/how-to-reshape-numpy-array-in-this-way
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 133, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[['r1_v1_t1' 'r1_v2_t1']\n", | |
" ['r2_v1_t1' 'r2_v2_t1']\n", | |
" ['r3_v1_t1' 'r3_v2_t1']]\n", | |
"\n", | |
" [['r1_v1_t2' 'r1_v2_t2']\n", | |
" ['r2_v1_t2' 'r2_v2_t2']\n", | |
" ['r3_v1_t2' 'r3_v2_t2']]\n", | |
"\n", | |
" [['r1_v1_t3' 'r1_v2_t3']\n", | |
" ['r2_v1_t3' 'r2_v2_t3']\n", | |
" ['r3_v1_t3' 'r3_v2_t3']]\n", | |
"\n", | |
" [['r1_v1_t4' 'r1_v2_t4']\n", | |
" ['r2_v1_t4' 'r2_v2_t4']\n", | |
" ['r3_v1_t4' 'r3_v2_t4']]]\n" | |
] | |
} | |
], | |
"source": [ | |
"import numpy as np\n", | |
"\n", | |
"\n", | |
"a=np.array([[['r1_v1_t1','r1_v1_t2','r1_v1_t3','r1_v1_t4'],\n", | |
" ['r1_v2_t1','r1_v2_t2','r1_v2_t3','r1_v2_t4']],\n", | |
"\n", | |
"[['r2_v1_t1','r2_v1_t2','r2_v1_t3','r2_v1_t4'],\n", | |
" ['r2_v2_t1','r2_v2_t2','r2_v2_t3','r2_v2_t4']],\n", | |
"\n", | |
"[['r3_v1_t1','r3_v1_t2','r3_v1_t3','r3_v1_t4'],\n", | |
" ['r3_v2_t1','r3_v2_t2','r3_v2_t3','r3_v2_t4']]\n", | |
" ])\n", | |
"\n", | |
"# 4 times, each has 3 repeats, each has 2 variables\n", | |
"b=np.array([[['r1_v1_t1','r1_v2_t1'],['r2_v1_t1','r2_v2_t1'],['r3_v1_t1','r3_v2_t1']],\n", | |
"[['r1_v1_t2','r1_v2_t2'],['r2_v1_t2','r2_v2_t2'],['r3_v1_t2','r3_v2_t2']],\n", | |
"[['r1_v1_t3','r1_v2_t3'],['r2_v1_t3','r2_v2_t3'],['r3_v1_t3','r3_v2_t3']],\n", | |
"[['r1_v1_t4','r1_v2_t4'],['r2_v1_t4','r2_v2_t4'],['r3_v1_t4','r3_v2_t4']]])\n", | |
"\n", | |
"\n", | |
"new_a = np.swapaxes( np.swapaxes(a,0,2) ,1,2)\n", | |
"\n", | |
"print new_a" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 128, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"True\n" | |
] | |
} | |
], | |
"source": [ | |
"print (new_a == b).all() # test all elems are same between new_a and original b" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 2", | |
"language": "python", | |
"name": "python2" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 2 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython2", | |
"version": "2.7.14" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment