Difference between np.asarray() and np.array()

In this tutorial we will discuss about difference between np.asarray() and np.array(). The main difference between both are Numpy functions used to generate arrays from array like objects but they have some difference on their behaviour.

The array() method creates a copy of an existing object whereas asarray creates a new object only when needed.

Numpy:

Numpy stands for ‘Numerical python’ powerful python library that is widely used for scientific computing, data analysis and numerical computing tasks. ThisĀ is a python library used for dealing with arrays.

np.array():

The nparray() in python is used to convert a list, tuple, etc into a numpy array.

Syntax: numpy.array(object, dtype = None)

Creating Numpy array

 

import numpy as np
a = np.array([1, 2, 3], [4, 5, 6])
print(a)
Output:([1,2,3][4,5,6])
np.asarray():

The numpy.asarray() function is used when we want to convert the input to an array .

Input can be lists, list of tuples, tuples, arrays.

Syntax:

numpy.asarray(arr, dtype = None, order = None, like=None)

import numpy as np
list 1 = [1,2,3,4,5]
array 1 = np.asarray(list1)
array 2 = np.asarray(list1,dtype = str)
print(array1)
print(array2
output:[1 2 3 4 5]
        ['1' '2' '3' '4' '5']

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top