NumPy Array Split

In NumPy, splitting means dividing one array into multiple smaller arrays. It is useful when you want to separate data into groups, divide sales records, or organize student marks. NumPy provides functions such as split(), array_split(), hsplit(), vsplit(), and dsplit() to split arrays in different directions.

1. What is Array Splitting in NumPy?

Array splitting divides an existing array into multiple smaller arrays.

The split() function is used to divide an array into equal-sized sections.

Example:

import numpy as np

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

result = np.split(arr, 3)

print(result)

Output:

[array([10, 20]), array([30, 40]), array([50, 60])]

Explanation:

  • The original array contains 6 elements.
  • np.split(arr, 3) divides it into 3 equal parts.
  • Each smaller array contains 2 elements.

2. Splitting an Array into Equal Parts

You can specify the number of sections when using split(). However, the original array must be divisible equally by that number.

Example:

import numpy as np

marks = np.array([70, 75, 80, 85, 90, 95, 88, 92])

result = np.split(marks, 4)

for group in result:
    print(group)

Output:

[70 75]
[80 85]
[90 95]
[88 92]

Explanation:

  • The array contains 8 elements.
  • We divide it into 4 equal sections.
  • Each section contains 2 elements.

3. Splitting an Array Using array_split()

The array_split() function also divides an array into multiple sections. Unlike split(), it can divide an array even when the elements cannot be distributed equally.

Example:

import numpy as np

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

result = np.array_split(arr, 3)

for part in result:
    print(part)

Output:

[10 20 30]
[40 50]
[60 70]

Explanation:

  • The array contains 7 elements.
  • We divide it into 3 sections.
  • NumPy distributes the elements as evenly as possible.
  • The first section receives one extra element.

4. Difference Between split() and array_split()

Both functions divide arrays, but they handle unequal divisions differently.

split()array_split()
Divides an array into equal sectionsCan divide an array into unequal sections
Requires equal divisionDoes not require equal division
Raises ValueError if equal splitting is not possibleDistributes elements as evenly as possible

Example:

import numpy as np

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

# This raises ValueError
result = np.split(arr, 3)

Output:

ValueError: array split does not result in an equal division

For the same array, np.array_split(arr, 3) works successfully.

5. Splitting a 2-D Array Using split()

You can split a two-dimensional array along rows or columns by using the axis parameter.

  • axis=0 splits the array row-wise.
  • axis=1 splits the array column-wise.

Example: Splitting by Rows

import numpy as np

arr = np.array([
    [10, 20, 30],
    [40, 50, 60],
    [70, 80, 90],
    [100, 110, 120]
])

result = np.split(arr, 2, axis=0)

for part in result:
    print(part)

Output:

[[10 20 30]
 [40 50 60]]

[[ 70  80  90]
 [100 110 120]]

Example: Splitting by Columns

import numpy as np

arr = np.array([
    [10, 20, 30, 40],
    [50, 60, 70, 80]
])

result = np.split(arr, 2, axis=1)

for part in result:
    print(part)

Output:

[[10 20]
 [50 60]]

[[30 40]
 [70 80]]

Explanation:

  • axis=0 divides the rows into separate arrays.
  • axis=1 divides the columns into separate arrays.
  • The selected axis must be divisible equally when using split().

6. Splitting Arrays Using hsplit()

The hsplit() function splits an array horizontally (column-wise).

It works like split() with axis=1 for a 2-D array.

Example:

import numpy as np

arr = np.array([
    [10, 20, 30, 40],
    [50, 60, 70, 80]
])

result = np.hsplit(arr, 2)

for part in result:
    print(part)

Output:

[[10 20]
 [50 60]]

[[30 40]
 [70 80]]

Explanation:

The array has 4 columns. np.hsplit(arr, 2) divides those columns into 2 equal sections.

7. Splitting Arrays Using vsplit()

The vsplit() function splits an array vertically (row-wise).

It works like split() with axis=0 for a 2-D array.

Example:

import numpy as np

arr = np.array([
    [10, 20, 30],
    [40, 50, 60],
    [70, 80, 90],
    [100, 110, 120]
])

result = np.vsplit(arr, 2)

for part in result:
    print(part)

Output:

[[10 20 30]
 [40 50 60]]

[[ 70  80  90]
 [100 110 120]]

Explanation:

The array contains 4 rows. np.vsplit(arr, 2) divides them into 2 equal sections.

8. Splitting Arrays Using dsplit()

The dsplit() function splits a 3-D array along its third axis (depth).

Example:

import numpy as np

arr = np.array([
    [
        [10, 20, 30, 40],
        [50, 60, 70, 80]
    ],
    [
        [90, 100, 110, 120],
        [130, 140, 150, 160]
    ]
])

result = np.dsplit(arr, 2)

for part in result:
    print(part)

Output:

