Numpy Tutorial
Creating NumPy Array
NumPy Array Manipulation
Matrix in NumPy
Operations on NumPy Array
Reshaping NumPy Array
Indexing NumPy Array
Arithmetic operations on NumPy Array
Linear Algebra in NumPy Array
NumPy and Random Data
Sorting and Searching in NumPy Array
Universal Functions
Working With Images
Projects and Applications with NumPy
In NumPy, stacking arrays horizontally means adding arrays column-wise. The function for this purpose is numpy.hstack()
. Let's dive into how it's used.
The numpy.hstack()
function is used to stack arrays in sequence horizontally (i.e., column-wise).
If you haven't installed NumPy yet, you can do so with:
pip install numpy
Begin by importing NumPy:
import numpy as np
hstack()
function:Given two or more arrays, you can stack them horizontally:
arr1 = np.array([[1, 2], [3, 4]]) arr2 = np.array([[5, 6], [7, 8]]) stacked_array = np.hstack((arr1, arr2)) print(stacked_array)
Output:
[[1 2 5 6] [3 4 7 8]]
You can stack more than two arrays at once:
arr3 = np.array([[9, 10], [11, 12]]) stacked_array = np.hstack((arr1, arr2, arr3)) print(stacked_array)
Output:
[[ 1 2 5 6 9 10] [ 3 4 7 8 11 12]]
If you want to horizontally stack arrays of different dimensions, one option is to use the reshape
or column_stack
functions to ensure they have matching shapes before stacking:
arr1 = np.array([1, 2, 3]) arr2 = np.array([[4, 5, 6], [7, 8, 9]]) # Reshape arr1 to match arr2's shape arr1_reshaped = arr1.reshape(2, -1) stacked_array = np.hstack((arr1_reshaped, arr2)) print(stacked_array)
Output:
[[1 4 5 6] [2 7 8 9]]
Note: In the above example, -1
in reshape
automatically calculates the necessary number of columns.
concatenate
:You can also use the concatenate
function with the axis=1
argument to achieve horizontal stacking:
stacked_array = np.concatenate((arr1, arr2), axis=1) print(stacked_array)
The hstack()
function in NumPy is a handy tool for horizontally stacking arrays. Whether you're working with two arrays or several arrays, it offers an efficient and intuitive way to combine data column-wise. Remember to ensure that the arrays are of compatible shapes for stacking.
Description: Horizontal stacking of arrays in NumPy refers to concatenating arrays side by side along a specified axis.
Code:
import numpy as np # Create two 1D NumPy arrays array1 = np.array([1, 2, 3]) array2 = np.array([4, 5, 6]) # Horizontal stack the arrays stacked_array = np.hstack((array1, array2)) print("Array 1:") print(array1) print("Array 2:") print(array2) print("Horizontally Stacked Array:") print(stacked_array)
Description: Concatenating arrays side by side in NumPy can be achieved using functions like numpy.hstack
.
Code:
import numpy as np # Create two 1D NumPy arrays array1 = np.array([1, 2, 3]) array2 = np.array([4, 5, 6]) # Concatenate arrays horizontally concatenated_array = np.hstack((array1, array2)) print("Array 1:") print(array1) print("Array 2:") print(array2) print("Concatenated Array:") print(concatenated_array)
Description: The numpy.hstack
function in Python NumPy is specifically designed for horizontally stacking arrays.
Code:
import numpy as np # Create two 1D NumPy arrays array1 = np.array([1, 2, 3]) array2 = np.array([4, 5, 6]) # Use hstack to horizontally stack arrays stacked_array = np.hstack((array1, array2)) print("Array 1:") print(array1) print("Array 2:") print(array2) print("Horizontally Stacked Array:") print(stacked_array)
Description: Stacking arrays along columns in NumPy can be done using functions like numpy.hstack
for 1D arrays or numpy.column_stack
for 2D arrays.
Code:
import numpy as np # Create two 1D NumPy arrays array1 = np.array([1, 2, 3]) array2 = np.array([4, 5, 6]) # Stack arrays along columns stacked_array = np.column_stack((array1, array2)) print("Array 1:") print(array1) print("Array 2:") print(array2) print("Stacked Array Along Columns:") print(stacked_array)
Description: Horizontally combining NumPy arrays in Python can be achieved using functions like numpy.hstack
or numpy.column_stack
.
Code:
import numpy as np # Create two 1D NumPy arrays array1 = np.array([1, 2, 3]) array2 = np.array([4, 5, 6]) # Combine arrays horizontally combined_array = np.hstack((array1, array2)) print("Array 1:") print(array1) print("Array 2:") print(array2) print("Horizontally Combined Array:") print(combined_array)
Description: NumPy horizontal concatenation along axis 1 is achieved using functions like numpy.hstack
or numpy.concatenate
with the appropriate axis parameter.
Code:
import numpy as np # Create two 2D NumPy arrays array1 = np.array([[1, 2], [3, 4]]) array2 = np.array([[5, 6], [7, 8]]) # Horizontal concatenation along axis 1 concatenated_array = np.concatenate((array1, array2), axis=1) print("Array 1:") print(array1) print("Array 2:") print(array2) print("Horizontal Concatenation along Axis 1:") print(concatenated_array)
Description: Concatenating multiple arrays horizontally in NumPy can be done using functions like numpy.hstack
or numpy.concatenate
.
Code:
import numpy as np # Create three 1D NumPy arrays array1 = np.array([1, 2, 3]) array2 = np.array([4, 5, 6]) array3 = np.array([7, 8, 9]) # Concatenate multiple arrays horizontally concatenated_array = np.hstack((array1, array2, array3)) print("Array 1:") print(array1) print("Array 2:") print(array2) print("Array 3:") print(array3) print("Concatenated Array:") print(concatenated_array)
Description: Efficient ways to stack arrays side by side in NumPy involve using functions like numpy.hstack
or numpy.column_stack
for 1D arrays, and numpy.concatenate
for 2D arrays.
Code:
import numpy as np # Create two 1D NumPy arrays array1 = np.array([1, 2, 3]) array2 = np.array([4, 5, 6]) # Efficiently stack arrays side by side stacked_array = np.hstack((array1, array2)) print("Array 1:") print(array1) print("Array 2:") print(array2) print("Stacked Array:") print(stacked_array)
Description: Joining arrays horizontally using NumPy can be achieved using functions like numpy.hstack
for 1D arrays or numpy.concatenate
for 2D arrays.
Code:
import numpy as np # Create two 1D NumPy arrays array1 = np.array([1, 2, 3]) array2 = np.array([4, 5, 6]) # Join arrays horizontally joined_array = np.hstack((array1, array2)) print("Array 1:") print(array1) print("Array 2:") print(array2) print("Joined Array Horizontally:") print(joined_array)