Difference between np.asarray() and np.array()?
Last Updated :
08 Mar, 2024
NumPy is a Python library used for dealing with arrays. In Python, we use the list inplace of the array but it’s slow to process. NumPy array is a powerful N-dimensional array object and is used in linear algebra, Fourier transform, and random number capabilities. It provides an array object much faster than traditional Python lists.
np.array in Python
The np.array() in Python is used to convert a list, tuple, etc. into a Numpy array.
Syntax: numpy.array(object, dtype=None)
Parameters :
- object: [array_like] Input data, in any form can be converted to an array.
- dtype: [data-type, optional] By default, the datatype is inferred from the input data.
Return : [ndarray] Array interpretation of arr.
Creating a NumPy array using the .array() function
Python3
# importing numpy module
import numpy as np
# creating list
l = [5, 6, 7, 9]
# creating numpy array
res = np.array(l)
print("List in python : ", l)
print("Numpy Array in python :", res)
print(type(res))
Output:
List in python : [5, 6, 7, 9]
Numpy Array in python : [5 6 7 9]
<class 'numpy.ndarray'>
np.asarray in Python
The numpy.asarray()function is used when we want to convert the input to an array. Input can be lists, lists of tuples, tuples, tuples of tuples, tuples of lists and arrays.
Syntax: numpy.asarray(arr, dtype=None, order=None)
Parameters :
- arr: [array_like] Input data, in any form can be converted to an array. This includes lists, lists of tuples, tuples, tuples of tuples, tuples of lists and ndarrays.
- dtype: [data-type, optional] By default, the data-type is inferred from the input data.
- order : Whether to use row-major (C-style) or column-major (Fortran-style) memory representation. Defaults to ‘C’.
Return : [ndarray] Array interpretation of arr. No copy is performed if the input is already ndarray with matching dtype and order. If arr is a subclass of ndarray, a base class array is returned.
Convert a list into an array using asarray()
Python3
import numpy as np
l = [1, 2, 5, 4, 6]
print ("Input list : ", l)
res = np.asarray(l)
print ("output array from input list : ", res)
print(type(res))
Output:
Input list : [1, 2, 5, 4, 6]
output array from input list : [1 2 5 4 6]
<class 'numpy.ndarray'>
Numpy array VS Numpy asarray
In Python, NumPy array and NumPy asarray are used to convert the data into ndarray. If we talk about the major difference that is when we make a NumPy array using np.array, it creates a copy of the object array or the original array and does not reflect any changes made to the original array. Whereas on the other hand, when we try to use NumPy asarray, it would reflect all the changes made to the original array.
Example
Here we can see that copy of the original object is created which is actually changed and hence it's not reflected outside. Whereas, in the next part, we can see that no copy of the original object is created and hence it's reflected outside.
Python3
import numpy as np
# creating array
a = np.array([ 2, 3, 4, 5, 6])
print("Original array : ",a)
# assigning value to np.array
np_array = np.array(a)
a[3] = 0
print("np.array Array : ",np_array)
# assigning value to np.asarray
np_array = np.asarray(a)
print("np.asarray Array : ",np_array)
Output:
Original array : [2 3 4 5 6]np.array Array : [2 3 4 5 6]
np.asarray Array : [2 3 4 0 6]
Similar Reads
Difference between Numpy array and Numpy matrix While working with Python many times we come across the question that what exactly is the difference between a numpy array and numpy matrix, in this article we are going to read about the same. What is np.array() in PythonThe Numpy array object in Numpy is called ndarray. We can create ndarray using
3 min read
Difference between NumPy.dot() and '*' operation in Python In Python if we have two numpy arrays which are often referred as a vector. The '*' operator and numpy.dot() work differently on them. It's important to know especially when you are dealing with data science or competitive programming problem. Working of '*' operator '*' operation caries out element
2 min read
How to append a NumPy array to an empty array in Python In this article, we will cover how to append a NumPy array to an empty array in Python. Â Here, we will discuss 2 different methods to append into an empty NumPy array. Both of these methods differ slightly, as shown below: Append a NumPy array to an empty array using the appendExample 1 Here we are
2 min read
Different Ways to Create Numpy Arrays in Python Creating NumPy arrays is a fundamental aspect of working with numerical data in Python. NumPy provides various methods to create arrays efficiently, catering to different needs and scenarios. In this article, we will see how we can create NumPy arrays using different ways and methods. Ways to Create
3 min read
How Do I Build A Numpy Array From A Generator? NumPy is a powerful library in Python for numerical operations, and it provides an efficient array object, numpy.ndarray, for working with large datasets. Often, data is generated using generators, and it becomes necessary to convert this data into NumPy arrays for further analysis. In this article,
3 min read
How to create an empty and a full NumPy array? Creating arrays is a basic operation in NumPy. Empty array: This array isnât initialized with any specific values. Itâs like a blank page, ready to be filled with data later. However, it will contain random leftover values in memory until you update it.Full array: This is an array where all the elem
2 min read
Python - Numpy Array Column Deletion Given a numpy array, write a programme to delete columns from numpy array. Examples - Input: [['akshat', 'nikhil'], ['manjeeet', 'akash']] Output: [['akshat']['manjeeet']] Input: [[1, 0, 0, 1, 0], [0, 1, 2, 1, 1]] Output: [[1 0 1 0][0 2 1 1]] Given below are various methods to delete columns from nu
2 min read
Count the occurrence of a certain item in an ndarray - Numpy In this article, the task is to find out how to count the occurrence of a certain item in an nd-Array in Python. Example Array = [[ 0 1 2 3] [ 4 5 2 7] [ 8 2 10 11]] Input: element = 2 Output: 3 timesCount the occurrence of a certain item in an array using a loop Here we are using a loop to count th
3 min read
NumPy Array Functions NumPy array functions are a set of built-in operations provided by the NumPy library that allow users to perform various tasks on arrays. With NumPy array functions, you can create, reshape, slice, sort, perform mathematical operations, and much moreâall while taking advantage of the library's speed
3 min read
Change the Data Type of the Given NumPy Array NumPy arrays are homogenous, meaning all elements in a NumPy array are of the same data type and referred to as array type. You might want to change the data type of the NumPy array to perform some specific operations on the entire data set. In this tutorial, we are going to see how to change the d
4 min read