How to Join 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 join different numpy arrays horizontally and vertically using different methods.We can merge multiple numpy arrays to form a new numpy array which will contain all the arrays given as input. Numpy provides the stacking concept which contains a number of functions that will help in merging the arrays.
We can perform vertical stacking to join two numpy arrays vertically with the help of np.vstack() method which will add the numpy arrays as new rows of new numpy array.In this case the array grows in vertical direction.We can implement the vstack() operation by below mentioned code.
import numpy as np
A=np.zeros((3,3))
print("Array A is:")
print(A)
B=np.ones((3,3))
print("Array B is:")
print(B)
print("After performing vertical stack Array grows as:")
np.vstack((A, B))

Output:
Array A is:
[[0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]]
Array B is:
[[1. 1. 1.]
 [1. 1. 1.]
 [1. 1. 1.]]
After performing vertical stack Array grows as:
array([[0., 0., 0.],
       [0., 0., 0.],
       [0., 0., 0.],
       [1., 1., 1.],
       [1., 1., 1.],
       [1., 1., 1.]])
In the above example we can see that  array A and B have the same shape. np.vstack() method takes a tuple of numpy arrays as an argument which must be in the same shape in all directions except vertically downwards direction across rows. If we pass a tuple of 1-D arrays as an argument, they must be of the same length.
We can join two numpy arrays horizontally by performing horizontal stacking with the help of np.hstack() method which adds numpy arrays as new columns of the output array.Hence,Array grows horizontally.
Below given implementation shows how hstack() works:
import numpy as np
A=np.zeros((3,3))
print("Array A is:")
print(A)
B=np.ones((3,3))
print("Array B is:")
print(B)
print("After performing vertical stack Array grows as:")
np.hstack((A, B))

Output:
Array A is:
[[0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]]

Array B is:
[[1. 1. 1.]
 [1. 1. 1.]
 [1. 1. 1.]]
After performing horizontal stack Array grows as:
array([[0., 0., 0., 1., 1., 1.],
       [0., 0., 0., 1., 1., 1.],
       [0., 0., 0., 1., 1., 1.]])

Here, np.hstack() takes a tuple of numpy arrays as argument.Also, the arrays in the tuple can be of any shape along any axis except the horizontal direction across columns. If we want to pass 1-D arrays in the tuple, they must be of the same length.
Now,we will see some functions that generally work with one-dimensional and two-dimensional arrays and perform stacking between multiple arrays.These functions are column_stack() and row_stack().
We can make 2-D numpy arrays from columns given in the form of 1-D numpy arrays using the  np.column_stack() method which stacks arrays as columns to form new two-dimensional array.The operation can be understood from following example :
import numpy as np
a=np.array([1,2,3])
b=np.array([4,5,6])
c=np.array([7,8,9])
print("After performing column_stack() the new two-dimensional array is:")
print(np.column_stack((a,b,c)))

Output:
After performing column_stack() the new two-dimensional array is:
[[1 4 7]
 [2 5 8]
 [3 6 9]]

In the above example, np.column_stack() takes a tuple of arrays as argument and returns a numpy array  formed by stacking the given arrays. The 1-D arrays passed as input must be of the same length. We can also pass 2-D arrays along with 1-D arrays as arguments given the condition that the arrays have columns of the same length.
For joining 1-D arrays to make them rows of the new 2-D array, we can use np.row_stack() method which joins the 1-D arrays in such a way that they constitute rows of the new 2-D array. The following example shows how to perform the operation.
import numpy as np
a=np.array([1,2,3])
b=np.array([4,5,6])
c=np.array([7,8,9])
print("After performing row_stack() the new two-dimensional array is:")
print(np.row_stack((a,b,c)))

Output:
After performing row_stack() the new two-dimensional array is:
[[1 2 3]
 [4 5 6]
 [7 8 9]]

Here, np.row_stack() method takes a tuple of numpy arrays as input and returns a new numpy array which has input arrays as it’s rows. Here we can also stack 2-D arrays along with 1-D arrays with np.row_stack() method given the condition that rows of the input arrays must be of same length.
In this tutorial we have seen how to join arrays horizontally and vertically using different methods of Numpy module like hstack(),vstack(), row_stack() and column_stack(). We have also seen the details of the different methods about their input and output.
NumPy Arrays
Numpy Array Shape Manipulation
Happy Learning 🙂

Slicing NumPy Arrays
Indexing in NumPy Arrays
Shape Manipulation of  Numpy Arrays
Arithmetic Operations on NumPy Arrays
NumPy Matrix Multiplication Example
What is Python NumPy Library
PHP Multidimensional Arrays Example
Java Program to swap two arrays Example
What is Java Arrays and how it works ?
Fork and Join in Java 7 Example
Thread  join Example in Java
Hibernate Left Join Example
How to Remove Spaces from String in Python
Hibernate Right Join Example
How to Sort an Array in Parallel in Java 8

Slicing NumPy Arrays
Indexing in NumPy Arrays
Shape Manipulation of  Numpy Arrays
Arithmetic Operations on NumPy Arrays
NumPy Matrix Multiplication Example
What is Python NumPy Library
PHP Multidimensional Arrays Example
Java Program to swap two arrays Example
What is Java Arrays and how it works ?
Fork and Join in Java 7 Example
Thread  join Example in Java
Hibernate Left Join Example
How to Remove Spaces from String in Python
Hibernate Right Join Example
How to Sort an Array in Parallel in Java 8
 


Δ
 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