How to split Numpy Arrays ? - onlinetutorialspoint

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
In this tutorial, we will see how to divide an array into multiple parts. We will use the splitting operation of NumPy arrays to split arrays into rows or columns.
We can use the hsplit() method of NumPy module to split a given NumPy array into equal parts into columns. Here in the following example, we will implement code to divide an array of size 4×4 into two equal parts of size 4×2.
import numpy as np
A = np.arange(16).reshape((4, 4))
print('Array A is:')
print(A)
[B,C] = np.hsplit(A, 2)
print('Array B is:')
print(B)
print(Array C is:)
print(C)

Output:
Array A is:
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]]

Array B is:
[[ 0  1]
 [ 4  5]
 [ 8  9]
 [12 13]]
 
Array C is:
[[ 2  3]
 [ 6  7]
 [10 11]
 [14 15]]

np.hsplit() takes the input array to be split as its first argument and number of parts as the second argument. hsplit() method returns a list of NumPy arrays which are the parts of the input array which may be a column or array consisting of many columns.
To divide a NumPy array into rows or groups of rows having an equal number of rows, we can use the vsplit() method of NumPy module. As shown in the following example, we can split a 4×4 NumPy array into two arrays of size 2×4.
import numpy as np
A = np.arange(16).reshape((4, 4))
print('Array A is:')
print(A)

[B,C] = np.vsplit(A, 2)
print('Array B is:')
print(B)
print(Array C is:)
print(C)

Output:
Array A is:
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]]

Array B is:
[[0, 1, 2, 3],
 [4, 5, 6, 7]]

Array C is:
[[ 8,  9, 10, 11],
 [12, 13, 14, 15]]

np.vsplit() method takes the array to be divided into rows as the first argument and number of parts to be done as a second argument as input. It returns a list of NumPy arrays which consist of rows of input arrays which were passed as arguments to the vsplit() method.
We can split NumPy arrays non symmetrically using the split() method of NumPy module. To split an array into non-equal parts, we will have to specify whether we have to split the array horizontally or vertically and indices of the rows or columns from which we have to split the array. np.split() method takes input array to be split as the first argument and a list of indices from which the array has to be split as the second argument. Axis along which the array has to be split is provided as a third argument. After splitting the array, it returns a list of NumPy arrays as output.
To split a NumPy array vertically i.e along the columns then we will have to specify the indices of columns by which the array will be split and axis=1 will be provided as an argument to split the array along columns. Following example illustrates the implementation in which we split a 4×4 NumPy array into two arrays of size 4×1 and an array of size 4×2.
import numpy as np
A = np.arange(16).reshape((4, 4))
print('Array A is:')
print(A)
[A1,A2,A3] = np.split(A,[1,3],axis=1)
print('Array A1 is:')
print(A1)
print('Array A2 is:')
print(A2)
print('Array A3 is:')
print(A3)

Output:
Array A is:
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]]
Array A1 is:
[[ 0]
 [ 4]
 [ 8]
 [12]]
Array A2 is:
[[ 1  2]
 [ 5  6]
 [ 9 10]
 [13 14]]
Array A3 is:
[[ 3]
 [ 7]
 [11]
 [15]]

In the above example, index 1 and 3 as passed in a list and the input array is split from the 2nd and 4th column. Here the column index specified in the input will be in the right subarray in output.
To split a NumPy array horizontally i.e along the rows then we will have to specify the indices of rows by which the array will be split and axis=0 will be provided as an argument to split the array along rows. Following example illustrates the implementation in which we split a 4×4 NumPy array into two arrays of size  1×4 and an array of size 2×4.
import numpy as np
A = np.arange(16).reshape((4, 4))
print('Array A is:')
print(A)
[A1,A2,A3] = np.split(A,[1,3],axis=0)
print('Array A1 is:')
print(A1)
print('Array A2 is:')
print(A2)
print('Array A3 is:')
print(A3)

Output for the above code snippet is:
Array A is:
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]]
 
Array A1 is:
[[0, 1, 2, 3]]
 
