How to Convert 1D Array of Tuples to 2D Numpy Array?


When working with data in Python, it is often necessary to transform and manipulate arrays to facilitate analysis and computations. One common scenario is converting a 1D array of tuples into a 2D Numpy array. This conversion allows for easier indexing, slicing, and applying array operations.

In this article, our focus will be on the conversion process of transforming a 1D array of tuples into a Numpy array.

1D Array of Tuple

A 1D array of tuples refers to a data structure in which tuples are arranged sequentially in a one−dimensional array.

For example, consider the following 1D array of tuples:

data = [(1, 2), (3, 4), (5, 6), (7, 8)]

2D Numpy array

A 2D Numpy array, also known as a two−dimensional array or matrix, is a data structure provided by the Numpy library in Python. It extends the concept of a one−dimensional array by introducing additional dimensions, creating a grid−like structure of rows and columns.

Below are the different approaches mentioned where I have discussed different approaches to convert 1D array of tuples to 2D Numpy array.

Using the numpy.array() Function

In this approach, we will utilise the numpy.array() function to convert the 1D array of tuples into a Numpy array.

Consider the code shown below.

Example

import numpy as np

# Sample 1D array of tuples
data = [(1, 2), (3, 4), (5, 6), (7, 8)]

# Convert 1D array of tuples to Numpy array
numpy_array = np.array(data)

# Print the Numpy array
print("Converted Numpy array:")
print(numpy_array)

Explanation

  • We import the `numpy` library as `np`.

  • We define the 1D array of tuples named `data`.

  • By using the `np.array()` function and passing `data` as the argument, we convert the 1D array of tuples into a Numpy array.

  • Finally, we print the resulting Numpy array.

  • The output demonstrates the converted Numpy array, where each tuple becomes a row in the 2D array.

Output

# Converted Numpy array:
[[1 2]
[3 4]
[5 6]
[7 8]]

Using List Comprehension and numpy.vstack()

In this approach, we will utilise list comprehension to extract the tuples' elements and then utilise numpy.vstack() to vertically stack them into a 2D Numpy array.

Consider the code shown below.

Example

import numpy as np

# Sample 1D array of tuples
data = [(1, 2), (3, 4), (5, 6), (7, 8)]

# Convert 1D array of tuples to Numpy array
numpy_array = np.vstack([np.array(t) for t in data])

# Print the Numpy array
print("Converted Numpy array:")
print(numpy_array)

Explanation

  • We import the `numpy` library as `np`.

  • We define the 1D array of tuples named `data`.

  • Using list comprehension, we iterate over each tuple `t` in `data` and convert it into a Numpy array using `np.array(t)`.

  • We then utilise `np.vstack()` to vertically stack the resulting Numpy arrays into a 2D Numpy array.

  • Finally, we print the resulting Numpy array.

  • The output demonstrates the converted Numpy array, where each tuple becomes a row in the 2D array.

Output

# Converted Numpy array:
 [[1 2]
 [3 4]
 [5 6]
 [7 8]]

Using the numpy.reshape() Function

In this approach, we will utilise the numpy.reshape() function to convert the 1D array of tuples into a 2D Numpy array.

Consider the code shown below.

Example

import numpy as np

# Sample 1D array of tuples
data = [(1, 2), (3, 4), (5, 6), (7, 8)]

# Convert 1D array of tuples to Numpy array
numpy_array = np.array(data).reshape((-1, 2))

# Print the Numpy array
print("Converted Numpy array:")
print(numpy_array)

Explanation

  • We import the `numpy` library as `np`.

  • We define the 1D array of tuples named `data`.

  • By using `np.array(data)` we convert the 1D array of tuples into a Numpy array.

  • We then apply the `reshape((-1, 2))` function on the Numpy array, which reshapes it into a 2D array with 2 columns, automatically determining the number of rows.

  • Finally, we print the resulting Numpy array.

  • The output demonstrates the converted Numpy array, where each tuple becomes a row in the 2D array.

Output

# Converted Numpy array:
 [[1 2]
 [3 4]
 [5 6]
 [7 8]]

Conclusion

Converting a 1D array of tuples into a 2D Numpy array is a common task in data manipulation and analysis. In this article, we explored three different approaches to achieve this transformation: using the numpy.array() function, employing list comprehension and numpy.vstack(), and utilising the numpy.reshape() function.

Updated on: 04-Aug-2023

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements