
- NumPy - Home
- NumPy - Introduction
- NumPy - Environment
- NumPy Arrays
- NumPy - Ndarray Object
- NumPy - Data Types
- NumPy Creating and Manipulating Arrays
- NumPy - Array Creation Routines
- NumPy - Array Manipulation
- NumPy - Array from Existing Data
- NumPy - Array From Numerical Ranges
- NumPy - Iterating Over Array
- NumPy - Reshaping Arrays
- NumPy - Concatenating Arrays
- NumPy - Stacking Arrays
- NumPy - Splitting Arrays
- NumPy - Flattening Arrays
- NumPy - Transposing Arrays
- NumPy Indexing & Slicing
- NumPy - Indexing & Slicing
- NumPy - Indexing
- NumPy - Slicing
- NumPy - Advanced Indexing
- NumPy - Fancy Indexing
- NumPy - Field Access
- NumPy - Slicing with Boolean Arrays
- NumPy Array Attributes & Operations
- NumPy - Array Attributes
- NumPy - Array Shape
- NumPy - Array Size
- NumPy - Array Strides
- NumPy - Array Itemsize
- NumPy - Broadcasting
- NumPy - Arithmetic Operations
- NumPy - Array Addition
- NumPy - Array Subtraction
- NumPy - Array Multiplication
- NumPy - Array Division
- NumPy Advanced Array Operations
- NumPy - Swapping Axes of Arrays
- NumPy - Byte Swapping
- NumPy - Copies & Views
- NumPy - Element-wise Array Comparisons
- NumPy - Filtering Arrays
- NumPy - Joining Arrays
- NumPy - Sort, Search & Counting Functions
- NumPy - Searching Arrays
- NumPy - Union of Arrays
- NumPy - Finding Unique Rows
- NumPy - Creating Datetime Arrays
- NumPy - Binary Operators
- NumPy - String Functions
- NumPy - Matrix Library
- NumPy - Linear Algebra
- NumPy - Matplotlib
- NumPy - Histogram Using Matplotlib
- NumPy Sorting and Advanced Manipulation
- NumPy - Sorting Arrays
- NumPy - Sorting along an axis
- NumPy - Sorting with Fancy Indexing
- NumPy - Structured Arrays
- NumPy - Creating Structured Arrays
- NumPy - Manipulating Structured Arrays
- NumPy - Record Arrays
- Numpy - Loading Arrays
- Numpy - Saving Arrays
- NumPy - Append Values to an Array
- NumPy - Swap Columns of Array
- NumPy - Insert Axes to an Array
- NumPy Handling Missing Data
- NumPy - Handling Missing Data
- NumPy - Identifying Missing Values
- NumPy - Removing Missing Data
- NumPy - Imputing Missing Data
- NumPy Performance Optimization
- NumPy - Performance Optimization with Arrays
- NumPy - Vectorization with Arrays
- NumPy - Memory Layout of Arrays
- Numpy Linear Algebra
- NumPy - Linear Algebra
- NumPy - Matrix Library
- NumPy - Matrix Addition
- NumPy - Matrix Subtraction
- NumPy - Matrix Multiplication
- NumPy - Element-wise Matrix Operations
- NumPy - Dot Product
- NumPy - Matrix Inversion
- NumPy - Determinant Calculation
- NumPy - Eigenvalues
- NumPy - Eigenvectors
- NumPy - Singular Value Decomposition
- NumPy - Solving Linear Equations
- NumPy - Matrix Norms
- NumPy Element-wise Matrix Operations
- NumPy - Sum
- NumPy - Mean
- NumPy - Median
- NumPy - Min
- NumPy - Max
- NumPy Set Operations
- NumPy - Unique Elements
- NumPy - Intersection
- NumPy - Union
- NumPy - Difference
- NumPy Random Number Generation
- NumPy - Random Generator
- NumPy - Permutations & Shuffling
- NumPy - Uniform distribution
- NumPy - Normal distribution
- NumPy - Binomial distribution
- NumPy - Poisson distribution
- NumPy - Exponential distribution
- NumPy - Rayleigh Distribution
- NumPy - Logistic Distribution
- NumPy - Pareto Distribution
- NumPy - Visualize Distributions With Sea born
- NumPy - Matplotlib
- NumPy - Multinomial Distribution
- NumPy - Chi Square Distribution
- NumPy - Zipf Distribution
- NumPy File Input & Output
- NumPy - I/O with NumPy
- NumPy - Reading Data from Files
- NumPy - Writing Data to Files
- NumPy - File Formats Supported
- NumPy Mathematical Functions
- NumPy - Mathematical Functions
- NumPy - Trigonometric functions
- NumPy - Exponential Functions
- NumPy - Logarithmic Functions
- NumPy - Hyperbolic functions
- NumPy - Rounding functions
- NumPy Fourier Transforms
- NumPy - Discrete Fourier Transform (DFT)
- NumPy - Fast Fourier Transform (FFT)
- NumPy - Inverse Fourier Transform
- NumPy - Fourier Series and Transforms
- NumPy - Signal Processing Applications
- NumPy - Convolution
- NumPy Polynomials
- NumPy - Polynomial Representation
- NumPy - Polynomial Operations
- NumPy - Finding Roots of Polynomials
- NumPy - Evaluating Polynomials
- NumPy Statistics
- NumPy - Statistical Functions
- NumPy - Descriptive Statistics
- NumPy Datetime
- NumPy - Basics of Date and Time
- NumPy - Representing Date & Time
- NumPy - Date & Time Arithmetic
- NumPy - Indexing with Datetime
- NumPy - Time Zone Handling
- NumPy - Time Series Analysis
- NumPy - Working with Time Deltas
- NumPy - Handling Leap Seconds
- NumPy - Vectorized Operations with Datetimes
- NumPy ufunc
- NumPy - ufunc Introduction
- NumPy - Creating Universal Functions (ufunc)
- NumPy - Arithmetic Universal Function (ufunc)
- NumPy - Rounding Decimal ufunc
- NumPy - Logarithmic Universal Function (ufunc)
- NumPy - Summation Universal Function (ufunc)
- NumPy - Product Universal Function (ufunc)
- NumPy - Difference Universal Function (ufunc)
- NumPy - Finding LCM with ufunc
- NumPy - ufunc Finding GCD
- NumPy - ufunc Trigonometric
- NumPy - Hyperbolic ufunc
- NumPy - Set Operations ufunc
- NumPy Useful Resources
- NumPy - Quick Guide
- NumPy - Cheatsheet
- NumPy - Useful Resources
- NumPy - Discussion
- NumPy Compiler
Numpy asarray() Function
The Numpy asarray() function is used to convert the given input to an array. The input data can be in the form of list, tuple, scalar, string or set. The data-tye of the array is inferred from the input data.
In Numpy, numpy.array() and numpy.asarray() both the functions are used to create or convert the given data into numpy array. The key difference between these functions is that numpy.array(), when we made changes in the original array it will not reflect in the original array. Whereas, the numpy.asarray() it would reflect all the changes made to the original array.
Syntax
Following is the syntax of the Numpy asarray() function −
numpy.asarray(arr, dtype=None, order=None, device=None, copy=None, like=None)
Parameters
Following are the parameters of the Numpy asarray() function −
- arr:This is the required input parameter. It can be any array-like object or any nested sequences. If object is a scalar, a 0-dimensional array containing object is returned.
- dtype: It specifies the desired data type of the array elements (e.g., int, float, complex, str). If not specified, NumPy will automatically consider the data type based on the input object.
- copy (optional): If True(default value), a new copy of the object is always created, even if the object is already a NumPy array. If False, the function will try to avoid copying the data and just create a view of the input object if possible.
- device(optional): It is the device on which to place the created array. Default value is None.
- like (optional): It allows the creation of an array which is like the input object but uses an existing array-like object (like another NumPy array).
- order (optional): It specifys the memory layout of the array. If object is not an array, the newly created array will be in C order (row major) unless F is specified, in which case it will be in Fortran order (column major) −
- 'C': C-style row-major order.
- 'F': Fortran-style column-major order.
- 'A': 'F' if the input is Fortran contiguous, 'C' otherwise.
- 'K': This is the default value keep the order as close as possible to the input.
Return Value
This function returns a NumPy array with the same data type as the input data, unless explicitly specified using the dtype parameter.
Example
Following is a basic example to create a numpy array using Python numpy.asarray() function −
import numpy as np my_array = np.asarray([1,2,3,4],) print("Array :",my_array)
Output
Following is the output of the above code −
Numpy Array : [1 2 3 4]
Example : Creating an array Using Sequences
A NumPy array can be created using sequences such as lists, tuples, or sets by passing them as an arguments to the numpy.array() function.
Here, we have passed my_tuple as an argument to the numpy.asarray() function which resulted an array −
import numpy as np my_tuple = (5, 10,15, 20) print(type(my_tuple)) my_array = np.asarray(my_tuple); print("Numpy Array :",my_array) print(type(my_array))
Output
Following is the output of the above code −
<class 'tuple'> Numpy Array : [ 5 10 15 20] <class 'numpy.ndarray'>
Example : Creating n Dimension array
The numpy.asarray() function can create an N-dimensional array by passing multiple sequences, such as lists or tuples, of the same size. If the sequences are of different sizes, it will raise a ValueError −
import numpy as np my_array = np.array([[10,20,30,40],[11,22,33,44]]) print("Numpy 2D Array :") print(my_array)
Output
Numpy 2D Array : [[10 20 30 40] [11 22 33 44]]
Example : Numpy array() Vs Numpy asarray()
In numpy.asarray(), any changes made to the resulting array will be reflected in the original array if the input is already a NumPy array, as it does not create a copy. However, in numpy.array(), changes made to the new array will not affect the original array because it creates a copy of the data.
In the following example, we can find the difference between numpy.array() and numpy.asarray() −
import numpy as np # creating array Original_Array = np.array([ 2, 3, 4, 5, 6]) print("Original array : ", Original_Array) # assigning value to np.array np_array = np.array(Original_Array) Original_Array[3] = 0 print("np.array Array : ",np_array) # assigning value to np.asarray np_array = np.asarray(Original_Array) print("np.asarray Array : ",np_array)
Output
Following is the output of the above code −
Original array : [2 3 4 5 6] np.array Array : [2 3 4 5 6] np.asarray Array : [2 3 4 0 6]