Convert tuple to NumPy Array in Python

In this post, we are going to understand how to Convert tuple to NumPy Array in Python that includes N-Dimensional array,2-Dimensional array, and 1-Dimensional numpy array.The numpy library two functions np.array() or np.asarray() is used to convert array.

NumPy function to Convert tuple to NumPy Array


  • np.array()
  • np.asarray()

1. Convert nested tuples to n-Dimensional Array


In our first example, we will use create a multi-dimensional array bypassing a nested tuple of mixed data types to np. array() function.The numpy array takes a python object as an argument and converts it into an array. To use the numpy library first we have installed it on the system and later import it using “import numpy as np”

import numpy as np

numtuple = ((5, 6,7,'A'), (7,9,8,'B'),(10,11,9,'C#'),(12,13,19,'D'),(14,15,18,'F'),(16,17,19,'G'))

#tuples to array

array_of_tuple = np.array(numtuple)

print('tuples to Multi-Dimensional array =\n' ,array_of_tuple)

Output:

tuples to Multi-Dimensional array =
 [['5' '6' '7' 'A']
 ['7' '9' '8' 'B']
 ['10' '11' '9' 'C#']
 ['12' '13' '19' 'D']
 ['14' '15' '18' 'F']
 ['16' '17' '19' 'G']]

2. Convert tuple to 2D NumPy Array


In this code snippet, The first step is to import NumPy as np then convert a nested tuple to 2Dimensional arrays by passing nested tuples to the np. array() function.

Code Example

import numpy as np

numtuple = ((5, 6), (7,9),(10,11),(12,13),(14,15),(16,17))

#tuples to array

array_of_tuple = np.array(numtuple)

print('tuples to array =\n' ,array_of_tuple) 

Output:

tuples to array =
 [[ 5  6]
 [ 7  9]
 [10 11]
 [12 13]
 [14 15]
 [16 17]]

3. Convert tuple to 1D NumPy array


we are converting the Tuple to one Dimensional Array, passing the tuple to array() method of NumPy, and printing the result. Let’s understand with an example

Code Example

import numpy as np

numtuple = (5, 6, 7,9,10,11,12,13,14,15,16,17)

#tuple to array

array_of_tuple = np.array(numtuple)

print('tuple to array =\n' ,array_of_tuple)      

Output:

tuple to array =
 [ 5  6  7  9 10 11 12 13 14 15 16 17]

4. np.asarray() to Convert nested tuple to numpy array


The numpy.asarray() function convert the input into numpy array. We have imported the numpy library using “import numpy as np” then pass nested tuple to asarray() function to convert a nested tuple to numpy n-dimensional array.

Syntax

numpy.asarray(input, dtype=None, order=None, *, like=None)

Parameters

  • input : any input that can be converted to array.
  • dtype:The datatype is interferred from the input data.
  • order :Memory order C,F,A.K depend upon order of input array.
  • like : array-like
import numpy as np

numtuple = ((5, 6,7,'A'), (7,9,8,'B'),(10,11,9,'C#'),(12,13,19,'D'),(14,15,18,'F'),(16,17,19,'G'))

#tuples to array

array_of_tuple = np.asarray(numtuple)

print('tuples to n-D numpy array =\n' ,array_of_tuple)

Output

tuples to n-D numpy array =
 [['5' '6' '7' 'A']
 ['7' '9' '8' 'B']
 ['10' '11' '9' 'C#']
 ['12' '13' '19' 'D']
 ['14' '15' '18' 'F']
 ['16' '17' '19' 'G']]