
- 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 asanyarray() Function
The Numpy asanyarray() function is used to convert the input to an array, but keeps ndarray subclasses unchanged, which means if the input data is not array it will convert into a standard numpy array.
If the input is already a NumPy ndarray subclass (e.g., numpy.matrix or other custom subclasses), it is returned as is without converting it to a plain ndarray.
Apart from array, Following are other input values to the numpy.asanyarray() function −
- scalars- It can be any numeric value. For an example, 3.4,5,112...
- Sequences- It can be list, tuple datatypes. For an example, [1,2,3,4], ('a', 'b', 'c', 'd')
- Nested Sequences- It can be nested tuples or lists. For an example, [[1,2,3], [4,5,6],[7,8,9]], ((12,10,45),(90,23,56))
Syntax
Following is the syntax of the Numpy asanyarray() function −
numpy.asanyarray(arr, dtype=None, order=None, like=None)
Parameters
Following are the parameters of the Numpy asanyarray() function −
- arr:This is the required input parameter. It can be any array-like object (like a list, tuple, or another NumPy array) that we want to convert into a NumPy array.
- dtype(optional): 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.
- 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 ndarray if the input data is an ndarray or a subclass of ndarray, it is returned as-is and no copy is performed.
Example
Following is a basic example to create a numpy array using Numpy asanyarray() function −
import numpy as np my_Array=np.asanyarray([1, 2, 3, 4, 5]) print("Numpy Array:",my_Array) print(type(my_Array))
Output
Following is the output of the above code −
Numpy Array: [1 2 3 4 5] <class 'numpy.ndarray'>
Example : Creating a Array Using Sequences
A NumPy array can be created using sequences such as lists, tuples, or sets by passing them as arguments to the numpy.asanyarray() function.
Here, we have passed my_tuple as an argument to the numpy.asanyarray() function converted it into numpy array −
#importing numpy module import numpy as np #defined tuple my_tuple=(15,25,35,45) my_Array=np.asanyarray(my_tuple) print("Numpy Array:",my_Array) print(type(my_Array))
Output
Following is the output of the above code −
Numpy Array: [15 25 35 45] <class 'numpy.ndarray'>
Example : Converting Scalar to Array
Using numpy.asanyarray() function is used to convert scalar value to a numpy array.
In the following example, we have converted my_scalar i.e 100 is passed as an argument to the numpy asanyarray() , which result a numpy array −
import numpy as np #initalizing a scalar value my_scalar = 100 print ("Input scalar : ", my_scalar) out_arr = np.asanyarray(my_scalar) print ("output array from input scalar : ", out_arr) print(type(out_arr))
Output
Following is the output of the above code −
Input scalar : 100 output array from input scalar : 100 <class 'numpy.ndarray'>
Example : Creating n Dimensional Array
We can create an n-dimensional NumPy array by passing a nested sequences such as nested list(list of lists), nested tuple(tuple of tuples) as an argument to the numpy.asanyarray() function. However, if the nested sequences have unequal sizes, a ValueError will be raised.
Here, we have created 2D dimensional numpy array using numpy.asanyarray() −
import numpy as np #initalizing a scalar value my_list = [[1,2,3],[10,20,30],[100,200,300]] out_arr = np.asanyarray(my_list) print ("2D Numpy Array - ") print(out_arr)
Output
Following is the output of the above code −
2D Numpy Array - [[ 1 2 3] [ 10 20 30] [100 200 300]]
Example: Passing an Array as an Argument
When a standard NumPy array is passed as an argument to the numpy.asanyarray() function, it remains unchanged. However, if a subclass of numpy.ndarray (numpy.matrix) is passed, it is returned as it is, preserving the subclass type −
import numpy as np # Input: NumPy matrix (a subclass of ndarray) mat = np.matrix([[1, 2], [3, 4]]) arr1 = np.asanyarray(mat) print(type(arr1)) # Input: Standard ndarray arr = np.array([5, 6, 7]) arr2 = np.asanyarray(arr) print(type(arr2))
Output
Following is the output of the above code −
<class 'numpy.matrix'> <class 'numpy.ndarray'>