[[[ 10  20]
  [ 50  60]]

 [[ 90 100]
  [130 140]]]

[[[ 30  40]
  [ 70  80]]

 [[110 120]
  [150 160]]]

Explanation:

  • The array has 4 elements along its third axis.
  • np.dsplit(arr, 2) divides that axis into 2 equal sections.
  • Each resulting array contains 2 depth elements.

9. Difference Between NumPy Splitting Functions

FunctionPurpose
split()Splits an array into equal sections
array_split()Splits an array even when equal division is not possible
hsplit()Splits arrays horizontally
vsplit()Splits arrays vertically
dsplit()Splits arrays along the third axis

Student Task

A teacher stores the marks of 12 students in a NumPy array.

import numpy as np

marks = np.array([
    65, 70, 75, 80,
    85, 90, 95, 88,
    72, 78, 82, 92
])

Complete these tasks:

  1. Divide the marks into 3 equal groups using split().
  2. Divide the marks into 5 sections using array_split().
  3. Print each group separately.
  4. Create a 2-D array with 4 rows and 3 columns.
  5. Split the 2-D array row-wise using vsplit().
  6. Split the same array column-wise using hsplit().

Final Student Challenge: Company Sales Data

A company stores monthly sales records for 6 employees across 4 months.

import numpy as np

sales = np.array([
    [10000, 12000, 15000, 18000],
    [14000, 16000, 19000, 21000],
    [11000, 13000, 17000, 20000],
    [15000, 18000, 22000, 25000],
    [12000, 14000, 16000, 19000],
    [17000, 20000, 23000, 27000]
])

# Split employees into 3 groups
employee_groups = np.vsplit(sales, 3)

print("Employee Groups:")
for group in employee_groups:
    print(group)

# Split monthly sales into 2 sections
monthly_groups = np.hsplit(sales, 2)

print("\nMonthly Sales Groups:")
for group in monthly_groups:
    print(group)

Expected Output:

Employee Groups:
[[10000 12000 15000 18000]
 [14000 16000 19000 21000]]

[[11000 13000 17000 20000]
 [15000 18000 22000 25000]]

[[12000 14000 16000 19000]
 [17000 20000 23000 27000]]

Monthly Sales Groups:
[[10000 12000]
 [14000 16000]
 [11000 13000]
 [15000 18000]
 [12000 14000]
 [17000 20000]]

[[15000 18000]
 [19000 21000]
 [17000 20000]
 [22000 25000]
 [16000 19000]
 [23000 27000]]

Your Challenge:

  1. Divide the employee records into 2 equal groups using vsplit().
  2. Divide the monthly sales columns into 4 sections using hsplit().
  3. Use array_split() to divide the 6 employees into 4 groups.
  4. Print the shape of every resulting array.
  5. Observe how the number of rows and columns changes after splitting.

Key Takeaways

  • Array splitting divides one array into multiple smaller arrays.
  • split() requires equal division along the selected axis.
  • array_split() supports unequal sections.
  • axis=0 splits rows, while axis=1 splits columns.
  • NumPy hsplit() divides arrays horizontally.
  • NumPy vsplit() divides arrays vertically.
  • dsplit() divides arrays along the third axis.

Frequently Asked Questions (FAQs)

1. What is NumPy Array Split in Python?

NumPy Array Split is the process of dividing a single NumPy Array into multiple smaller arrays. NumPy provides functions such as split(), array_split(), hsplit(), and vsplit() to split arrays in different ways.

2. What is the NumPy split() function used for?

The NumPy split() function is used to divide an array into multiple sub-arrays along a specified axis. It is useful for separating large datasets into smaller sections.

Example: np.split(arr, 3)

3. How do you split a NumPy Array into multiple arrays?

You can use the np.split() function to divide a NumPy Array into multiple equal-sized sub-arrays, provided the array can be divided evenly.

Example: np.split(arr, 2)

4. What is the difference between split() and array_split() in NumPy?

The split() function requires the array to be divided into equal-sized sections, whereas array_split() can divide an array into sections of unequal sizes when an equal division is not possible.

5. What is NumPy hsplit() used for?

NumPy hsplit() is used to split an array horizontally (column-wise). It is commonly used to divide a 2-D NumPy Array into multiple smaller arrays based on columns.

6. What is NumPy vsplit() in Python?

NumPy vsplit() is used to split a 2-D or higher-dimensional array vertically (row-wise). It divides an array into smaller arrays along the first axis.

7. What is the difference between NumPy hsplit() and NumPy vsplit()?

The main difference between NumPy hsplit() and vsplit() is that hsplit() divides an array horizontally along columns, whereas vsplit() divides an array vertically along rows.

8. Can we split a NumPy Array into unequal parts?

Yes, you can use the np.array_split() function to divide a NumPy Array into multiple unequal parts. It distributes elements as evenly as possible, even when the array cannot be divided equally.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top