Array A2 is:
[[ 4  5  6  7]
 [ 8  9 10 11]]

Array A3 is:
[[12 13 14 15]]

In the above example, index 1 and 3 as passed in a list and the input array is split from the 2nd and 4th row. Here the row index specified in the input will be in the lower sub-array in output.
In this tutorial, we saw how to split arrays both vertically and horizontally into equal parts. We also looked at methods to split the arrays into non-equal parts horizontally and vertically.
NumPy Arrays Library
NumPy Array Shape Manipulation
NumPy Array Doc
Happy Learning 🙂

How to Join NumPy Arrays ?
Slicing NumPy Arrays
Indexing in NumPy Arrays
Arithmetic Operations on NumPy Arrays
Shape Manipulation of  Numpy Arrays
What is Python NumPy Library
NumPy Matrix Multiplication Example
Java Program to swap two arrays Example
PHP Multidimensional Arrays Example
How to Split String in Java example
What is Java Arrays and how it works ?
Python How to read input from keyboard
How to do Multiprocessing in Python
Angularjs Custom Filter Example
Fork and Join in Java 7 Example

How to Join NumPy Arrays ?
Slicing NumPy Arrays
Indexing in NumPy Arrays
Arithmetic Operations on NumPy Arrays
Shape Manipulation of  Numpy Arrays
What is Python NumPy Library
NumPy Matrix Multiplication Example
Java Program to swap two arrays Example
PHP Multidimensional Arrays Example
How to Split String in Java example
What is Java Arrays and how it works ?
Python How to read input from keyboard
How to do Multiprocessing in Python
Angularjs Custom Filter Example
Fork and Join in Java 7 Example
 


Δ
 Python – Introduction
 Python – Features
 Python – Install on Windows
 Python – Modes of Program
 Python – Number System
 Python – Identifiers
 Python – Operators
 Python – Ternary Operator
 Python – Command Line Arguments
 Python – Keywords
 Python – Data Types
 Python – Upgrade Python PIP
 Python – Virtual Environment
 Pyhton – Type Casting
 Python – String to Int
 Python – Conditional Statements
 Python – if statement
 Python – *args and **kwargs
 Python – Date Formatting
 Python – Read input from keyboard
 Python – raw_input
 Python – List In Depth
 Python – List Comprehension
 Python – Set in Depth
 Python – Dictionary in Depth
 Python – Tuple in Depth
 Python – Stack Datastructure
 Python – Classes and Objects
 Python – Constructors
 Python – Object Introspection
 Python – Inheritance
 Python – Decorators
 Python – Serialization with Pickle
 Python – Exceptions Handling
 Python – User defined Exceptions
 Python – Multiprocessing
 Python – Default function parameters
 Python – Lambdas Functions
 Python – NumPy Library
 Python – MySQL Connector
 Python – MySQL Create Database
 Python – MySQL Read Data
 Python – MySQL Insert Data
 Python – MySQL Update Records
 Python – MySQL Delete Records
 Python – String Case Conversion
 Howto – Find biggest of 2 numbers
 Howto – Remove duplicates from List
 Howto – Convert any Number to Binary
 Howto  – Merge two Lists
 Howto – Merge two dicts
 Howto – Get Characters Count in a File
 Howto – Get Words Count in a File
 Howto – Remove Spaces from String
 Howto –  Read Env variables
 Howto – Read a text File
 Howto – Read a JSON File
 Howto –  Read Config.ini files
 Howto – Iterate Dictionary
 Howto – Convert List Of Objects to CSV
 Howto – Merge two dict in Python
 Howto – create Zip File
 Howto – Get OS info
 Howto – Get size of Directory
 Howto – Check whether a file exists
 Howto –  Remove key from dictionary
 Howto – Sort Objects
 Howto – Create or Delete Directories
 Howto – Read CSV File
 Howto – Create Python Iterable class
 Howto – Access for loop index
 Howto – Clear all elements from List
 Howto – Remove empty lists from a List
 Howto – Remove special characters from String
 Howto – Sort dictionary by key
 Howto – Filter a list