Kodeclik Logo

Our Programs

Courses

Gifting

Learn More

Schedule

Kodeclik Blog

Python numpy.split()

numpy is a powerful Python library that provides a wide range of mathematical and scientific computing functionalities. One of its key features is the ability to split arrays into multiple sub-arrays using the split function.

The split function is a useful tool for organizing and manipulating arrays. It allows you to divide an array into multiple sub-arrays along a specified axis. This can be particularly useful in situations where you need to process parts of an array independently.

In this blog post, we will explore how to use the split function in numpy to split arrays into multiple sub-arrays.

The NumPy split function takes three arguments: the array to be split, the number of sub-arrays to create, and the axis along which to split the array.

Here is a very simple example of how this works:

import numpy as np

# Create an array
arr = np.array([1, 2, 3, 4, 5, 6])

# Split the array into two sub-arrays
subarrays = np.split(arr, 2)

# Print the sub-arrays
print(subarrays)

Thus this code uses the numpy.split() function to split the given array into two sub-arrays. The output is:

[array([1, 2, 3]), array([4, 5, 6])]

Thus the split() function has created two sub-arrays of equal size. As we can see from the output, the numpy.split() function has split the array into two sub-arrays of length 3 each.

We can also split it unevenly by specifying particular sizes, like so:

Python numpy.split()

import numpy as np

# Create an array
arr = np.array([1, 2, 3, 4, 5, 6])

# Split the array into two sub-arrays
subarrays = np.split(arr, [1,3,6])

# Print the sub-arrays
print(subarrays)

Here we are splitting it into three arrays of sizes 1, 2, and 3 respectively (note that the indices are given in cumulative fashion, i.e., the first array stops before index 1, the second array stops before index 3, and the third array stops before index 6. The output will thus be:

[array([1]), array([2, 3]), array([4, 5, 6]), array([], dtype=int64)]

Finally, in addition to splitting arrays into sub-arrays of specific sizes, we can also split arrays along a specified axis. To do this, we need to use a multi-dimensional array like so:

import numpy as np

# Create a two-dimensional array
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

# Split the array along the y-axis
subarrays = np.split(arr, 3, axis=0)

# Print the sub-arrays
print(subarrays)

The output is:

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

Thus, we have split the array into three sub-arrays, one for each row. If we change the axis:

import numpy as np

# Create a two-dimensional array
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

# Split the array along the y-axis
subarrays = np.split(arr, 3, axis=1)

# Print the sub-arrays
print(subarrays)

The output will be:

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

In this case we have split the array along the columns.

So in conclusion numpy.split() is a very handy function to break down an array into specified sizes along specified dimensions.

If you liked learning about numpy.split() checkout our blogpost about numpy.gradient().

Interested in more things Python? Checkout our post on Python queues. Also see our blogpost on Python's enumerate() capability. Also if you like Python+math content, see our blogpost on Magic Squares. Finally, master the Python print function!

Want to learn Python with us? Sign up for 1:1 or small group classes.

Kodeclik sidebar newsletter

Join our mailing list

Subscribe to get updates about our classes, camps, coupons, and more.

About

Kodeclik is an online coding academy for kids and teens to learn real world programming. Kids are introduced to coding in a fun and exciting way and are challeged to higher levels with engaging, high quality content.

Copyright @ Kodeclik 2024. All rights reserved.