Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why am I getting this output while using split() in numpy array?

I am running this code, and I used indices in the order of (2, 1) on purpose.

a1 = np.arange(30).reshape(5, 6)
print(a1)
a, b, c = np.split(a1, (2, 1))
print("A is\n",a)
print("B is\n",b)
print("C is\n",c)

And this is the output:

[[ 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]]
A is
 [[ 0  1  2  3  4  5]
 [ 6  7  8  9 10 11]]
B is
 []
C is
 [[ 6  7  8  9 10 11]
 [12 13 14 15 16 17]
 [18 19 20 21 22 23]
 [24 25 26 27 28 29]]

I understand that I am getting b = [] because I used indices (2, 1). But why am I getting the c output from 1st index? Thank you for your time

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

According to the documentation:

If indices_or_sections is a 1-D array of sorted integers, the entries
indicate where along axis the array is split. For example,
[2, 3] would, for axis=0, result in

 - ary[:2]
 - ary[2:3]
 - ary[3:]

I.e., your example a, b, c = np.split(a1, (2, 1)) translates to:

a = a1[:2]
b = a1[2:1]
c = a1[1:]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading