Using numpy.column_stack() function (5 examples)

Updated: February 29, 2024 By: Guest Contributor Post a comment

Introduction

The numpy.column_stack() function is a powerful tool in the NumPy library, enabling users to stack 1D or 2D arrays as columns into a 2D array. This function is particularly useful for data manipulation and analysis in Python. This tutorial provides a comprehensive guide on how to use the numpy.column_stack() function, illustrated with five examples of increasing complexity.

What does numpy.column_stack() Do?

Before diving into examples, it’s essential to understand what column_stack() does. Given sequences of 1D or 2D arrays, column_stack() stacks them as columns to form a 2D array. It’s an intuitive way to combine data when dealing with rows and columns.

Syntax:

numpy.column_stack(tup)

Parameters:

  • tup: sequence of array_like. Any number of arrays and sequences. 1-D or 2-D arrays must be the same length.

Returns:

  • stacked: 2-D array. An array with the input arrays stacked as columns.

Example 1: Stacking 1D Arrays

import numpy as np

# Define two 1D arrays
array1 = np.array([1, 2, 3])
array2 = np.array([4, 5, 6])

# Use column_stack to combine the arrays
result = np.column_stack((array1, array2))

# Output the result
print(result)

The output of this example will be:

[[1 4]
 [2 5]
 [3 6]]

This example demonstrates the basic usage of column_stack() for combining two 1D arrays into a single 2D array.

Example 2: Stacking 2D Arrays

import numpy as np

# Define two 2D arrays
array1 = np.array([[1, 2], [3, 4]])
array2 = np.array([[5, 6], [7, 8]])

# Stack them as columns
result = np.column_stack((array1, array2))

# Output the result
print(result)

The output:

[[1 2 5 6]
 [3 4 7 8]]

This example shows how column_stack() can also effectively combine 2D arrays, appending each array as a new column in the resultant 2D array.

Example 3: Stacking Arrays of Different Dimensions

import numpy as np

# Define a 1D array and a 2D array
array1 = np.array([1, 2])
array2 = np.array([[3, 4], [5, 6]])

# Use column_stack to combine them
result = np.column_stack((array1, array2))

# Output the result
print(result)

The output will be:

[[1 3 4]
 [2 5 6]]

In this example, we see that column_stack() is flexible enough to handle arrays of different dimensions, seamlessly integrating a 1D array with a 2D array.

Example 4: Using column_stack with Multidimensional Arrays

import numpy as np

# Create multidimensional arrays
arrays = [np.random.rand(2, 2) for _ in range(3)]

# Stack them using column_stack
result = np.column_stack(arrays)

# Display the result
print(result)

This advanced example demonstrates column_stack()‘s capability to handle a list of multidimensional arrays, creating a complex 2D array that incorporates all the given arrays as columns.

Example 5: Real-world Application – Combining Features into a Data Matrix

import numpy as np
import pandas as pd

# Assume we have feature vectors stored as series in a pandas DataFrame
data = pd.DataFrame({'feature1': np.random.rand(5),
                     'feature2': np.random.rand(5),
                     'feature3': np.random.rand(5)})

# Convert the pandas series to numpy arrays
arrays = [data[feature].values for feature in data]

# Combine the feature vectors using column_stack
features_matrix = np.column_stack(arrays)

# Output the result, which is ready for machine learning models
print(features_matrix)

This final example ties together a practical application of column_stack() in data science. By converting features from a pandas DataFrame into numpy arrays and stacking them, we prepare a data matrix suitable for machine learning algorithms.

Conclusion

The numpy.column_stack() function is a versatile tool that simplifies the task of combining arrays into a 2D matrix, accommodating a wide range of data structures. Through practical examples, this tutorial demonstrates its utility in both simple and complex data manipulation scenarios, highlighting its importance in Python data science.