In this article, we will dsicuss how to join a sequence of numpy arrays along any given axis using concatenate() function.
Table of Contents:
- Overview of numpy.concatenate().
- Concatenate two 1D Numpy Arrays.
- Concatenate multiple 1D Numpy Arrays.
- Concatenate 2D Numpy Arrays row wise.
- Concatenate 2D Numpy Arrays column wise.
- Concatenate 2D Numpy Arrays by flattening the shapes.
Overview of numpy.concatenate()
Numpy library in python provides a function to concatenate two or more arrays along a given axis.
numpy.concatenate((a1, a2, …), axis=0, out=None)
Arguments:
- a1, a2,…: A Sequence of array_like like objects
- The arrays in sequence must be of shape same shape.
- axis: int, optional | Default value is 0.
- The axis along which we want the arrays to be joined.
- If axis is None, then all arrays are flattened and the..
- If axis is 0, then all arrays are joined row wise.
- If axis is 1, then all arrays are joined column wise.
- The axis along which we want the arrays to be joined.
- out: ndarray, optional
- If provided, place the result in the our array. The shape of our must match with expected result value of concatenate() function.
Returns:
- Returns a new ndarray i.e. a Numpy array containing the concatenated values from all input arrays.
Examples of numpy.concatenate()
Concatenate two 1D Numpy Arrays
Suppose we have two 1D NumPy Arrays and we want to join them one after another and create a merged array. For that we need to create a sequence of both the arrays and pass them to the numpy.concatenate() function. For example,
import numpy as np # A Numpy Array of integers first = np.array([1, 1, 1, 1, 1]) # Another Numpy Array of integers second = np.array([2, 2, 2, 2, 2]) # Concatenate two arrays to create a merged array merged_arr = np.concatenate( (first, second) ) print(merged_arr)
Output:
[1 1 1 1 1 2 2 2 2 2]
We created a tuple of 2 arrays and passed that to the concatenate() function. It returned a new merged array containing the contents of both the arrays.
Concatenate multiple 1D Numpy Arrays
To join multiple 1D Numpy Arrays, we can create a sequence of all these arrays and pass that sequence to concatenate() function. For example, let’s see how to join three numpy arrays to create a single merged array,
import numpy as np # Create three Numpy Arrays of integers first = np.array([1, 1, 1, 1, 1]) second = np.array([2, 2, 2, 2, 2]) third = np.array([3, 3, 3, 3, 3]) # Concatenate three arrays to create a merged array merged_arr = np.concatenate( (first, second, third) ) print(merged_arr)
Output:
[1 1 1 1 1 2 2 2 2 2 3 3 3 3 3]
Concatenate 2D Numpy Arrays row wise
To join two 2D Numpy Arrays row-wise, we need pass a sequence of arrays to concatenate() function along with value 0 for the axis parameter. It will insert all the rows of arrays one after another into a new array and returns the merged array. For example,
import numpy as np # Create 2D Numpy array of hard coded numbers first = np.array([[1, 1, 1], [2, 2, 2], [3, 3, 3]]) # Create another 2D Numpy array of hard coded numbers second = np.array([ [4, 4, 4], [6, 5, 5], [6, 6, 6]]) # Concatenate 2D Numpy Arrays row wise # Merge two 2D arrays row-wise merged_arr = np.concatenate( (first, second), axis=0 ) print(merged_arr)
Output:
[[1 1 1] [2 2 2] [3 3 3] [4 4 4] [6 5 5] [6 6 6]]
Concatenate 2D Numpy Arrays column wise
To join two 2D Numpy Arrays column-wise, we need pass a sequence of arrays to concatenate() function along with value 1 for the axis parameter. It will insert all the columns of arrays, one after another into a new array and returns the merged array. For example,
import numpy as np # Create 2D Numpy array of hard coded numbers first = np.array([[1, 1, 1], [2, 2, 2], [3, 3, 3]]) # Create another 2D Numpy array of hard coded numbers second = np.array([ [4, 4, 4], [6, 5, 5], [6, 6, 6]]) # Concatenate 2D Numpy Arrays column wise merged_arr = np.concatenate( (first, second), axis=1 ) print(merged_arr)
Output:
[[1 1 1 4 4 4] [2 2 2 6 5 5] [3 3 3 6 6 6]]
Concatenate 2D Numpy Arrays by flattening the shape
If we pass None as value for axis parameter, then concatenate() function will flatten all the arrays and join them. For example,
import numpy as np # Create 2D Numpy array of hard coded numbers first = np.array([[1, 1, 1], [2, 2, 2], [3, 3, 3]]) # Create another 2D Numpy array of hard coded numbers second = np.array([ [4, 4, 4], [6, 5, 5], [6, 6, 6]]) # Concatenate 2D Numpy Arrays by flattening the shape merged_arr = np.concatenate( (first, second), axis=None ) print(merged_arr)
Output:
[1 1 1 2 2 2 3 3 3 4 4 4 6 5 5 6 6 6]
Summary:
In this article, we learned that how we can join NumPy Arrays of different size and shapes.
Pandas Tutorials -Learn Data Analysis with Python
-
Pandas Tutorial Part #1 - Introduction to Data Analysis with Python
-
Pandas Tutorial Part #2 - Basics of Pandas Series
-
Pandas Tutorial Part #3 - Get & Set Series values
-
Pandas Tutorial Part #4 - Attributes & methods of Pandas Series
-
Pandas Tutorial Part #5 - Add or Remove Pandas Series elements
-
Pandas Tutorial Part #6 - Introduction to DataFrame
-
Pandas Tutorial Part #7 - DataFrame.loc[] - Select Rows / Columns by Indexing
-
Pandas Tutorial Part #8 - DataFrame.iloc[] - Select Rows / Columns by Label Names
-
Pandas Tutorial Part #9 - Filter DataFrame Rows
-
Pandas Tutorial Part #10 - Add/Remove DataFrame Rows & Columns
-
Pandas Tutorial Part #11 - DataFrame attributes & methods
-
Pandas Tutorial Part #12 - Handling Missing Data or NaN values
-
Pandas Tutorial Part #13 - Iterate over Rows & Columns of DataFrame
-
Pandas Tutorial Part #14 - Sorting DataFrame by Rows or Columns
-
Pandas Tutorial Part #15 - Merging or Concatenating DataFrames
-
Pandas Tutorial Part #16 - DataFrame GroupBy explained with examples
Are you looking to make a career in Data Science with Python?
Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.
Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.
Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.