NumPy Array Splitting

In this discussion, we will delve into the different techniques for NumPy Array Splitting, including the use of functions such as numpy.hsplit, and numpy.array_split.

This function takes the array that needs to be split and the number of splits desired as its arguments.

The purpose of this function is to break a single array into multiple smaller sub-arrays.



NumPy Array Split Examples

Divide the prime_arr array into four sections.

Example: 

import numpy as npy prime_arr = npy.array([2, 3, 5, 7, 11, 13, 17, 19]) mrx_arr = npy.array_split(prime_arr, 4) print(mrx_arr)

NumPy Array Splitting

Make three array partitions from the prime_arr array.

Example: 

import numpy as npy cube_arr = npy.array([1, 8, 27, 64, 125, 216]) mrx_arr = npy.array_split(cube_arr, 3) print(mrx_arr)
Reminder: A output value consists of three arrays.

Numpy array split will modify from the end appropriately if the array has fewer elements than needed.

Separate the even_arr array into 4 subarrays:

Example: 

import numpy as npy even_arr = npy.array([0, 2, 4, 6, 8, 10, 12, 14]) mrx_arr = npy.array_split(even_arr, 5) print(mrx_arr)

Make three subarrays from the even_arr array:

Example: 

import numpy as npy odd_arr = npy.array([1, 3, 5, 7, 9, 11, 13]) mrx_arr = npy.array_split(odd_arr, 3) print(mrx_arr)
Reminder: As with array_split(), split() will not modify the items when there are less items in the source array for splitting. For example, array_split() executed successfully but split() was unsuccessful.

Split Into Arrays

When we talk about Numpy array split, the array_split() method produces an array including every split.

Retrieve the array which is divided:

Example: 

import numpy as npy fibonacci_arr = npy.array([ 0, 1, 1, 2, 3, 5, 8, 13]) mrx_arr = npy.array_split(fibonacci_arr, 4) print(mrx_arr[0]) print(mrx_arr[1]) print(mrx_arr[2]) print(mrx_arr[3])

The array that is separated can be obtained as follows:

Example: 

import numpy as npy factorial_arr = npy.array([ 1, 2, 6, 24, 120, 720, 5040]) mrx_arr = npy.array_split(factorial_arr, 5) print(mrx_arr[0]) print(mrx_arr[1]) print(mrx_arr[2]) print(mrx_arr[3]) print(mrx_arr[4])

Numpy Array Splitting 2-D

Split 2-D arrays utilizing the identical syntax. Simply pass in the array you need to split and the number of splits you need to make to the array_split() method.

Make five two-dimensional arrays out of one two-dimensional array.

Example: 

import numpy as npy rational_arr = npy.array([[2.7, 3.84], [1.54, 3.22], [6.49, 5.11], [7.34, 8.96], [3.29, 7.33], [9.39, 9.99], [8.91, 6.16]]) mrx_arr = npy.array_split(rational_arr, 5) print(mrx_arr)

From one palindrome_arr two-dimensional array, create five two-dimensional arrays.

Example: 

import numpy as npy palindrome_arr = npy.array([["101", "252"], ["25252", "89098"], ["1845481", "1010101"], ["3456543", "989"], ["9672769", "24542"], ["01110", "225535522"], ["800008", "61216"]]) mrx_arr = npy.array_split(palindrome_arr, 5) print(mrx_arr)

The outputs of the above two examples provide five two-dimensional arrays.

Obtain four two-dimensional arrays by partitioning the two-dimensional array in half.

Example: 

import numpy as npy even_arr = npy.array([[0, 2, 4], [6, 8, 10], [12, 14, 16], [20, 22, 24], [26, 28, 30]]) mrx_arr = npy.array_split(even_arr, 4) print(mrx_arr)

Divide the silentWords_arr two-dimensional array into four two-dimensional arrays.

Example: 

import numpy as npy silentWords_arr = npy.array([['aplomb', 'bomb', 'abscess'], ['conscience', 'conscience', 'edge'], ['assign', 'align', 'handful'], ['descent', 'doubt', 'handkerchief'], ['fluorescent', 'breathe', 'muscle']]) mrx_arr = npy.array_split(silentWords_arr, 4) print(mrx_arr)

There are four two-dimensional arrays generated by the above two examples.

You can also indicate which axis you need to divide around.

Assemble three two-dimensional arrays in rows from the two-dimensional array.

Example: 

import numpy as npy odd_arr = npy.array([[1, 3, 5], [7, 9, 11], [13, 15, 17], [19, 21, 23], [25, 27, 29]]) mrx_arr = npy.array_split(odd_arr, 3, axis=1) print(mrx_arr)

Separate the two-dimensional array into three two-dimensional arrays in rows.

Example: 

import numpy as npy prime_arr = npy.array([[2, 3, 5], [7, 11, 13], [17, 19, 23], [27, 31, 33], [37, 41, 43]]) mrx_arr = npy.array_split(prime_arr, 3, axis=1) print(mrx_arr)

The better approach is to utilize hsplit() instead of hstack()

To divide the two-dimensional array into three two-dimensional arrays according to rows, invoke the hsplit() method.

Example: 

import numpy as npy odd_arr = npy.array([[1, 3, 5], [7, 9, 11], [13, 15, 17], [19, 21, 23], [25, 27, 29]]) mrx_arr = npy.hsplit(odd_arr, 3) print(mrx_arr)

By utilizing the hsplit() method, you can seperate a two-dimensional array into three two-dimensional arrays based on rows.

Example: 

import numpy as npy prime_arr = npy.array([[2, 3, 5], [7, 11, 13], [17, 19, 23], [27, 31, 33], [37, 41, 43]]) mrx_arr = npy.hsplit(prime_arr, 3) print(mrx_arr)

Example Explanation

In above example we have import the NumPy library using the alias npy.

Then we have created a 2D NumPy array called prime_arr, which has 5 rows and 3 columns. Each row contains 3 prime numbers.

The npy.hsplit() function is used to split the prime_arr array horizontally into 3 arrays.

This function splits the array along its columns, creating 3 separate arrays. These arrays are then assigned to the variable mrx_arr.

Finally, the code prints the value of mrx_arr. This should output a list of 3 arrays, where each array has 5 rows and 1 column, containing the prime numbers that were originally in the corresponding columns of prime_arr.

Reminder: A suitable replacement for vstack() and dstack() is vsplit() and dsplit().
We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *