Splitting one array into several smaller ones in NumPy Module

Splitting one array into several smaller ones in NumPy Module

Instructor-svg Al-Mamun Sarkar
Mar 31 , 2020

Splitting one array into several smaller ones in NumPy Module. The following shows how to split one array into several smaller ones of the Python NumPy module. 

Code:

a = np.floor(10*np.random.random((2,12)))
a

Output:

array([[ 7.,  4.,  3.,  0.,  9.,  6.,  4.,  4.,  6.,  2.,  2.,  1.],
       [ 9.,  9.,  3.,  7.,  6.,  5.,  7.,  5.,  9.,  2.,  0.,  9.]])

 

Code:

np.hsplit(a,3)

Output:

[array([[ 7.,  4.,  3.,  0.],
        [ 9.,  9.,  3.,  7.]]), array([[ 9.,  6.,  4.,  4.],
        [ 6.,  5.,  7.,  5.]]), array([[ 6.,  2.,  2.,  1.],
        [ 9.,  2.,  0.,  9.]])]

 

Code:

np.hsplit(a,(3,4))

Output:

[array([[ 7.,  4.,  3.],
        [ 9.,  9.,  3.]]), array([[ 0.],
        [ 7.]]), array([[ 9.,  6.,  4.,  4.,  6.,  2.,  2.,  1.],
        [ 6.,  5.,  7.,  5.,  9.,  2.,  0.,  9.]])]

 

  • Share On:
  • fb
  • twitter
  • pinterest
  • instagram