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
Python NumPy Numpy is a general-purpose array-processing package. It provides a high-performance multidimensional array object, and tools for working with these arrays. It is the fundamental package for scientific computing with Python.Besides its obvious scientific uses, Numpy can also be used as an efficient m
6 min read
NumPy Tutorial - Python Library NumPy (short for Numerical Python ) is one of the most fundamental libraries in Python for scientific computing. It provides support for large, multi-dimensional arrays and matrices along with a collection of mathematical functions to operate on arrays.At its core it introduces the ndarray (n-dimens
3 min read
numpy.where() in Python We will explore the basics of numpy.where(), how it works, and practical use cases to illustrate its importance in data manipulation and analysis.Syntax of numpy.where()Syntax :numpy.where(condition[, x, y]) Parameters condition: A condition that tests elements of the array.x (optional): Values from
3 min read
numpy.zeros() in Python numpy.zeros() function creates a new array of specified shapes and types, filled with zeros. It is beneficial when you need a placeholder array to initialize variables or store intermediate results. We can create 1D array using numpy.zeros().Let's understand with the help of an example:Pythonimport
2 min read
Numpy - ndarray ndarray is a short form for N-dimensional array which is a important component of NumPy. Itâs allows us to store and manipulate large amounts of data efficiently. All elements in an ndarray must be of same type making it a homogeneous array. This structure supports multiple dimensions which makes it
3 min read
Convert Numpy Array to Dataframe Converting a NumPy array into a Pandas DataFrame makes our data easier to understand and work with by adding names to rows and columns and giving us tools to clean and organize it.In this article, we will take a look at methods to convert a numpy array to a pandas dataframe. We will be discussing tw
4 min read
NumPy Array Broadcasting Broadcasting in NumPy allows us to perform arithmetic operations on arrays of different shapes without reshaping them. It automatically adjusts the smaller array to match the larger array's shape by replicating its values along the necessary dimensions. This makes element-wise operations more effici
5 min read
Types of Autoencoders Autoencoders are a type of neural network designed to learn efficient data representations. They work by compressing input data into a smaller, dense format called the latent space using an encoder and then reconstructing the original input from this compressed form using a decoder. This makes autoe
11 min read
How to inverse a matrix using NumPy In this article, we will see NumPy Inverse Matrix in Python before that we will try to understand the concept of it. The inverse of a matrix is just a reciprocal of the matrix as we do in normal arithmetic for a single number which is used to solve the equations to find the value of unknown variable
3 min read
NumPy Interview Questions with Answers If you are aware of Python, then you are also aware of NumPy because it is one of the most commonly used libraries in Python for working with arrays. Now, if you are looking for your future career as a data scientist, then you know about NumPy. In this article, we have filtered the top 70 NumPy inte
15+ min read