NumPy Tutorial NumPy Statistics NumPy References

NumPy - Array from Existing Data



The NumPy package contains a number of functions which can be used to create an array from an existing data. Below mentioned are most commonly used functions for this purpose:

FunctionDescription
asarray() Converts the input to an array.
frombuffer() Interpret a buffer as a 1-dimensional array.
fromiter() Create a new 1-dimensional array from an iterable object.

Lets discuss these functions in detail:

numpy.asarray() function

The numpy.asarray() function is used to convert the input to an array. The syntax for using this function is given below:

Syntax

numpy.asarray(a, dtype=None, order=None)

Parameters

a Required. Specify the input data, in any form that can be converted to an array. This includes lists, lists of tuples, tuples, tuples of tuples, tuples of lists and ndarrays.
dtype Optional. Specify the desired data type. By default, the data-type is inferred from the input data.
order Optional. Specify whether to store the result. Two possible values are: C (C-style) and F (Fortran-style). Default: 'C'

Example:

In the example below, the function is used to create a numpy array from an existing data.

import numpy as np

x1 = [10, 20, 30, 40, 50, 60]
x2 = (100, 200, 300)
x3 = [[10, 20, 30], [40, 50, 60]]

#creating numpy array from a list
Arr1 = np.asarray(x1)
print("Arr1 is:", Arr1)

#creating numpy array from a tuple
Arr2 = np.asarray(x2, dtype=float)
print("\nArr2 is:", Arr2)

#creating numpy array from a list of list
Arr3 = np.asarray(x3)
print("\nArr3 is:\n", Arr3)

The output of the above code will be:

Arr1 is: [10 20 30 40 50 60]

Arr2 is: [100. 200. 300.]

Arr3 is:
[[10 20 30]
 [40 50 60]]

numpy.frombuffer() function

The numpy.frombuffer() function is used to interpret a buffer as a 1-dimensional array. The syntax for using this function is given below:

Syntax

numpy.frombuffer(buffer, dtype=float, count=-1, offset=0)

Parameters

buffer Required. Specify an object that exposes the buffer interface.
dtype Optional. Specify the desired data type. Default is float.
count Optional. Specify the number of items to read. Default is -1 which means all data in the buffer.
offset Optional. Start reading the buffer from this offset (in bytes). Default is 0.

Example:

In the example below, the frombuffer() function is used to create a numpy array from a buffer.

import numpy as np

x = b"Hello World"

#creating 1-D numpy array from buffer
Arr1 = np.frombuffer(x, dtype='S1')
print("Arr1 is:", Arr1)

#using count parameter
Arr2 = np.frombuffer(x, dtype='S1', count=5)
print("\nArr2 is:", Arr2)

#using count and offset parameter
Arr3 = np.frombuffer(x, dtype='S1', count=5, offset=6)
print("\nArr3 is:", Arr3)

The output of the above code will be:

Arr1 is: [b'H' b'e' b'l' b'l' b'o' b' ' b'W' b'o' b'r' b'l' b'd']

Arr2 is: [b'H' b'e' b'l' b'l' b'o']

Arr3 is: [b'W' b'o' b'r' b'l' b'd']

numpy.fromiter() function

The numpy.fromiter() function is used to create a new 1-dimensional array from an iterable object.

Syntax

numpy.fromiter(iterable, dtype, count=-1)

Parameters

iterable Required. Specify an iterable object providing data for the array.
dtype Required. Specify the desired data type of returned array.
count Optional. Specify the number of items to read from iterable. The default is -1, which means all data is read.

Example:

In the example below, the fromiter() function is used to create a numpy array from an iterable object.

import numpy as np

it = (x*x for x in range(5))

#creating numpy array from an iterable
Arr = np.fromiter(it, dtype=float)
print(Arr)

The output of the above code will be:

[ 0.  1.  4.  9. 16.]

5