Signup/Sign In

Python NumPy Array Creation using existing data

In this tutorial, we will cover how to create an array using existing data in the Numpy Library.

The Numpy library provides various ways to create an array from the existing data and these are as given below:

  1. numpy.asarray

  2. numpy.frombuffer

  3. numpy.fromiter

Now we will cover the above mentioned one by one in our upcoming sections.

1. Python Sequence to Array - Using numpy.asarray

The routine numpy.asarray is used for converting the Python sequence into ndarray. The numpy.asarray is somehow similar to numpy.array but it has fewer parameters than numpy.array. This function is mainly used to create an array by using the existing data that is in the form of lists, or tuples.

Given below is the required syntax that is used for numpy.asarray:

numpy.asarray(sequence,  dtype, order)  

Parameters:

Let us discuss the parameters of the above constructor:

  1. sequence:
    This parameter is used to indicate the python sequence that is to be converted into the python array.

  2. dtype:
    This parameter is used to indicate the data type of each item of the array.

  3. order:
    This parameter can be either set to C or F. The default value of this parameter is C.

Let us have a few examples to see the working of this technique of array creation.

Example 1 - Array using Tuple

In the example given below we will create an array using a tuple:

import numpy as np  

# python tuple
l = (34,7,8,78)
# creating array using the tuple
a = np.asarray(l)

print(type(a))  
print(a)  


<class 'numpy.ndarray'>
[34 7 8 78]

Example 2 - Array using List

Now we will create an array using more than one list. The code for the same is given below:

import numpy as np  

# python list
l = [[1,2,3],[8,9],[5,7]] 
# creating array from list 
b = np.asarray(l)

print(type(b))  
print(b)  


<class 'numpy.ndarray'>
[list([1, 2, 3]) list([8, 9]) list([5, 7])]

2. Using numpy.frombuffer

The numpy.frombuffer routine is used to create an array by using the specified buffer.

Given below is the required syntax that is used for numpy.frombuffer:

numpy.frombuffer(buffer, dtype, count, offset) 

Parameters:

Let us discuss the parameters of the above constructor:

  • buffer:
    This parameter is used to represent an object that exposes a buffer interface.

  • dtype:
    This parameter is used to represent the data type of the returned data type array. The default value of this parameter is 0.

  • count:
    This parameter represents the length of the returned ndarray. The default value of this parameter is -1.

  • offset:
    This parameter indicates the starting position to read from. The default value of this parameter is 0.

Let us now discuss some examples using frombuffer routine.

Basic Example:

Given below is a code snippet where we will use numpy.frombuffer routine:

import numpy as np  

# intialize bytes
l = b'StudyTonight!'  
print(type(l))  

a = np.frombuffer(l, dtype = "S1")  
print(a)  
print(type(a))  


<class 'bytes'>
[b'S' b't' b'u' b'd' b'y' b'T' b'o' b'n' b'i' b'g' b'h' b't' b'!']
<class 'numpy.ndarray'>

3. Using numpy.fromiter

The routine numpy.fromiter is used to create an ndarray by using an iterable object. This routine mainly returns a one-dimensional ndarray object.

Given below is the required syntax that is used for numpy.fromiter:

numpy.fromiter(iterable, dtype, count)  

Parameters:

Let us discuss the parameters of the above constructor:

  1. iterable:
    This parameter is mainly used to represents an iterable object.

  2. dtype:
    This parameter is used to represent the data type of the resultant array items.

  3. count:
    This parameter is used to represents the number of items to read from the buffer in the array.

Let us have an example using this routine.

Basic Example:

Given below is a code snippet where we will use numpy.fromiter:

import numpy as np  

# using python tuple
tup = (2,4,6,20)  
# create an iterator
it = iter(tup)  

# create ndarray using the iterator
x = np.fromiter(it, dtype = float)  

print(x)  
print(type(x)) 


[ 2. 4. 6. 20.]
<class 'numpy.ndarray'>

Summary

This tutorial is all about how to create an array in numpy using existing data and we have covered different ways by which this can be done with their syntax and examples.



About the author:
Aspiring Software developer working as a content writer. I like computer related subjects like Computer Networks, Operating system, CAO, Database, and I am also learning Python.