# NumPy array Split Comprehensive Guide Python NumPy Tutorials 11 | Better4Code

**NumPy Array Split: **NumPy is a popular Python library for scientific computing that provides powerful tools for numerical operations on arrays and matrices. One of the essential functions of NumPy is **NumPy array split**, which allows you to divide an array into smaller arrays based on certain criteria. In this article, we’ll explore the NumPy array split function and how to use it effectively.

**Understanding NumPy Array Split**

NumPy provides two main array splitting functions: **split()** and **hsplit()**. The split() function is used to split an array into multiple sub-arrays based on a specified index, while the **hsplit()** function is used to split an array horizontally along its columns.

Let’s take a look at the syntax for both functions:

numpy.split(ary, indices_or_sections, axis=0) numpy.hsplit(ary, indices_or_sections)

The **ary** parameter is the input array that you want to split. The indices_or_sections parameter specifies the split points along the given axis. The axis parameter is optional and specifies the axis along which to split the array. If axis is not specified, it defaults to 0, which means the array will be split vertically.

**Splitting an array vertically with NumPy**

To split an array vertically using the split() function, you need to provide the indices at which the array should be split. For example, if you have a 2D array of shape (4, 4) and you want to split it into two smaller arrays, you can use the following code:

import numpy as np arr = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12], [13,14,15,16]]) result = np.split(arr, 2) print(result)

The output will be:

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

Here, we have split the array into two sub-arrays of shape (2, 4).

**Splitting an array horizontally with NumPy**

### To split an array horizontally using the **hsplit()** function, you need to provide the indices at which the array should be split. For example, if you have a 2D array of shape (4, 4) and you want to split it into two smaller arrays along the second axis, you can use the following code:

import numpy as np arr = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12], [13,14,15,16]]) result = np.hsplit(arr, 2) print(result)

The output will be:

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

Here, we have split the array into two sub-arrays of shape (4, 2).

**Conclusion**

NumPy array splitting is a powerful function that allows you to divide arrays into smaller arrays based on a specified index or axis. Whether you’re working with 1D or multi-dimensional arrays, NumPy’s** split()** and **hsplit()** functions provide a flexible way to manipulate your data.

By understanding how to use these functions, you can create more efficient and optimized code that takes full advantage of the capabilities of NumPy. Whether you are working on data analysis, machine learning, or any other data-intensive project, NumPy’s array-splitting functions can be a useful tool in your arsenal.

By utilizing these functions in your code, you can save time and effort in manipulating your data, leading to more efficient and effective data analysis.