# Why is Numpy asarray() Important in Python?

Contents

## What Exactly is Numpy asarray?

The numpy asarray() function is used when need to convert an input to an array. Whether the input is a list, lists of tuples, tuples, tuples of tuples, tuples of lists, and ndarrays, this is the function of the numpy module present in the standard library of python.

## Syntax

``numpy.asarray(sequence,  dtype = None, order = None, like= None)  ``

## Parameters

1. Data – This is the input data that we want to convert into an array. It can be of list, lists of tuples, tuples, tuples of tuples, tuples of lists and ndarrays.
2. dtype – It is a data type that is optional in the syntax. If we don’t define it in the syntax, it is defined by default inferred from the input data.
3. Order – This is also optional in the syntax. It just decides whether to use row-major or column-major memory representation.
4. like – This is a reference object which allows the creation of array which are not numpy arrays.

## Return

None of the copies are performed if the input is already a ndarray with matching dtype and order. If an array is a subclass of ndarray, then a base class ndarray is returned.

## Examples of Numpy asarray

### 1. Conversion of list to array

In this example, we will convert the list into an array with the numpy module’s help and use the asarray() function in numpy.

```import numpy as np

lst = [1,2,3,4,5]
print("Input List : ",lst)

converted_list = np.asarray(lst)
print("Array formed : ",converted_list)
```

Output:

``````Input List :  [1, 2, 3, 4, 5]
Array formed :  [1 2 3 4 5]``````

Explanation:

Here, Firstly we have imported numpy as np. Secondly, we have taken a simple list of some elements. Thirdly, we have converted that list into an array and printed the output.

### 2. Conversion of tuple to array

In this example, we will convert the tuple into an array with the numpy module’s help and use the asarray() function in numpy.

```import numpy as np

tuple1 = ([2, 4, 6], [8, 10, 12], [14, 16, 18])
print ("Input tuple : ",tuple1)

converted_tuple = np.asarray(tuple1)
print("Array converted : ", converted_tuple)
```

Output:

``````Input tuple :  ([2, 4, 6], [8, 10, 12], [14, 16, 18])
Array converted :  [[ 2  4  6]
[ 8 10 12]
[14 16 18]]``````

Explanation:

Here, Firstly we have imported numpy as np. Secondly, we have taken input in the form of a tuple. Thirdly, converted tuple into array by using asarray() function and printed the output.

### 3. Checking existing array

In this example, we will check that the list we have converted into an array is now an array or not.

```import numpy as np

a = np.array([1, 2, 3])
np.asarray(a) is a
```

Output:

``True``

Explanation:

Here, we have checked with the inbuilt function that it is an array or not.

### 4. Checking the type() of array

In this example, we will check the data type of an array with the help of type() function.

```import numpy as np

lst=[1,2,3,4,5,6,7,8,9,10]

a = np.asarray(lst);

print(a)
print(type(a))
```

Output:

``````[1 2 3 4 5 6 7 8 9 10]
<class numpy.ndarray>``````

Explanation:

Here, we have imported numpy as an np standard library. Then we have taken the list and converted it into an array. At last, we have checked that the list we have converted is now of which type i. e datatype.

### 5. Using multiple list in numpy.asarray()

In this example, we will be using multiple lists in the input, and then we will convert the multiple lists into an array.

```import numpy as np

lst=[[1,2,3,4,5],[6,7,8,9]]

converted_lst = np.asarray(lst);

print(type(converted_lst))
print(converted_lst)
```

Output:

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

Explanation:

Here Firstly, we have imported numpy as np standard library. Secondly, we have inputted multiple lists in them and then converted that list into an array through the asarray() function. Finally, we have printed the type() and the converted list, i.e., now an array.

### 6. using dtype in numpy.asarray()

```import numpy as np

x = np.array([3, 5], dtype=np.float32)
np.asarray(x, dtype=np.float32) is x
```

Output:

``True``

Explanation:

Here, we have used the dtype function in the program with np.float32, so the output came as True. If we use np.float64, the output will be False.

## Pros of Numpy.asarray()

• It takes less amount of memory as compared to lists, tuple, etc.
• It helps to find the data type of the input through which we can further optimize the code.
• We can easily convert a list, lists of tuples, tuples, tuples of tuples, tuples of lists, etc., into an array.
• Speed is much faster than that of lists.

## Cons of Numpy.asarray()

• It requires a contiguous memory allocation – Insertion and deletion operations become difficult as data is stored in contiguous memory allocation.

## Numpy array VS Numpy asarray

Both numpy array and numpy asarray can convert the data into ndarray. The main difference is that when you try to make a numpy array using np.array, it would create a copy of the object array and not reflect changes to the original array. On the other hand, when you use numpy asarray, it would reflect changes to the original array.

Let us understand with the help of example:

```import numpy as np

arr = np.array([5,6,7])

x = np.array(arr)
y = np.asarray(arr)
arr = 0

print("Array : ",x)
print("Asarray : ",y)
```

Output:

``````Array :  [5 6 7]
Asarray :  [5 0 7]``````

Explanation:

Here firstly, we have imported the python module numpy as np. Secondly, we have taken an input array. Thirdly, we have taken two variables, x and y, in which we have operated np.array() and np.asarray() on the input array. At last, we have printed the output of the operation performed.

## Numpy asarray Memory Error

Memory errorMemory error means that you have run out of memory in your RAM to execute your code.

When this type of error occurs, it means that you have loaded your entire data into memory. For performing large operations, you need to load your data in batch processing. As when you load your entire program, a memory error will occur, but if you load your datasets on a hard drive and access them in batches, you will not get this error in the numpy asarray function.

## Np.asarray() can not converted into Np.array()

As we know, we can convert an array into an asarray() with the help of the numpy module in python, but the interesting thing is that we cannot convert np.asarray() into np.array().

Let us look at the example:

```import numpy as np

arr = np.array([5,6,7])

x = np.array(arr)
y = np.asarray(arr)
arr = 0

z = np.array(y)

print("Array : ",x)
print("Asarray : ",y)

print("Array : ",z)
```

Output:

``````Array :  [5 6 7]
Asarray :  [5 0 7]
Array :  [5 0 7]``````

Explanation:

Here firstly, we have imported the python module numpy as np. Secondly, we have taken an input array. Thirdly, we have taken two variables, x and y, in which we have operated np.array() and np.asarray() on the input array. Fourthly, we have printed the output of the operation performed. Finally, we try to convert np.asarray() to np.array in the variable z and printed the output, but we can see that the output is not converted back into the array.

## Conclusion

NumPy is a python package in which we have function asarray()used for converting a list, tuple into an array. It also provides better runtime and space complexity. If you wish to perform general-purpose operations, use python lists. But, if you care about performance and space complexity, use Numpy functions.

However, if you have any doubts or questions, do let me know in the comment section below. I will try to help you as soon as possible.

Happy Pythoning!

Subscribe
Notify of 