Split Numpy Array

Split means to break/ slash an array into multiple arrays. To split an array, use the array_split() method. The following is the syntax:

array_split(arr_name, split_num)

Here, arr_name is the name of the array, and split_num is the count of splits.

In this lesson, we will see the following examples:

  1. Split a 1D array with NumPy
  2. Split a 1D array and access the divided arrays with NumPy
  3. Split a 2D array (axis = 0) and access the divided arrays with NumPy
  4. Split a 2D array (axis = 1) and access the divided arrays with NumPy
  5. Split a 3D array (axis = 0) and access the divided arrays with NumPy
  6. Split a 3D array (axis = 1) and access the divided arrays with NumPy
  7. Split a 3D array (axis = 2) and access the divided arrays with NumPy

1. Split a 1D array with NumPy

As shown above, to split an array in Numpy, we use the array_split() method. Herein, we will split a 1D array. Let us see an example of splitting a 1D array:

import numpy as np

# Create a Numpy array
n = np.array([10, 20, 30, 40, 50, 60])

print("Iterating array...")
for a in n:
    print(a)

# splitting array into 2
resarr = np.array_split(n, 2)

print("\nArray after splitting i.e. returns 2 arrays");
for a in resarr:
    print(a)

Output

Iterating array...
10
20
30
40
50
60

Array after splitting i.e. returns 2 arrays
[10 20 30]
[40 50 60]

2. Split a 1D array and access the divided arrays with NumPy

To access a split One-Dimensional array, use the index number for the array element you want to display. Let us see an example to learn how to access a splitter 1D array:

import numpy as np

n = np.array([10, 20, 30, 40, 50, 60])

print("Iterating array...")
for a in n:
  print(a)

# splitting into 2
resarr = np.array_split(n, 2)

print("\nArray after splitting i.e. returns 2 arrays");
print(resarr)

print("\nAccess splitted array individually...");
print("Array1 = ",resarr[0])
print("Array2 = ",resarr[1])

Output

Iterating array...
10
20
30
40
50
60

Array after splitting i.e. returns 2 arrays
[array([10, 20, 30]), array([40, 50, 60])]

Access splitted array individually...
Array1 =  [10 20 30]
Array2 =  [40 50 60]

3. Split a 2D array (axis = 0) and access the divided arrays with NumPy

The split result will be a 2D array, for example, if a 2D array is split into three, there will be three 2D arrays as a result. Let us see an example of splitting a 2D array:

# Split a 2D array and access the divided arrays with NumPy
# axis = 0

import numpy as np

# Create a 2d array
n = np.array([[1, 3, 5, 7],
              [4, 8, 12, 18],
              [25, 38, 82, 98]
              ])

# axis = 0 is the default
res = np.array_split(n, 3)
print(res)

# access the divided arrays
print("Array 1 = ",res[0])
print("Array 2 = ",res[1])
print("Array 2 = ",res[2])

Output

[array([[1, 3, 5, 7]]), array([[ 4,  8, 12, 18]]), array([[25, 38, 82, 98]])]
Array 1 =  [[1 3 5 7]]
Array 2 =  [[ 4  8 12 18]]
Array 2 =  [[25 38 82 98]]

4. Split a 2D array (axis = 1) and access the divided arrays with NumPy

# Split a 2D array and access the divided arrays with NumPy
# axis = 1

import numpy as np

# Create a 2d array
n = np.array([[1, 3, 5, 7],
              [4, 8, 12, 18],
              [25, 38, 82, 98]
              ])

res = np.array_split(n, 4, axis=1)
print(res)

# access the divided arrays
print("\nArray 1 = \n",res[0])
print("Array 2 = \n",res[1])
print("Array 3 = \n",res[2])
print("Array 4 = \n",res[3])

Output

[array([[ 1],
       [ 4],
       [25]]), array([[ 3],
       [ 8],
       [38]]), array([[ 5],
       [12],
       [82]]), array([[ 7],
       [18],
       [98]])]

Array 1 = 
 [[ 1]
 [ 4]
 [25]]
Array 2 = 
 [[ 3]
 [ 8]
 [38]]
Array 3 = 
 [[ 5]
 [12]
 [82]]
Array 4 = 
 [[ 7]
 [18]
 [98]]

5. Split a 3D array (axis = 0) and access the divided arrays with NumPy

# Split a 3D array and access the divided arrays with NumPy
# axis = 0

import numpy as np

n = np.array([[
               [1, 2, 3],
               [4, 5, 6]],
              [
               [7, 8, 9],
               [10, 11, 12]]
             ])

# axis = 0 is the default
# depth
res = np.array_split(n, 2, axis = 0)
print(res)

# access the divided arrays
print("\nArray 1 = \n",res[0])
print("Array 2 = \n",res[1])

Output

[array([[[1, 2, 3],
        [4, 5, 6]]]), array([[[ 7,  8,  9],
        [10, 11, 12]]])]

Array 1 = 
 [[[1 2 3]
  [4 5 6]]]
Array 2 = 
 [[[ 7  8  9]
  [10 11 12]]]

6. Split a 3D array (axis = 1) and access the divided arrays with NumPy

# Split a 3D array and access the divided arrays with NumPy
# axis = 1

import numpy as np

n = np.array([[
               [1, 2, 3],
               [4, 5, 6]],
              [
               [7, 8, 9],
               [10, 11, 12]]
             ])

# rows
res = np.array_split(n, 2, axis = 1)
print(res)

# access the divided arrays
print("\nArray 1 = \n",res[0])
print("Array 2 = \n",res[1])

Output

[array([[[1, 2, 3]],

       [[7, 8, 9]]]), array([[[ 4,  5,  6]],

       [[10, 11, 12]]])]

Array 1 = 
 [[[1 2 3]]

 [[7 8 9]]]
Array 2 = 
 [[[ 4  5  6]]

 [[10 11 12]]]

7. Split a 3D array (axis = 2) and access the divided arrays with NumPy

# Split a 3D array and access the divided arrays with NumPy
# axis = 2

import numpy as np

n = np.array([[
               [1, 2, 3],
               [4, 5, 6]],
              [
               [7, 8, 9],
               [10, 11, 12]]
             ])

# columns
res = np.array_split(n, 2, axis = 2)
print(res)

# access the divided arrays
print("\nArray 1 = \n",res[0])
print("Array 2 = \n",res[1])
# print("Array 3 = \n",res[2])
# print("Array 4 = \n",res[3])

Output

[array([[[ 1,  2],
        [ 4,  5]],

       [[ 7,  8],
        [10, 11]]]), array([[[ 3],
        [ 6]],

       [[ 9],
        [12]]])]

Array 1 = 
 [[[ 1  2]
  [ 4  5]]

 [[ 7  8]
  [10 11]]]
Array 2 = 
 [[[ 3]
  [ 6]]

 [[ 9]
  [12]]]

If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.

For Videos, Join Our YouTube Channel: Join Now


Read More:

Joining Numpy Arrays
Search a Numpy Array for a value
Studyopedia Editorial Staff
contact@studyopedia.com

We work to create programming tutorials for all.

No Comments

Post A Comment