split 3D numpy to 3 diffrent arrays - python

I have numpy.array pf shape (64 , 64 , 64)
I would like to split it on to 3 variables ,so
x.shape ==> (64)
y.shape ==> (64)
z.shape ==> (64)
as each dim represent voxels coordinate (x,y,z) , I tried use dsplit() but no luck. any suggestion?

What you're looking for is probably transpose + ravel:
X = np.arange(27).reshape((3,3,3))
>>> X
([[[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8]],
[[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]],
[[18, 19, 20],
[21, 22, 23],
[24, 25, 26]]])
Your x,y,z:
>>> X.transpose((0,1,2)).ravel()
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23, 24, 25, 26])
>>> X.transpose((1,2,0)).ravel()
array([ 0, 9, 18, 1, 10, 19, 2, 11, 20, 3, 12, 21, 4, 13, 22, 5, 14,
23, 6, 15, 24, 7, 16, 25, 8, 17, 26])
>>> X.transpose((2,0,1)).ravel()
array([ 0, 3, 6, 9, 12, 15, 18, 21, 24, 1, 4, 7, 10, 13, 16, 19, 22,
25, 2, 5, 8, 11, 14, 17, 20, 23, 26])

Related

matplotlib quiver() displaying double arrows

The code below is producing double arrows. This is most noticeable in the center and along the bottom row.
Am I missing something or is this a bug of some sort? The Googlebox has yielded nothing helpful.
X = [[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22]]
Y = [[ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1],
[ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1],
[ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2],
[ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3],
[ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4],
[ 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5],
[ 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6],
[ 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7],
[ 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8],
[ 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
9, 9, 9, 9, 9, 9, 9, 9],
[10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10],
[11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
11, 11, 11, 11, 11, 11, 11, 11],
[12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
12, 12, 12, 12, 12, 12, 12, 12],
[12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
12, 12, 12, 12, 12, 12, 12, 12],
[13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
13, 13, 13, 13, 13, 13, 13, 13],
[14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
14, 14, 14, 14, 14, 14, 14, 14],
[15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 15, 15, 15, 15],
[16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
16, 16, 16, 16, 16, 16, 16, 16],
[17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
17, 17, 17, 17, 17, 17, 17, 17],
[18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
18, 18, 18, 18, 18, 18, 18, 18],
[19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
19, 19, 19, 19, 19, 19, 19, 19],
[20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
20, 20, 20, 20, 20, 20, 20, 20],
[21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
21, 21, 21, 21, 21, 21, 21, 21],
[22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22,
22, 22, 22, 22, 22, 22, 22, 22],
[23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23,
23, 23, 23, 23, 23, 23, 23, 23]]
U = [[ 5.91106782e-01, 6.22366562e-01, 6.49723913e-01,
6.58423221e-01, 6.34788082e-01, 5.64001424e-01,
4.29552877e-01, 2.26181450e-01, -1.45749370e-02,
-2.33836090e-01, -3.97062982e-01, -5.07288787e-01,
-5.80344621e-01, -6.28652118e-01, -6.56798746e-01,
-6.59563028e-01, -6.18178096e-01, -5.00722193e-01,
-2.93582966e-01, -5.16521582e-02, 1.45799368e-01,
2.69658133e-01, 3.26500153e-01, 3.26768709e-01],
[ 5.77152607e-01, 5.82746773e-01, 5.76183972e-01,
5.43266956e-01, 4.66312191e-01, 3.24661378e-01,
1.07385088e-01, -1.54908642e-01, -3.91341641e-01,
-5.56849441e-01, -6.55288824e-01, -7.07393105e-01,
-7.30393353e-01, -7.34785105e-01, -7.24202206e-01,
-6.92703347e-01, -6.16664334e-01, -4.51489078e-01,
-1.96144234e-01, 3.89095593e-02, 1.80222779e-01,
2.38107484e-01, 2.33390450e-01, 1.75753839e-01],
[ 5.55201554e-01, 5.36916267e-01, 4.95331092e-01,
4.14297782e-01, 2.72441471e-01, 5.30267270e-02,
-2.23958353e-01, -4.85686407e-01, -6.72063827e-01,
-7.80930667e-01, -8.34052999e-01, -8.49154520e-01,
-8.36753045e-01, -8.02912499e-01, -7.48984464e-01,
-6.66861836e-01, -5.28032125e-01, -2.83307017e-01,
2.22999217e-02, 2.17035214e-01, 2.78107345e-01,
2.57213982e-01, 1.80414526e-01, 5.58040480e-02],
[ 5.30514869e-01, 4.88045325e-01, 4.08507605e-01,
2.73305715e-01, 6.32261747e-02, -2.15499555e-01,
-5.00468423e-01, -7.16424820e-01, -8.46362641e-01,
-9.11763344e-01, -9.32463829e-01, -9.17913774e-01,
-8.71699781e-01, -7.94959450e-01, -6.84618047e-01,
-5.27977847e-01, -3.00601184e-01, 3.24725025e-03,
2.84572480e-01, 4.15678719e-01, 4.08817699e-01,
3.17903989e-01, 1.68926905e-01, -2.47734503e-02],
[ 5.02548139e-01, 4.33179379e-01, 3.10308116e-01,
1.15832201e-01, -1.53873192e-01, -4.53740654e-01,
-7.02597717e-01, -8.60992569e-01, -9.43881451e-01,
-9.75369646e-01, -9.68057700e-01, -9.24950942e-01,
-8.45413757e-01, -7.27664488e-01, -5.66705523e-01,
-3.53336189e-01, -8.54415234e-02, 2.02537484e-01,
4.30399404e-01, 5.35069052e-01, 5.14397103e-01,
3.91608994e-01, 1.87450108e-01, -6.90256334e-02],
[ 4.65201575e-01, 3.63695264e-01, 1.90407737e-01,
-6.27986111e-02, -3.66107539e-01, -6.43527995e-01,
-8.36008710e-01, -9.43058071e-01, -9.89396085e-01,
-9.93329532e-01, -9.61559805e-01, -8.93929627e-01,
-7.88224158e-01, -6.41454775e-01, -4.49066265e-01,
-2.09439702e-01, 6.10838713e-02, 3.17493844e-01,
5.06540308e-01, 5.97919655e-01, 5.83094325e-01,
4.57218933e-01, 2.21129548e-01, -8.56001665e-02],
[ 4.07605489e-01, 2.67483222e-01, 4.05687053e-02,
-2.54052862e-01, -5.49121397e-01, -7.74824152e-01,
-9.12683654e-01, -9.80602627e-01, -9.99708951e-01,
-9.81982007e-01, -9.30930539e-01, -8.45440583e-01,
-7.22549396e-01, -5.57873670e-01, -3.47081454e-01,
-9.50528305e-02, 1.70316784e-01, 4.02869279e-01,
5.66564552e-01, 6.47406192e-01, 6.37165072e-01,
5.16241932e-01, 2.63738110e-01, -8.10456144e-02],
[ 3.16982526e-01, 1.36224084e-01, -1.31317283e-01,
-4.28869103e-01, -6.80165940e-01, -8.50494619e-01,
-9.46930042e-01, -9.87780174e-01, -9.87413002e-01,
-9.53497923e-01, -8.88237499e-01, -7.90275139e-01,
-6.55616984e-01, -4.77864944e-01, -2.52458286e-01,
9.81222811e-03, 2.71633461e-01, 4.87237886e-01,
6.32706314e-01, 7.04459838e-01, 6.96319353e-01,
5.81475147e-01, 3.21122831e-01, -5.36729232e-02],
[ 1.89851174e-01, -1.85511352e-02, -2.92612142e-01,
-5.56206782e-01, -7.55569048e-01, -8.83561174e-01,
-9.52641527e-01, -9.75602026e-01, -9.61454612e-01,
-9.15325308e-01, -8.38796591e-01, -7.30116952e-01,
-5.83859882e-01, -3.91508312e-01, -1.49024508e-01,
1.25224816e-01, 3.84250312e-01, 5.84434393e-01,
7.13492616e-01, 7.76382012e-01, 7.69585148e-01,
6.63495229e-01, 4.05530281e-01, 8.00655892e-03],
[ 4.93393011e-02, -1.59077638e-01, -4.07446556e-01,
-6.27159359e-01, -7.86840781e-01, -8.87988595e-01,
-9.40149757e-01, -9.51231448e-01, -9.26802697e-01,
-8.70250066e-01, -7.82391229e-01, -6.60618012e-01,
-4.97916636e-01, -2.84557428e-01, -2.01313492e-02,
2.65144709e-01, 5.15478531e-01, 6.95092413e-01,
8.05054050e-01, 8.57357453e-01, 8.51220724e-01,
7.58939184e-01, 5.22126402e-01, 1.20841224e-01],
[-6.08161695e-02, -2.48600589e-01, -4.63433277e-01,
-6.50364713e-01, -7.86735731e-01, -8.73048406e-01,
-9.15192518e-01, -9.18037801e-01, -8.84901395e-01,
-8.17468539e-01, -7.15217876e-01, -5.74265632e-01,
-3.86590763e-01, -1.44298331e-01, 1.42640838e-01,
4.27841719e-01, 6.54596037e-01, 8.04151106e-01,
8.90457558e-01, 9.29136623e-01, 9.21288722e-01,
8.45538031e-01, 6.51467861e-01, 2.89354206e-01],
[-1.11855856e-01, -2.76262916e-01, -4.66494984e-01,
-6.36195917e-01, -7.62707033e-01, -8.42791922e-01,
-8.79618811e-01, -8.76344309e-01, -8.34510258e-01,
-7.53704299e-01, -6.31437753e-01, -4.62760383e-01,
-2.41283252e-01, 3.19831190e-02, 3.28392442e-01,
5.90402526e-01, 7.76991386e-01, 8.90488023e-01,
9.51679598e-01, 9.75365138e-01, 9.63233139e-01,
9.01562554e-01, 7.57051594e-01, 4.78793525e-01],
[-1.02763752e-01, -2.49208456e-01, -4.25698141e-01,
-5.90445663e-01, -7.17037110e-01, -7.97411218e-01,
-8.32440059e-01, -8.24121526e-01, -7.72259450e-01,
-6.73888301e-01, -5.24476588e-01, -3.20200623e-01,
-6.29603397e-02, 2.27897175e-01, 5.06403936e-01,
7.23466314e-01, 8.64328407e-01, 9.44675955e-01,
9.84485357e-01, 9.95247332e-01, 9.78583745e-01,
9.26839981e-01, 8.22017325e-01, 6.32198023e-01],
[-4.76297792e-02, -1.80235400e-01, -3.48835361e-01,
-5.15498990e-01, -6.48860867e-01, -7.34607790e-01,
-7.70573611e-01, -7.57371172e-01, -6.92888223e-01,
-5.71674412e-01, -3.88941513e-01, -1.47948226e-01,
1.31928638e-01, 4.12166625e-01, 6.47847253e-01,
8.14343231e-01, 9.16397002e-01, 9.71963635e-01,
9.96490008e-01, 9.98129614e-01, 9.78410399e-01,
9.33624383e-01, 8.55392368e-01, 7.29066548e-01],
[ 3.44453327e-02, -8.45500183e-02, -2.44567678e-01,
-4.13390951e-01, -5.55986626e-01, -6.50278759e-01,
-6.88905719e-01, -6.69887784e-01, -5.89049719e-01,
-4.40034407e-01, -2.23153205e-01, 4.24368019e-02,
3.17706147e-01, 5.60575371e-01, 7.45372080e-01,
8.69074149e-01, 9.43218293e-01, 9.82237250e-01,
9.96859104e-01, 9.92775230e-01, 9.71548772e-01,
9.31926675e-01, 8.70952635e-01, 7.84147337e-01],
[ 1.24531829e-01, 2.14951720e-02, -1.24142269e-01,
-2.88285785e-01, -4.36332137e-01, -5.38600278e-01,
-5.79503157e-01, -5.52137571e-01, -4.50556205e-01,
-2.72020914e-01, -3.08257107e-02, 2.33881288e-01,
4.76396240e-01, 6.69624907e-01, 8.08474102e-01,
9.00042790e-01, 9.55065954e-01, 9.83283570e-01,
9.91629328e-01, 9.84084754e-01, 9.62320925e-01,
9.26691302e-01, 8.77415997e-01, 8.15482847e-01],
[ 2.08997618e-01, 1.24559020e-01, 8.48684407e-04,
-1.46598137e-01, -2.88661918e-01, -3.91618296e-01,
-4.29961569e-01, -3.89309376e-01, -2.63943288e-01,
-6.30136416e-02, 1.78668276e-01, 4.12249396e-01,
6.04758495e-01, 7.48714864e-01, 8.50152685e-01,
9.17783980e-01, 9.59112226e-01, 9.79804775e-01,
9.83901943e-01, 9.74114572e-01, 9.52212374e-01,
9.19706766e-01, 8.78912089e-01, 8.33903997e-01],
[ 2.82249691e-01, 2.18162184e-01, 1.22747194e-01,
5.37550368e-03, -1.12692673e-01, -1.99604825e-01,
-2.22963823e-01, -1.62177004e-01, -1.79584651e-02,
1.81399471e-01, 3.89284568e-01, 5.68595908e-01,
7.06857534e-01, 8.07907267e-01, 8.79716132e-01,
9.28814854e-01, 9.59433518e-01, 9.74181247e-01,
9.74926863e-01, 9.63345918e-01, 9.41247109e-01,
9.11068122e-01, 8.76727946e-01, 8.44239721e-01],
[ 3.46009504e-01, 3.03334542e-01, 2.40609727e-01,
1.64792336e-01, 9.10650256e-02, 4.46918062e-02,
5.47680721e-02, 1.36260477e-01, 2.74393229e-01,
4.32734135e-01, 5.78349795e-01, 6.96327399e-01,
7.86097123e-01, 8.52735284e-01, 9.01501863e-01,
9.35893510e-01, 9.57589073e-01, 9.67107013e-01,
9.64733890e-01, 9.51264330e-01, 9.28488288e-01,
8.99692809e-01, 8.70252069e-01, 8.47561147e-01],
[ 4.06955125e-01, 3.86836289e-01, 3.60008319e-01,
3.33800629e-01, 3.19793639e-01, 3.32294319e-01,
3.80910459e-01, 4.60529276e-01, 5.54085368e-01,
6.45433976e-01, 7.25560042e-01, 7.91585217e-01,
8.44113209e-01, 8.85113897e-01, 9.16480279e-01,
9.39239758e-01, 9.53365969e-01, 9.58045905e-01,
9.52421824e-01, 9.36509635e-01, 9.12084965e-01,
8.83417478e-01, 8.57475919e-01, 8.42768229e-01],
[ 4.75102487e-01, 4.79444876e-01, 4.91103049e-01,
5.17028392e-01, 5.61580652e-01, 6.19364889e-01,
6.75394533e-01, 7.20462603e-01, 7.57762648e-01,
7.92301360e-01, 8.25331968e-01, 8.55937836e-01,
8.83019438e-01, 9.05962858e-01, 9.24460078e-01,
9.38025098e-01, 9.45636106e-01, 9.45638264e-01,
9.36251692e-01, 9.16741704e-01, 8.89028586e-01,
8.58882429e-01, 8.35359426e-01, 8.27878351e-01],
[ 5.62693753e-01, 5.93592734e-01, 6.42845488e-01,
7.09881932e-01, 7.82737342e-01, 8.39735853e-01,
8.68496463e-01, 8.76464004e-01, 8.77560033e-01,
8.80145097e-01, 8.86486243e-01, 8.95793855e-01,
9.06385360e-01, 9.16670844e-01, 9.25375659e-01,
9.31289596e-01, 9.32857010e-01, 9.27783629e-01,
9.13322352e-01, 8.87929386e-01, 8.54220866e-01,
8.20792210e-01, 7.99707058e-01, 8.00713493e-01],
[ 6.75223956e-01, 7.28299137e-01, 7.98132045e-01,
8.70828514e-01, 9.28100696e-01, 9.58357163e-01,
9.63664076e-01, 9.54511170e-01, 9.41109289e-01,
9.29563623e-01, 9.22166117e-01, 9.18823138e-01,
9.18280235e-01, 9.18945445e-01, 9.19301773e-01,
9.17852346e-01, 9.12687865e-01, 9.00755102e-01,
8.77918913e-01, 8.41778780e-01, 7.97438172e-01,
7.59412209e-01, 7.43766496e-01, 7.58426057e-01],
[ 7.48028585e-01, 8.00967465e-01, 8.60639839e-01,
9.17314355e-01, 9.62374525e-01, 9.89077372e-01,
9.96239777e-01, 9.88737117e-01, 9.73767736e-01,
9.57273913e-01, 9.42647501e-01, 9.30980153e-01,
9.21800247e-01, 9.13829662e-01, 9.05516974e-01,
8.95103386e-01, 8.80063037e-01, 8.55819040e-01,
8.15680432e-01, 7.57286053e-01, 6.94667063e-01,
6.54674360e-01, 6.55242773e-01, 6.95951505e-01],
[ 5.88142228e-01, 6.56524283e-01, 7.39388262e-01,
8.26540072e-01, 9.05382649e-01, 9.63084972e-01,
9.93428420e-01, 9.99593348e-01, 9.90003898e-01,
9.72940215e-01, 9.53944608e-01, 9.35599844e-01,
9.18217361e-01, 9.00665492e-01, 8.80954002e-01,
8.56210996e-01, 8.21553193e-01, 7.67628947e-01,
6.82523479e-01, 5.74145986e-01, 4.89341942e-01,
4.69017002e-01, 5.14459717e-01, 6.05048760e-01]]
V = [[-7.64071106e-01, -7.61939824e-01, -7.56999777e-01,
fig, ax = plt.subplots(figsize=(6, 6), dpi=300);
ax.quiver(X, Y, U, V, pivot='middle');
ax.set_aspect('equal');
Output:
I've tried the above. I expect single arrows as usual.
I don't know what additional details to add to get rid of the
Didn't you ask for exactly that, repeating values 1 and 12?
#(...)
X = [[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
#(...)
Y = [[ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1],
[ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1],
#(...)
U = [[ 5.91106782e-01, 6.22366562e-01, 6.49723913e-01, (...)
[ 5.77152607e-01, 5.82746773e-01, 5.76183972e-01, (...)
You even have a quadruple arrow at coordinate (1,1). Just pair X and Y, and see that X=1 Y=1 occurs 4 times! But values of U in those 4 occurences of X=1 Y=1 are all different. So those 4 arrows that starts for (1,1) are not exactly identical.
So does X=11 Y=1.
X=2 Y=1 occurs twice.
Etc.
You said "especially in the center". No, not really. It occurs on line Y=1, on column X=1, and, indeed on line Y=12 and column X=11 that both pass not far from the center.

How to connect many short eulerian cycles into only a few longer ones?

I want to connect eulerian cycles into longer ones without exceed a value.
So, I have this eulerian cycles and their length in a list. The maximal length of a cycle can be for example 500. The length of all cycles added up is 6176.778566350282. By connecting them cleverly together there could be probably only 13 or 14 cycles. But I don't really know how I could do that. I tried to just add one cycle to another but there I got 21 cycles out. The problem is that if you have a cycle of these numbers for example: [8, 21, 9, 22, 8, 23, 9, 24, 8] and you want to integrate this cycle [10, 11, 12, 10] it will not work because in the first cycle there is no edge of the number 10. I just started then a new cycle with this numbers [10, 11, 12, 10] and saved [8, 21, 9, 22, 8, 23, 9, 24, 8] as one of the 21 result cycles. But with this method I don't really get a good result. What would be a cleverer way to solve this problem?
This is an example how the list of the short eulerian cycles looks like:
[([0, 1, 2, 0], 36.36630772776802), ([0, 3, 1, 4, 0], 93.83277865587606), ([0, 5, 1, 6, 0], 45.79353710664728), ([0, 7, 1, 8, 0], 49.60782827778143), ([0, 9, 1, 10, 0], 73.2674533926481), ([0, 11, 1, 12, 0], 75.52124688926921), ([0, 13, 1, 14, 0], 57.88021234723078), ([0, 15, 1, 16, 0], 62.21469065955568), ([0, 17, 1, 18, 0], 81.43809748917617), ([0, 19, 1, 20, 0], 98.88867905572438), ([0, 21, 1, 22, 0], 95.3596513800762), ([0, 23, 1, 24, 0], 116.15359042770964), ([2, 3, 4, 2], 49.106297391220245), ([2, 5, 3, 6, 2], 71.5422470782724), ([2, 7, 3, 8, 2], 50.237654764168), ([2, 9, 3, 10, 2], 71.36355688043689), ([2, 11, 3, 12, 2], 44.474596239420634), ([2, 13, 3, 14, 2], 103.42527218232905), ([2, 15, 3, 16, 2], 65.92444557445982), ([2, 17, 3, 18, 2], 83.30561323888043), ([2, 19, 3, 20, 2], 144.20150278029047), ([2, 21, 3, 22, 2], 131.70030082856), ([2, 23, 3, 24, 2], 141.63032737825358), ([4, 5, 6, 4], 42.10300780814433), ([4, 7, 5, 8, 4], 88.13162862262575), ([4, 9, 5, 10, 4], 29.40312423743285), ([4,
11, 5, 12, 4], 35.06685249446684), ([4, 13, 5, 14, 4], 83.54113932583394), ([4, 15, 5, 16, 4], 57.669814210895076), ([4, 17, 5, 18, 4], 85.16088821443248), ([4, 19, 5, 20, 4], 115.83839679838714), ([4, 21, 5, 22, 4], 96.32509817470469), ([4, 23, 5, 24, 4], 95.72504474795447), ([6, 7, 8, 6], 39.680511478789455), ([6, 9, 7, 10, 6], 78.55998969220359), ([6, 11, 7, 12, 6], 75.38181527864062), ([6, 13, 7, 14, 6], 65.59514045044449), ([6, 15, 7, 16, 6], 64.00893982862813), ([6, 17, 7, 18, 6], 82.99423226082924), ([6, 19, 7, 20, 6], 107.80803412093549), ([6, 21, 7, 22, 6], 104.34384551877056), ([6, 23, 7, 24, 6], 125.5684717784), ([8, 9, 10, 8], 52.130784276071026), ([8, 11, 9, 12, 8], 60.084249983353345), ([8, 13, 9, 14,
8], 80.8264707041123), ([8, 15, 9, 16, 8], 56.067658306081576), ([8, 17, 9, 18, 8], 87.79739969269264), ([8, 19, 9, 20, 8], 115.04095207094785),
([8, 21, 9, 22, 8], 100.28892183336735), ([8, 23, 9, 24, 8], 107.98171312085222), ([10, 11, 12, 10], 18.073592581964586), ([10, 13, 11, 14, 10],
86.59048377734861), ([10, 15, 11, 16, 10], 53.62896051047471), ([10, 17, 11, 18, 10], 79.42707393175432), ([10, 19, 11, 20, 10], 121.75438335508098), ([10, 21, 11, 22, 10], 103.13320830479722), ([10, 23, 11, 24, 10], 104.67092453129686), ([12, 13, 14, 12], 65.01056040398879), ([12, 15, 13, 16, 12], 73.92038351218434), ([12, 17, 13, 18, 12], 75.85986620162797), ([12, 19, 13, 20, 12], 99.9668143111241), ([12, 21, 13, 22, 12], 97.01425784207544), ([12, 23, 13, 24, 12], 113.28618776429398), ([14, 15, 16, 14], 53.12806382231952), ([14, 17, 15, 18, 14], 83.32318283097464), ([14,
19, 15, 20, 14], 59.489711796339975), ([14, 21, 15, 22, 14], 49.93204117686305), ([14, 23, 15, 24, 14], 59.39628730132421), ([16, 17, 18, 16], 76.30230372794964), ([16, 19, 17, 20, 16], 151.38369644764225), ([16, 21, 17, 22, 16], 137.27131752575687), ([16, 23, 17, 24, 16], 146.11467181532439), ([18, 19, 20, 18], 28.731124011957917), ([18, 21, 19, 22, 18], 51.78367537918862), ([18, 23, 19, 24, 18], 86.45013419422762), ([20, 21, 22,
20], 39.010097887844154), ([20, 23, 21, 24, 20], 63.48159687540681), ([22, 23, 24, 22], 22.283951753399037)]
I designed an elaborate branch-and-price scheme and then realized that it probably wouldn’t work well. Here’s a much simpler local search that achieves 14 cycles on your sample input.
from collections import defaultdict
from itertools import combinations
import random
# pip3 install networkx if necessary.
import networkx as nx
# The input consists of the variables maximum_length and cycles.
maximum_length = 500
cycles = [
([0, 1, 2, 0], 36.36630772776802),
([0, 3, 1, 4, 0], 93.83277865587606),
([0, 5, 1, 6, 0], 45.79353710664728),
([0, 7, 1, 8, 0], 49.60782827778143),
([0, 9, 1, 10, 0], 73.2674533926481),
([0, 11, 1, 12, 0], 75.52124688926921),
([0, 13, 1, 14, 0], 57.88021234723078),
([0, 15, 1, 16, 0], 62.21469065955568),
([0, 17, 1, 18, 0], 81.43809748917617),
([0, 19, 1, 20, 0], 98.88867905572438),
([0, 21, 1, 22, 0], 95.3596513800762),
([0, 23, 1, 24, 0], 116.15359042770964),
([2, 3, 4, 2], 49.106297391220245),
([2, 5, 3, 6, 2], 71.5422470782724),
([2, 7, 3, 8, 2], 50.237654764168),
([2, 9, 3, 10, 2], 71.36355688043689),
([2, 11, 3, 12, 2], 44.474596239420634),
([2, 13, 3, 14, 2], 103.42527218232905),
([2, 15, 3, 16, 2], 65.92444557445982),
([2, 17, 3, 18, 2], 83.30561323888043),
([2, 19, 3, 20, 2], 144.20150278029047),
([2, 21, 3, 22, 2], 131.70030082856),
([2, 23, 3, 24, 2], 141.63032737825358),
([4, 5, 6, 4], 42.10300780814433),
([4, 7, 5, 8, 4], 88.13162862262575),
([4, 9, 5, 10, 4], 29.40312423743285),
([4, 11, 5, 12, 4], 35.06685249446684),
([4, 13, 5, 14, 4], 83.54113932583394),
([4, 15, 5, 16, 4], 57.669814210895076),
([4, 17, 5, 18, 4], 85.16088821443248),
([4, 19, 5, 20, 4], 115.83839679838714),
([4, 21, 5, 22, 4], 96.32509817470469),
([4, 23, 5, 24, 4], 95.72504474795447),
([6, 7, 8, 6], 39.680511478789455),
([6, 9, 7, 10, 6], 78.55998969220359),
([6, 11, 7, 12, 6], 75.38181527864062),
([6, 13, 7, 14, 6], 65.59514045044449),
([6, 15, 7, 16, 6], 64.00893982862813),
([6, 17, 7, 18, 6], 82.99423226082924),
([6, 19, 7, 20, 6], 107.80803412093549),
([6, 21, 7, 22, 6], 104.34384551877056),
([6, 23, 7, 24, 6], 125.5684717784),
([8, 9, 10, 8], 52.130784276071026),
([8, 11, 9, 12, 8], 60.084249983353345),
([8, 13, 9, 14, 8], 80.8264707041123),
([8, 15, 9, 16, 8], 56.067658306081576),
([8, 17, 9, 18, 8], 87.79739969269264),
([8, 19, 9, 20, 8], 115.04095207094785),
([8, 21, 9, 22, 8], 100.28892183336735),
([8, 23, 9, 24, 8], 107.98171312085222),
([10, 11, 12, 10], 18.073592581964586),
([10, 13, 11, 14, 10], 86.59048377734861),
([10, 15, 11, 16, 10], 53.62896051047471),
([10, 17, 11, 18, 10], 79.42707393175432),
([10, 19, 11, 20, 10], 121.75438335508098),
([10, 21, 11, 22, 10], 103.13320830479722),
([10, 23, 11, 24, 10], 104.67092453129686),
([12, 13, 14, 12], 65.01056040398879),
([12, 15, 13, 16, 12], 73.92038351218434),
([12, 17, 13, 18, 12], 75.85986620162797),
([12, 19, 13, 20, 12], 99.9668143111241),
([12, 21, 13, 22, 12], 97.01425784207544),
([12, 23, 13, 24, 12], 113.28618776429398),
([14, 15, 16, 14], 53.12806382231952),
([14, 17, 15, 18, 14], 83.32318283097464),
([14, 19, 15, 20, 14], 59.489711796339975),
([14, 21, 15, 22, 14], 49.93204117686305),
([14, 23, 15, 24, 14], 59.39628730132421),
([16, 17, 18, 16], 76.30230372794964),
([16, 19, 17, 20, 16], 151.38369644764225),
([16, 21, 17, 22, 16], 137.27131752575687),
([16, 23, 17, 24, 16], 146.11467181532439),
([18, 19, 20, 18], 28.731124011957917),
([18, 21, 19, 22, 18], 51.78367537918862),
([18, 23, 19, 24, 18], 86.45013419422762),
([20, 21, 22, 20], 39.010097887844154),
([20, 23, 21, 24, 20], 63.48159687540681),
([22, 23, 24, 22], 22.283951753399037),
]
for cycle, length in cycles:
assert cycle[0] == cycle[-1]
assert 0 <= length <= maximum_length
# Two cycles can be merged if and only if there exists a vertex that they have
# in common. Compute the graph where each cycle is a node and each pair of
# cycles that can be merged is an edge. A set of cycles can be merged if and
# only if the total length does not exceed the maximum and the corresponding set
# of nodes induces a connected subgraph.
inverted_index = defaultdict(list)
for i, (cycle, length) in enumerate(cycles):
for v in set(cycle):
inverted_index[v].append(i)
cycle_graph = nx.Graph()
for i, (cycle, length) in enumerate(cycles):
cycle_graph.add_node(i, length=length)
for posting_list in inverted_index.values():
for e in combinations(posting_list, 2):
cycle_graph.add_edge(*e)
lengths = [round(length * 2**40) / 2**40 for (cycle, length) in cycles]
# We want to find the smallest partition of cycles into mergeable parts. This
# code implements a greedy local search. Initialize the partition where every
# cycle is in its own part. For some number of steps, move one cycle to another
# part, respecting the connectivity constraint.
def make_part_graph(part):
part_graph = cycle_graph.subgraph(part)
return nx.Graph(
part_graph,
can_move=set(part_graph.nodes()) - set(nx.articulation_points(part_graph)),
length=sum(lengths[i] for i in part_graph.nodes()),
)
def merge_cycles(indexes):
g = nx.DiGraph()
for i in indexes:
cycle, length = cycles[i]
for j in range(1, len(cycle)):
g.add_edge(cycle[j - 1], cycle[j])
cycle = []
for u, v in nx.eulerian_circuit(g):
if not cycle:
cycle.append(u)
cycle.append(v)
return cycle, sum(lengths[i] for i in indexes)
labels = list(range(len(cycles)))
cycle_subgraphs = {i: make_part_graph({i}) for i in range(len(cycles))}
for step in range(10000):
moves = []
for tail, cycle_subgraph in cycle_subgraphs.items():
for i in cycle_subgraph.graph["can_move"]:
for j in cycle_graph.neighbors(i):
head = labels[j]
if (
head != tail
and cycle_subgraphs[head].graph["length"] + lengths[i]
<= maximum_length
):
moves.append((i, tail, head))
i, tail, head = random.choice(moves)
labels[i] = head
cycle_subgraphs[tail] = make_part_graph(set(cycle_subgraphs[tail].nodes()) - {i})
cycle_subgraphs[head] = make_part_graph(set(cycle_subgraphs[head].nodes()) | {i})
for cycle_subgraph in cycle_subgraphs.values():
part = sorted(cycle_subgraph.nodes())
if not part:
continue
print(*merge_cycles(part))
Output:
[0, 23, 11, 24, 8, 23, 9, 3, 10, 2, 9, 24, 10, 23, 1, 24, 0, 21, 1, 22, 0] 495.5294363403709
[0, 19, 17, 20, 16, 19, 22, 18, 21, 3, 22, 2, 21, 19, 1, 20, 0, 5, 1, 6, 0] 479.5498888177626
[6, 23, 17, 24, 16, 10, 15, 11, 14, 10, 13, 11, 16, 23, 7, 24, 6] 411.90258788154733
[0, 9, 22, 8, 21, 9, 18, 16, 17, 18, 8, 17, 9, 16, 8, 15, 9, 12, 8, 6, 7, 8, 11, 9, 1, 10, 0] 493.488498414883
[2, 5, 3, 6, 19, 9, 10, 8, 9, 20, 23, 21, 24, 20, 8, 19, 7, 20, 6, 2] 410.00361442163285
[0, 7, 22, 6, 21, 7, 16, 12, 15, 20, 14, 19, 11, 20, 10, 19, 15, 13, 16, 6, 15, 7, 1, 8, 0] 473.1250922887857
[0, 13, 1, 14, 0, 11, 22, 10, 21, 11, 5, 12, 4, 11, 3, 12, 2, 11, 1, 12, 0, 1, 2, 0] 352.442424002953
[0, 3, 1, 4, 23, 15, 24, 14, 23, 5, 24, 4, 7, 5, 8, 4, 0] 337.0857393277802
[4, 21, 13, 7, 14, 8, 13, 9, 14, 6, 13, 22, 23, 24, 22, 20, 21, 22, 12, 21, 5, 22, 4, 9, 5, 10, 4] 430.458141050015
[2, 23, 3, 24, 2, 13, 3, 14, 2, 7, 12, 6, 11, 7, 10, 6, 4, 5, 6, 9, 7, 3, 8, 2] 491.33806710373847
[0, 15, 22, 14, 21, 15, 5, 20, 12, 10, 11, 12, 19, 13, 20, 4, 19, 5, 16, 4, 15, 1, 16, 0] 403.69534973878945
[0, 17, 22, 16, 21, 17, 11, 18, 19, 3, 20, 2, 19, 20, 18, 10, 17, 1, 18, 0] 471.0691157389365
[2, 15, 16, 14, 4, 13, 24, 18, 23, 19, 24, 12, 23, 13, 5, 14, 15, 3, 16, 2, 3, 4, 2] 451.43626807235523
[2, 17, 15, 18, 14, 12, 13, 14, 17, 13, 18, 12, 17, 7, 18, 6, 17, 5, 18, 4, 17, 3, 18, 2] 475.65434315073435

Struggling to reshape an odd array

I am struggling with a problem from my python class that has been assigned where I have to create a 1D array with the arange function from 0 to 29. Then reshape it into:
An array of rank 2 of the appropriate size.
An array of rank 3 of the appropriate size.
I am able to create the array with z = np.arange(29), however I am unable to reshape it to be a 2d/3d array.
z = np.arange(29)
print(z.shape)
z = z.reshape(2,14)
But then I get an error saying:
ValueError:cannot reshape array of size 29 into shape (2,14)
While the specification is a bit ambiguous, I suspect they want you to generate 30 numbers that include 0 and 29:
In [73]: arr = np.arange(30)
In [74]: arr
Out[74]:
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29])
In [75]: arr.shape
Out[75]: (30,)
There many ways you can reshape this, all of which assume 30 values:
In [76]: arr.reshape(2,15)
Out[76]:
array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14],
[15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]])
In [77]: arr.reshape(3,10)
Out[77]:
array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
[20, 21, 22, 23, 24, 25, 26, 27, 28, 29]])
In [79]: arr.reshape(2,3,5)
Out[79]:
array([[[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14]],
[[15, 16, 17, 18, 19],
[20, 21, 22, 23, 24],
[25, 26, 27, 28, 29]]])
Proposed solution based on comments.
Assumption is the that the array should include the numbers from 0 to 28, but it doesn't necessarily have to be of size 29. This allows us to add np.nan as the 30th element allowing the reshape.
import numpy as np
x = np.arange(29)
x = np.append(x, np.nan)
print(x.shape)
y = x.reshape(15, 2)
print(y.shape)
z = x.reshape(5, 3, 2)
print(z.shape)
output:
(30,)
(15, 2)
(5, 3, 2)

Python - How to add n zeros randomly in an existing matrix?

i have this array that i generated using the default_rng:
import numpy as np
from numpy.random import default_rng
rng = default_rng(seed=10)
rng = rng.integers(1,20,(5,10))
rng
>>>array([[15, 19, 6, 4, 16, 16, 10, 3, 16, 10],
[ 3, 3, 8, 14, 8, 16, 1, 9, 10, 19],
[ 5, 16, 2, 7, 15, 11, 18, 15, 18, 16],
[ 3, 18, 17, 3, 19, 15, 6, 3, 8, 18],
[15, 5, 10, 17, 13, 6, 3, 19, 5, 10]], dtype=int64)
I want to add 10 zeros in this matrix using the generator with seed=5.
I thought to create a new array with dimessions [5,10] and to put 10 zeros inside and the rest to be one and then mutliply the two arrays but i have to use the generator so i can't do this.
Try with np.random.choice to choose the index, then set the values at those indexes to 0:
np.random.seed(10)
idx = np.random.choice(np.arange(5*10), size=5, replace=False)
rng.ravel()[idx] = 0
Output:
array([[15, 19, 6, 4, 16, 16, 10, 3, 16, 10],
[ 3, 3, 8, 14, 8, 16, 1, 9, 10, 19],
[ 5, 16, 2, 0, 15, 11, 18, 15, 18, 16],
[ 3, 18, 17, 3, 19, 15, 6, 0, 8, 18],
[15, 5, 0, 17, 0, 6, 3, 0, 5, 10]])
Of course
idx = np.random.choice(rng.ravel(), 10, replace= False)
print(idx)
rng.ravel()[idx] = 0
rng
Output
[10 17 3 6 15 15 15 16 15 15]
array([[15, 19, 6, 0, 16, 16, 0, 3, 16, 10],
[ 0, 3, 8, 14, 8, 0, 0, 0, 10, 19],
[ 5, 16, 2, 7, 15, 11, 18, 15, 18, 16],
[ 3, 18, 17, 3, 19, 15, 6, 3, 8, 18],
[15, 5, 10, 17, 13, 6, 3, 19, 5, 10]], dtype=int64)
So instead of take 10 zeros i take only 6 becaus of 15 appears five times in my idx.

Swaping columns of numpy array in all rows but the first one

Given a numpy array
import numpy as np
a = np.arange(4*7).reshape([4, 7])
array([[ 0, 1, 2, 3, 4, 5, 6],
[ 7, 8, 9, 10, 11, 12, 13],
[14, 15, 16, 17, 18, 19, 20],
[21, 22, 23, 24, 25, 26, 27]])
I can apply slicing to swap the second and third columns by:
a[:, [0, 2, 1, 3, 4, 5, 6]]
array([[ 0, 2, 1, 3, 4, 5, 6],
[ 7, 9, 8, 10, 11, 12, 13],
[14, 16, 15, 17, 18, 19, 20],
[21, 23, 22, 24, 25, 26, 27]])
But, can I use slices to swap the second and third columns for all rows but the first one? The expected output would be:
array([[ 0, 1, 2, 3, 4, 5, 6],
[ 7, 9, 8, 10, 11, 12, 13],
[14, 16, 15, 17, 18, 19, 20],
[21, 23, 22, 24, 25, 26, 27]])
For in-situ edit, we can use flipping after slicing out the two columns -
a[1:,1:3] = a[1:,2:0:-1]
Sample run -
In [556]: a = np.arange(4*7).reshape([4, 7])
In [557]: a
Out[557]:
array([[ 0, 1, 2, 3, 4, 5, 6],
[ 7, 8, 9, 10, 11, 12, 13],
[14, 15, 16, 17, 18, 19, 20],
[21, 22, 23, 24, 25, 26, 27]])
In [559]: a[1:,1:3] = a[1:,2:0:-1]
In [560]: a
Out[560]:
array([[ 0, 1, 2, 3, 4, 5, 6],
[ 7, 9, 8, 10, 11, 12, 13],
[14, 16, 15, 17, 18, 19, 20],
[21, 23, 22, 24, 25, 26, 27]])
For columns that are two-step apart, use a stepsize of 2 to assign (LHS) and -2 to select (RHS). Hence, for column IDs 1 & 3 -
In [577]: a = np.arange(4*7).reshape([4, 7])
In [578]: a
Out[578]:
array([[ 0, 1, 2, 3, 4, 5, 6],
[ 7, 8, 9, 10, 11, 12, 13],
[14, 15, 16, 17, 18, 19, 20],
[21, 22, 23, 24, 25, 26, 27]])
In [579]: a[1:,1:4:2] = a[1:,3:0:-2]
In [580]: a
Out[580]:
array([[ 0, 1, 2, 3, 4, 5, 6],
[ 7, 10, 9, 8, 11, 12, 13],
[14, 17, 16, 15, 18, 19, 20],
[21, 24, 23, 22, 25, 26, 27]])
Another method would be with explicit column numbered indexing -
a[1:,[1,2]] = a[1:,[2,1]]
Note that this creates a copy with a[1:,[2,1]] and as such would be less memory efficient than the first method.

Categories