How to Use np.concatenate() in NumPy (With Examples and Comparisons)
NumPy, short for Numerical Python, is the go-to library for efficient numerical computation in Python. One of its most powerful features is multi-dimensional arrays, which are the building blocks for most operations in NumPy. Knowing how to join them effectively is crucial for any data scientist, data analyst, or developer working with numerical data.
In this beginner-friendly guide, we’ll walk through the different functions that we can use to join NumPy arrays, such as np.concatenate()
, np.stack()
, and more. Each function serves a specific purpose and works best under different conditions. By the end, we’ll have a clear understanding of which function to use and when, helping us write cleaner and more efficient code.
Let’s start the discussion with a brief introduction to NumPy arrays.
What are NumPy arrays?
NumPy arrays are the core data structure provided by the NumPy library, designed for efficient numerical computation in Python. They are known as ndarray
(short for N-dimensional array) and provide a fast, flexible way to store and operate on large datasets.
At their core, NumPy arrays are similar to Python lists, but they offer several advantages:
- Homogeneous data: Unlike Python lists, which can hold elements of different data types, NumPy arrays require all elements to be of the same data type. This consistency enables optimized performance.
- Multi-dimensional support: While Python lists are primarily one-dimensional (or can be nested to simulate higher dimensions), NumPy arrays can be one, two, or multi-dimensional with ease.
- Vectorized operations: NumPy allows us to perform element-wise operations without the need for explicit loops, resulting in more concise and faster code.
- Memory efficiency: NumPy arrays use less memory than equivalent Python lists, which is crucial when working with large datasets.
Here is an example that demonstrates the usage of NumPy arrays:
import numpy as np# Creating a 1D arrayarray_1d = np.array([1, 2, 3, 4, 5])# Creating a 2D arrayarray_2d = np.array([[1, 2], [3, 4], [5, 6]])print("1D Array:", array_1d)print("2D Array:\n", array_2d)
The output for this example is:
1D Array: [1 2 3 4 5]2D Array:[[1 2][3 4][5 6]]
Since we’ve now refreshed our memories for NumPy arrays, let’s learn how to join NumPy arrays using functions like np.concatenate()
, np.stack()
, and more in the next few sections.
Let’s first see how to join NumPy arrays using np.concatenate()
.
Learn Statistics with NumPy
Learn about NumPy, a Python library used to store arrays of numbers, organize large amounts of data, and perform statistical calculations. Try it for freeHow to join NumPy arrays using np.concatenate()
The np.concatenate()
function joins two NumPy arrays along an existing axis, which means joining along the rows (horizontally) or columns (vertically).
Here is the syntax for np.concatenate()
:
numpy.concatenate((a1, a2, ...), axis=0, out=None, dtype=None, casting="same_kind")
In the syntax:
(a1, a2, ...)
: A sequence (e.g., a tuple or list) of array-like objects that we want to concatenate.axis
(Optional): The axis along which the joining will be performed.- If set to
0
, the arrays are joined along the first dimension (i.e., vertically for 2D arrays). - If set to
1
, the arrays are joined along the second dimension (i.e., horizontally for 2D arrays). - If set to
None
, the arrays are flattened before concatenation.
- If set to
out
(Optional): A NumPy array for storing the result.dtype
(Optional): Specifies the desired data type for the output array.casting
(Optional): Defines what kind of data casting is allowed when specifyingdtype
. Common options include:"no"
: No casting allowed."equiv"
: Only byte-order changes allowed."safe"
: Only casts that preserve values are allowed."unsafe"
: Any data conversions are allowed, even if they lose information."same_kind"
: Only casts between similar kinds (e.g.,int
tofloat
).
This function returns a new array that contains the concatenation of the given arrays.
Next, let’s see an example that uses the np.concatenate()
function to join two NumPy arrays vertically (axis=0
):
import numpy as np# Creating two arraysarr1 = np.array([[11, 12], [13, 14]])arr2 = np.array([[15, 16], [17, 18]])# Joining the NumPy arrays vertically using np.concatenate()arr3 = np.concatenate((arr1, arr2), axis=0)print(arr3)
The output for this example will be:
[[11 12][13 14][15 16][17 18]]
Here is another example that uses the np.concatenate()
function to join two NumPy arrays horizontally (axis=1
):
import numpy as np# Creating two arraysarr1 = np.array([[21, 22], [23, 24]])arr2 = np.array([[25, 26], [27, 28]])# Joining the NumPy arrays horizontally using np.concatenate()arr3 = np.concatenate((arr1, arr2), axis=1)print(arr3)
Here is the output:
[[21 22 25 26][23 24 27 28]]
In the next section, we’ll discover how to join NumPy arrays using np.stack()
.
How to join NumPy arrays using np.stack()
Another function for joining NumPy arrays is np.stack()
. This function works just like np.concatenate()
, but there’s a little difference between them. The np.stack()
function joins two NumPy arrays along a new axis, whereas np.concatenate()
does that along an existing axis.
Specifically, np.stack()
creates a new array that includes one more dimension than the dimensions of the input arrays. Then, the function stacks the input arrays horizontally or vertically along the new dimension in the created array and returns it.
Here’s the syntax for the function:
numpy.stack((a1, a2, ...), axis=0, out=None, *, dtype=None, casting='same_kind')
Next, let’s check out an example that shows how to use the np.stack()
function to join two NumPy arrays vertically (axis=0
):
import numpy as np# Creating two NumPy arraysarr1 = np.array([12, 23, 34])arr2 = np.array([45, 56, 67])# Joining the arrays vertically using np.stack()arr3 = np.stack((arr1, arr2), axis=0)print(arr3)
Here is the output for the example:
[[12 23 34][45 56 67]]
Let’s go through another example that utilizes the np.stack()
function to join two NumPy arrays horizontally (axis=1
):
import numpy as np# Creating two NumPy arraysarr1 = np.array([12, 23, 34])arr2 = np.array([45, 56, 67])# Joining the arrays horizontally using np.stack()arr3 = np.stack((arr1, arr2), axis=1)print(arr3)
The output for the example will be:
[[12 45][23 56][34 67]]
The next function on the list is np.hstack()
. Let’s check that out next.
How to join NumPy arrays using np.hstack()
The np.hstack()
function joins two NumPy arrays horizontally (axis=1
).
Here is the syntax for it:
numpy.hstack((a1, a2, ...), *, dtype=None, casting='same_kind')
In the syntax:
(a1, a2, ...)
: A sequence (e.g., a tuple or list) of array-like objects that we want to concatenate.dtype
(Optional): Specifies the desired data type for the output array.casting
(Optional): Defines what kind of data casting is allowed when specifyingdtype
. Common options include:"no"
: No casting allowed."equiv"
: Only byte-order changes allowed."safe"
: Only casts that preserve values are allowed."unsafe"
: Any data conversions are allowed, even if they lose information."same_kind"
: Only casts between similar kinds (e.g.,int
tofloat
).
This function returns a new array in which the input arrays are horizontally stacked.
Let’s see an example that demonstrates how to use np.hstack()
to join two NumPy arrays horizontally:
import numpy as np# Creating two NumPy arraysarr1 = np.array([12, 23, 34])arr2 = np.array([45, 56, 67])# Joining the arrays horizontally using np.hstack()arr3 = np.hstack((arr1, arr2))print(arr3)
Here is the output:
[12 23 34 45 56 67]
Let’s move on to the next function, which is np.vstack()
.
How to join NumPy arrays using np.vstack()
The np.vstack()
function joins two NumPy arrays vertically (axis=0
).
The syntax for this function is:
numpy.vstack((a1, a2, ...), *, dtype=None, casting='same_kind')
In the syntax:
(a1, a2, ...)
: A sequence (e.g., a tuple or list) of array-like objects that we want to concatenate.dtype
(Optional): Specifies the desired data type for the output array.casting
(Optional): Defines what kind of data casting is allowed when specifyingdtype
. Common options include:"no"
: No casting allowed."equiv"
: Only byte-order changes allowed."safe"
: Only casts that preserve values are allowed."unsafe"
: Any data conversions are allowed, even if they lose information."same_kind"
: Only casts between similar kinds (e.g.,int
tofloat
).
This function returns a new array in which the input arrays are vertically stacked.
Here is an example showing the usage of np.vstack()
for joining two NumPy arrays vertically:
import numpy as np# Creating two NumPy arraysarr1 = np.array([12, 23, 34])arr2 = np.array([45, 56, 67])# Joining the arrays vertically using np.vstack()arr3 = np.vstack((arr1, arr2))print(arr3)
The output will be:
[[12 23 34][45 56 67]]
Last but not least, let’s discuss the np.dstack()
function in the next section.
How to join NumPy arrays using np.dstack()
The np.dstack()
function joins two NumPy arrays depth-wise (axis=2
).
Here is the syntax for the function:
numpy.dstack((a1, a2, ...), *, dtype=None, casting='same_kind')
In the syntax:
(a1, a2, ...)
: A sequence (e.g., a tuple or list) of array-like objects that we want to concatenate.dtype
(Optional): Specifies the desired data type for the output array.casting
(Optional): Defines what kind of data casting is allowed when specifyingdtype
. Common options include:"no"
: No casting allowed."equiv"
: Only byte-order changes allowed."safe"
: Only casts that preserve values are allowed."unsafe"
: Any data conversions are allowed, even if they lose information."same_kind"
: Only casts between similar kinds (e.g.,int
tofloat
).
This function returns a new array in which the input arrays are stacked depth-wise.
Let’s see an example that uses the np.dstack()
function to join two NumPy arrays depth-wise:
import numpy as np# Creating two NumPy arraysarr1 = np.array([12, 23, 34])arr2 = np.array([45, 56, 67])# Joining the arrays depth-wise using np.dstack()arr3 = np.dstack((arr1, arr2))print(arr3)
Here is the output:
[[[12 45][23 56][34 67]]]
With the functions covered, let’s navigate through the differences between np.concatenate()
and the other joining functions in NumPy in the next section.
np.concatenate()
vs. other joining functions
Here is a comparison table that compares the np.concatenate()
function with np.stack()
, np.hstack()
, np.vstack()
, and np.dstack()
:
Function | Purpose | Axis of Concatenation | Input Shape Requirement |
---|---|---|---|
np.concatenate() |
Joins arrays along an existing axis | Specified via axis (default is 0 ) |
Same shape except in the dimension specified by axis |
np.stack() |
Joins arrays along a new axis | New axis specified via axis |
All arrays must have the same shape |
np.hstack() |
Stacks arrays horizontally | Along the second axis for 2D arrays (axis=1 ) |
1D or 2D arrays of compatible shape |
np.vstack() |
Stacks arrays vertically | Along the first axis (axis=0 ) |
1D or 2D arrays of compatible shape |
np.dstack() |
Stacks arrays in depth | Along the third axis (axis=2 ) |
1D or 2D arrays of compatible shape |
If we choose these functions based on these scenarios, we’ll be able to get the maximum out of them.
Conclusion
In this tutorial, we had a detailed discussion on the functions used for joining NumPy arrays, including np.concatenate()
, np.stack()
, np.hstack()
, np.vstack()
, and np.dstack()
. We covered their syntax and went through some examples that made us understand their functionalities with clarity. We also checked out the differences between np.concatenate()
and the rest, which gave us a fair idea of when to use each.
Joining NumPy arrays is a crucial operation in data workflows as it helps us solve a diverse range of challenging problems seamlessly. By mastering it, we’ll be better equipped to tackle complex data analysis and scientific computing tasks in an efficient manner.
If you want to expand your knowledge of NumPy, check out the Learn Statistics with NumPy course on Codecademy.
Frequently Asked Questions
1. What is the difference between np.concatenate()
and np.stack()
?
np.concatenate()
joins arrays along an existing axis, while np.stack()
joins them along a new axis.
2. Can I join arrays of different shapes?
Only if they are compatible along the specified axis, for example, when using np.concatenate()
, all arrays must match in shape except for the axis you’re joining on.
3. What is the use of np.hstack()
vs. np.vstack()
?
np.hstack()
joins arrays horizontally, while np.vstack()
joins them vertically.
'The Codecademy Team, composed of experienced educators and tech experts, is dedicated to making tech skills accessible to all. We empower learners worldwide with expert-reviewed content that develops and enhances the technical skills needed to advance and succeed in their careers.'
Meet the full teamRelated articles
- Article
How to Split Arrays in NumPy?
Learn how to split NumPy arrays using functions like `np.split()`, `np.array_split()`, `np.hsplit()`, `np.vsplit()`, and `np.dsplit()`. A beginner-friendly guide with practical examples. - Article
Creating and Using NumPy Arrays - A Complete Guide
In this article, we will discuss how to create and use NumPy arrays. We will also go through various array operations, indexing, slicing, and reshaping with practical Python examples for data science. - Article
Sorting and Unary Operations in NumPy
Explore sorting and unary operations in NumPy arrays with examples for single and multi-dimensional data.
Learn more on Codecademy
- Course
Learn Statistics with NumPy
Learn about NumPy, a Python library used to store arrays of numbers, organize large amounts of data, and perform statistical calculations.With CertificateIntermediate4 hours - Free course
Learn C#: Arrays and Loops
Learn how to organize data and automate repetitive tasks with arrays and loops.Beginner Friendly3 hours - Free course
Learn C: Arrays and Strings
Sharpen your programming skills by learning how to create, assess, and modify arrays and strings in C.Beginner Friendly2 hours
- What are NumPy arrays?
- How to join NumPy arrays using `np.concatenate()`
- How to join NumPy arrays using `np.stack()`
- How to join NumPy arrays using `np.hstack()`
- How to join NumPy arrays using `np.vstack()`
- How to join NumPy arrays using `np.dstack()`
- `np.concatenate()` vs. other joining functions
- Conclusion
- Frequently Asked Questions