# Splitting Arrays in NumPy Python Functions

Guide to Splitting Arrays in NumPy – We can use functions hsplit(), vsplit() and dsplit() for Array splitting horizontal, vertical and depth-wise respectively. We can split arrays into arrays of the same shape by indicating the position after which the split should occur.

## Introduction to Splitting Arrays

### NumPy dsplit

The numpy.split() function Split the array into multiple sub-arrays along the 3rd axis (depth).

Syntax:

`numpy.dsplit(ary, indices_or_sections)`

### NumPy hsplit

The function hsplit() splits an array along with the axis parameter = 1. The numpy.hsplit() is equal to ‘split’ with axis parameter = 1, and regardless of the array dimension, the array is always split along the second axis. This function divides an array horizontally into multiple sub-arrays (column-wise).

Syntax:

`numpy.hsplit(ary, indices_or_sections)`

### NumPy vsplit

The function vsplit() splits an array along the axis argument = 0. Split with axis parameter = 0 is similar to numpy.vsplit(). This function splits an array vertically into multiple sub-arrays (row-wise).

Syntax:

`numpy.vsplit(ary, indices_or_sections)`

## Code Examples of Splitting Arrays

### NumPy dsplit

Example 01:

``````# welcome to softhunt.net
import numpy as np

# Making of a 3x3x3 array.
b = np.arange(27).reshape(3, 3, 3)

# Depth-wise splitting of array
# 'b' using np.dsplit().
print("The array :\n {} \n\n gets splitted depth-wise to form: \n\n {}".format(b, np.dsplit(b, 3)))``````

Output:

```The array :
[[[ 0  1  2]
[ 3  4  5]
[ 6  7  8]]

[[ 9 10 11]
[12 13 14]
[15 16 17]]

[[18 19 20]
[21 22 23]
[24 25 26]]]

gets splitted depth-wise to form:

[array([[[ 0],
[ 3],
[ 6]],

[[ 9],
[12],
[15]],

[[18],
[21],
[24]]]), array([[[ 1],
[ 4],
[ 7]],

[[10],
[13],
[16]],

[[19],
[22],
[25]]]), array([[[ 2],
[ 5],
[ 8]],

[[11],
[14],
[17]],

[[20],
[23],
[26]]])]```

### NumPy hsplit

Example 01:

``````# welcome to softhunt.net
# Horizontal array splitting using np.hsplit()
import numpy as np

# Making of a 3x3 array
a = np.arange(9).reshape(3, 3)

# Horizontal splitting of array
# 'a' using np.hsplit().
print("The array: \n {} \n\n gets splitted horizontally to form: \n {} ".format(a, np.hsplit(a, 3)))

# Horizontal splitting of array 'a'
# using 'split' with axis parameter = 1.
print("\n The array: \n {} \n\n gets splitted horizontally to form: \n {} ".format(a, np.split(a, 3, 1)))``````

Output:

```The array:
[[0 1 2]
[3 4 5]
[6 7 8]]

gets splitted horizontally to form:
[array([[0],
[3],
[6]]), array([[1],
[4],
[7]]), array([[2],
[5],
[8]])]

The array:
[[0 1 2]
[3 4 5]
[6 7 8]]

gets splitted horizontally to form:
[array([[0],
[3],
[6]]), array([[1],
[4],
[7]]), array([[2],
[5],
[8]])] ```

### NumPy vsplit

Example 01:

``````# welcome to softhunt.net
# Vertical array splitting using np.hsplit()
import numpy as np

# Making of a 3x3 array
a = np.arange(9).reshape(3, 3)

# Vertical splitting of array
# 'a' using np.hsplit().
print("The array: \n {} \n\n gets splitted Vertically to form: \n {} ".format(a, np.vsplit(a, 3)))

# Vertical splitting of array 'a'
# using 'split' with axis parameter = 0.
print("\n The array: \n {} \n\n gets splitted Vertically to form: \n {} ".format(a, np.split(a, 3, 0)))``````

Output:

```The array:
[[0 1 2]
[3 4 5]
[6 7 8]]

gets splitted Vertically to form:
[array([[0, 1, 2]]), array([[3, 4, 5]]), array([[6, 7, 8]])]

The array:
[[0 1 2]
[3 4 5]
[6 7 8]]

gets splitted Vertically to form:
[array([[0, 1, 2]]), array([[3, 4, 5]]), array([[6, 7, 8]])] ```

## Splitting Arrays: FAQs

### How does the split function work in Python?

• Manipulation of strings is necessary for all of the programs dealing with strings. In such cases, you need to make use of a function called the split() function in Python.
• The split() function works by scanning the given string or line based on the separator passed as the parameter to the split() function.
• In case the separator is not passed as a parameter to the split() function, the white spaces in the given string or line are considered as the separator by the split() function.
• The number of times a given string or line must be broken up can also be specified using the split() function. In case you do not specify this value, the entire string or line is scanned and separated based on the delimiter.
• The split() function returns the substrings as the elements of a list.

### How do you split letters in Python?

The split() function is includes in the Python standard library and may be use to separate strings. To divide strings between characters, use this function. There are two arguments to the split() function. The separator, for example, controls which character is use to separate the string.

## Splitting Arrays: Conclusion

That’s all for this article, if you have any confusion contact us through our website or email us at [email protected] or by using LinkedIn. And make sure you check out our NumPy tutorials.

Suggested Articles:

1. NumPy split and array_split Python Function