numpy.asarray()

This function is used to create an array by using the existing data in the form of lists, or tuples. This function is useful in the scenario where we need to convert a python sequence into the numpy array object.

Syntax

snippet
numpy.asarray(sequence,  dtype = None, order = None)

Parameters

It accepts the following parameters.

  1. shape: It is the Tuple defining the shape of the matrix.
  2. dtype: It is the data type each item of the array
  3. order: It is the insertion order of the array. The default is C.

Return

An array with the equivalent values to the sequence is returned.

Example

snippet
import numpy as np

l=[1,2,3,4,5,6,7]

a = np.asarray(l);

print(type(a))

print(a)

Output:

Output
[1 2 3 4 5 6 7]

Example: Creating a numpy array from the Tuple

snippet
import numpy as np
l=(1,2,3,4,5,6,7)   
a = np.asarray(l);
print(type(a))
print(a)

Output:

Output
[1 2 3 4 5 6 7]

Example: creating a numpy array using more than one list

snippet
import numpy as np
l=[[1,2,3,4,5,6,7],[8,9]]
a = np.asarray(l);
print(type(a))
print(a)

Output:

Output
[list([1, 2, 3, 4, 5, 6, 7]) list([8, 9])]
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +