
- NumPy - Home
- NumPy - Introduction
- NumPy - Environment
- NumPy Arrays
- NumPy - Ndarray Object
- NumPy - Data Types
- NumPy Creating and Manipulating Arrays
- NumPy - Array Creation Routines
- NumPy - Array Manipulation
- NumPy - Array from Existing Data
- NumPy - Array From Numerical Ranges
- NumPy - Iterating Over Array
- NumPy - Reshaping Arrays
- NumPy - Concatenating Arrays
- NumPy - Stacking Arrays
- NumPy - Splitting Arrays
- NumPy - Flattening Arrays
- NumPy - Transposing Arrays
- NumPy Indexing & Slicing
- NumPy - Indexing & Slicing
- NumPy - Indexing
- NumPy - Slicing
- NumPy - Advanced Indexing
- NumPy - Fancy Indexing
- NumPy - Field Access
- NumPy - Slicing with Boolean Arrays
- NumPy Array Attributes & Operations
- NumPy - Array Attributes
- NumPy - Array Shape
- NumPy - Array Size
- NumPy - Array Strides
- NumPy - Array Itemsize
- NumPy - Broadcasting
- NumPy - Arithmetic Operations
- NumPy - Array Addition
- NumPy - Array Subtraction
- NumPy - Array Multiplication
- NumPy - Array Division
- NumPy Advanced Array Operations
- NumPy - Swapping Axes of Arrays
- NumPy - Byte Swapping
- NumPy - Copies & Views
- NumPy - Element-wise Array Comparisons
- NumPy - Filtering Arrays
- NumPy - Joining Arrays
- NumPy - Sort, Search & Counting Functions
- NumPy - Searching Arrays
- NumPy - Union of Arrays
- NumPy - Finding Unique Rows
- NumPy - Creating Datetime Arrays
- NumPy - Binary Operators
- NumPy - String Functions
- NumPy - Matrix Library
- NumPy - Linear Algebra
- NumPy - Matplotlib
- NumPy - Histogram Using Matplotlib
- NumPy Sorting and Advanced Manipulation
- NumPy - Sorting Arrays
- NumPy - Sorting along an axis
- NumPy - Sorting with Fancy Indexing
- NumPy - Structured Arrays
- NumPy - Creating Structured Arrays
- NumPy - Manipulating Structured Arrays
- NumPy - Record Arrays
- Numpy - Loading Arrays
- Numpy - Saving Arrays
- NumPy - Append Values to an Array
- NumPy - Swap Columns of Array
- NumPy - Insert Axes to an Array
- NumPy Handling Missing Data
- NumPy - Handling Missing Data
- NumPy - Identifying Missing Values
- NumPy - Removing Missing Data
- NumPy - Imputing Missing Data
- NumPy Performance Optimization
- NumPy - Performance Optimization with Arrays
- NumPy - Vectorization with Arrays
- NumPy - Memory Layout of Arrays
- Numpy Linear Algebra
- NumPy - Linear Algebra
- NumPy - Matrix Library
- NumPy - Matrix Addition
- NumPy - Matrix Subtraction
- NumPy - Matrix Multiplication
- NumPy - Element-wise Matrix Operations
- NumPy - Dot Product
- NumPy - Matrix Inversion
- NumPy - Determinant Calculation
- NumPy - Eigenvalues
- NumPy - Eigenvectors
- NumPy - Singular Value Decomposition
- NumPy - Solving Linear Equations
- NumPy - Matrix Norms
- NumPy Element-wise Matrix Operations
- NumPy - Sum
- NumPy - Mean
- NumPy - Median
- NumPy - Min
- NumPy - Max
- NumPy Set Operations
- NumPy - Unique Elements
- NumPy - Intersection
- NumPy - Union
- NumPy - Difference
- NumPy Random Number Generation
- NumPy - Random Generator
- NumPy - Permutations & Shuffling
- NumPy - Uniform distribution
- NumPy - Normal distribution
- NumPy - Binomial distribution
- NumPy - Poisson distribution
- NumPy - Exponential distribution
- NumPy - Rayleigh Distribution
- NumPy - Logistic Distribution
- NumPy - Pareto Distribution
- NumPy - Visualize Distributions With Sea born
- NumPy - Matplotlib
- NumPy - Multinomial Distribution
- NumPy - Chi Square Distribution
- NumPy - Zipf Distribution
- NumPy File Input & Output
- NumPy - I/O with NumPy
- NumPy - Reading Data from Files
- NumPy - Writing Data to Files
- NumPy - File Formats Supported
- NumPy Mathematical Functions
- NumPy - Mathematical Functions
- NumPy - Trigonometric functions
- NumPy - Exponential Functions
- NumPy - Logarithmic Functions
- NumPy - Hyperbolic functions
- NumPy - Rounding functions
- NumPy Fourier Transforms
- NumPy - Discrete Fourier Transform (DFT)
- NumPy - Fast Fourier Transform (FFT)
- NumPy - Inverse Fourier Transform
- NumPy - Fourier Series and Transforms
- NumPy - Signal Processing Applications
- NumPy - Convolution
- NumPy Polynomials
- NumPy - Polynomial Representation
- NumPy - Polynomial Operations
- NumPy - Finding Roots of Polynomials
- NumPy - Evaluating Polynomials
- NumPy Statistics
- NumPy - Statistical Functions
- NumPy - Descriptive Statistics
- NumPy Datetime
- NumPy - Basics of Date and Time
- NumPy - Representing Date & Time
- NumPy - Date & Time Arithmetic
- NumPy - Indexing with Datetime
- NumPy - Time Zone Handling
- NumPy - Time Series Analysis
- NumPy - Working with Time Deltas
- NumPy - Handling Leap Seconds
- NumPy - Vectorized Operations with Datetimes
- NumPy ufunc
- NumPy - ufunc Introduction
- NumPy - Creating Universal Functions (ufunc)
- NumPy - Arithmetic Universal Function (ufunc)
- NumPy - Rounding Decimal ufunc
- NumPy - Logarithmic Universal Function (ufunc)
- NumPy - Summation Universal Function (ufunc)
- NumPy - Product Universal Function (ufunc)
- NumPy - Difference Universal Function (ufunc)
- NumPy - Finding LCM with ufunc
- NumPy - ufunc Finding GCD
- NumPy - ufunc Trigonometric
- NumPy - Hyperbolic ufunc
- NumPy - Set Operations ufunc
- NumPy Useful Resources
- NumPy - Quick Guide
- NumPy - Cheatsheet
- NumPy - Useful Resources
- NumPy - Discussion
- NumPy Compiler
NumPy - Splitting Arrays
Splitting NumPy Array
Splitting arrays in NumPy is a way to divide a single array into multiple sub-arrays. This can be done along any axis, depending on how you want to partition the data. NumPy provides several functions to split arrays in different ways. They are as follows −
- Using numpy.split() Function
- Using numpy.array_split() Function
- Using numpy.hsplit() Function
- Using numpy.vsplit() Function
- Using numpy.dsplit() Function
Splitting Arrays Using split() Function
We can use the split() function in NumPy to split an array into multiple sub-arrays along a specified axis. The array is divided based on the provided indices. Following is the syntax −
numpy.split(array, indices_or_sections, axis=0)
Where,
array − The input array to be split.
-
indices_or_sections − This can be either an integer or a 1D array of sorted integers.
If an integer, it specifies the number of equal-sized sub-arrays to create. The array must be divisible evenly into this number of sections.
If a 1D array of sorted integers, it specifies the points at which to split the array.
axis − The axis along which to split the array. The default is 0 (split along rows for 2D arrays).
Example: Splitting into Equal-sized Sub-arrays
In the below example, we are splitting an array "arr" into 3 equal sub-arrays along the columns (axis=1) using the numpy.split() function−
import numpy as np # array arr = np.arange(9).reshape(3, 3) # Split into 3 equal sub-arrays split_arr = np.split(arr, 3, axis=1) print("Original Array:") print(arr) print("\nSplit into 3 equal sub-arrays along axis 1:") for sub_arr in split_arr: print(sub_arr)
Following is the output obtained −
Original Array: [[0 1 2] [3 4 5] [6 7 8]] Split into 3 equal sub-arrays along axis 1: [[0] [3] [6]] [[1] [4] [7]] [[2] [5] [8]]
Example: Splitting at Specific Indices
Here, we are splitting an array at indices [1, 2] along the rows (axis=0) using the split() function in NumPy −
import numpy as np # array arr = np.arange(9).reshape(3, 3) # Split array at specified indices split_arr = np.split(arr, [1, 2], axis=0) print("\nSplit at indices [1, 2] along axis 0:") for sub_arr in split_arr: print(sub_arr)
This will produce the following result −
Split at indices [1, 2] along axis 0: [[0 1 2]] [[3 4 5]] [[6 7 8]]
Splitting Arrays Using array_split() Function
We can also use the array_split() function in NumPy to split an array into multiple sub-arrays along a specified axis. Unlike numpy.split() function, the array_split() function allows for unequal splits if the array cannot be evenly divided.
When the array does not evenly divide into the specified number of sections, the numpy.array_split() function ensures that the resulting sub-arrays are as equal in size as possible, distributing any extra elements to the earlier sub-arrays. Following is the syntax −
numpy.array_split(array, indices_or_sections, axis=0)
Where,
array − The input array to be split.
-
indices_or_sections − This can be either an integer or a 1D array of sorted integers.
If an integer, it specifies the number of equal-sized sub-arrays to create. The array must be divided as equally as possible.
If a 1D array of sorted integers, it specifies the points at which to split the array.
axis − The axis along which to split the array. The default is 0 (split along rows for 2D arrays).
Example
In the example below, we are splitting a 1D array with "10" elements into 3 unequal sub-arrays using numpy.array_split() function −
import numpy as np # array arr = np.arange(10) # Split into 3 sub-arrays along axis 0 split_arr = np.array_split(arr, 3) print("Original Array:") print(arr) print("\nSplit into 3 unequal sub-arrays:") for sub_arr in split_arr: print(sub_arr)
Following is the output of the above code −
Original Array: [0 1 2 3 4 5 6 7 8 9] Split into 3 unequal sub-arrays: [0 1 2 3] [4 5 6] [7 8 9]
Horizontal Splitting
We can split an array along the horizontal axis, which is axis = 1, for 2D arrays using the hsplit() function in NumPy. This function divides the array into sub-arrays horizontally, effectively separating columns of data. Following is the syntax −
numpy.hsplit(array, indices_or_sections)
Where,
array − The input array to be split.
indices_or_sections − Either an integer or a 1D array of indices that indicate how to split the array.
Example
In this example, we are splitting a 2D array "arr" along its columns into 2 equal parts using the numpy.hsplit() function −
import numpy as np # 2D array arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]]) # Split into 2 equal parts along axis 1 split_arr = np.hsplit(arr, 2) print("Original Array:") print(arr) print("\nSplit into 2 equal parts along axis 1:") for sub_arr in split_arr: print(sub_arr)
After executing the above code, we get the following output −
Original Array:[[1 2 3 4] [5 6 7 8]] Split into 2 equal parts along axis 1: [[1 2] [5 6]] [[3 4] [7 8]]
Vertical Splitting
We can also split an array along the vertical axis, which is axis = 0, for 2D arrays using the vsplit() function in NumPy. This function divides the array into sub-arrays vertically, effectively separating rows of data. Following is the syntax −
numpy.vsplit(array, indices_or_sections)
Where,
array − The input array to be split.
indices_or_sections − Either an integer or a 1D array of indices that indicate how to split the array.
Example
In the example below, we are splitting a 2D array "arr" along its rows into 3 equal parts using the numpy.vsplit() function −
import numpy as np # 2D array arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) # Split into 3 equal parts along axis 0 split_arr = np.vsplit(arr, 3) print("Original Array:") print(arr) print("\nSplit into 3 equal parts along axis 0:") for sub_arr in split_arr: print(sub_arr)
The output obtained is as shown below −
Original Array: [[1 2 3] [4 5 6] [7 8 9]] Split into 3 equal parts along axis 0: [[1 2 3]] [[4 5 6]] [[7 8 9]]
Depth Splitting
The numpy.dsplit() function is used to split a 3D array along its third dimension. This dimension is commonly referred to as the depth dimension, corresponding to axis=2. Following is the syntax −
numpy.dsplit(array, indices_or_sections)
Example
In this example, using the numpy.dsplit() function to split a 3D array "arr" into four equal parts along its third dimension −
import numpy as np # Example 3D array arr = np.arange(24).reshape((2, 3, 4)) # Split into 4 equal parts along axis 2 (depth) split_arr = np.dsplit(arr, 4) print("Original Array:") print(arr) print("\nSplit into 4 equal parts along axis 2 (depth):") for sub_arr in split_arr: print(sub_arr) print()
The result produced is as follows −
Original 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]]] Split into 4 equal parts along axis 2 (depth): [[[ 0] [ 4] [ 8]] [[12] [16] [20]]] [[[ 1] [ 5] [ 9]] [[13] [17] [21]]] [[[ 2] [ 6] [10]] [[14] [18] [22]]] [[[ 3] [ 7] [11]] [[15] [19] [23]]]