Numpy array_split()

``numpy.array_split(ary, indices_or_sections, axis=0)``
Return broken array .
 `ary` input array to be broken `sections` number, number of sections of output .

Examples

``````import numpy as np
my_ar=np.arange(9)
print(np.array_split(my_ar,3))``````
Output
``[array([0, 1, 2]), array([3, 4, 5]), array([6, 7, 8])]``
With unequal number of elements in output
``````my_ar=np.arange(9)
print(np.array_split(my_ar,5))``````
Output
``[array([0, 1]), array([2, 3]), array([4, 5]), array([6, 7]), array([8])]``
Example
``````import numpy as np
my_ar=np.arange(9)
print(np.array_split(my_ar,4))``````
Output
``[array([0, 1, 2]), array([3, 4]), array([5, 6]), array([7, 8])]``
``````my_data=np.array([[6, 3, 2], [7, 2, 2], [6, 2, 9]])
print(np.array_split(my_data,3))``````
Output
``[array([[6, 3, 2]]), array([[7, 2, 2]]), array([[6, 2, 9]])]``
Elements of the output array
``````import numpy as np
my_data=np.array([[6, 3, 2], [7, 2, 2], [6, 2, 9]])
my_ar=np.array_split(my_data,3)
print(my_ar[1])    # [[7 2 2]]
print(my_ar[1][0]) #  [7 2 2]
print(my_ar[1][0][2]) # 2``````
Using float dtype
``````my_ar=np.arange(9.)
print(np.array_split(my_ar,4))``````
Output
``[array([0., 1., 2.]), array([3., 4.]), array([5., 6.]), array([7., 8.])]``

Subscribe to our YouTube Channel here

Subscribe

* indicates required
Subscribe to plus2net

plus2net.com