
- 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 array() Function
The Numpy array() function is used to create a array by converting input data. The input data can be a list, tuple, string, or other iterable. If the input is a scalar, the function returns a 0-dimensional array. For example, passing 90 as input will create a 0-dimensional array. This function returns multi-dimensional numpy array.
A 0 dimensional array in Python, used in libraries like Numpy, is an array structure that contains a single value and has no axes or dimensions, represented by a shape of ().It behaves like a scalar but retains all properties of an array.
Syntax
Following is the syntax of the Numpy array() function −
numpy.array(object, dtype=None, copy=True, order='K', subok=False, ndmin=0, like=None)
Parameters
Following are the parameters of the Numpy array() function −
- object: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: 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.
- subok (optional): If True, subclasses of ndarray are passed through. If False(default value), the output will always be a base-class ndarray. The default value is True.
- ndmin (optional): It specifies the minimum number of dimensions for the resulting array. If the input array does not have the required number of dimensions, additional dimensions are prepended to make up the difference. The default value is 0.
- 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 Type
This function return ndarray when an object such as list, tuple or set is passed as an argument.
Example
Following is a basic example to create a numpy array using Numpy array() function −
import numpy as np my_Array = np.array([13,25,53,94,85]) print("Array:",my_Array)
Output
Following is the output of the above code −
Array: [13 25 53 94 85]
Example : Creating Array from Sequences
A NumPy array can be created from sequences such as lists, tuples, or sets by passing them as arguments to the numpy.array() function.
Here, we have passed my_list as an argument to the numpy.array(), which resulted an array −
#importing numpy module import numpy as np #defined list my_list=[20,30,40,50] print(type(my_list)) #passed list an argument my_Array = np.array(my_list) #printing numpy array print("Array:",my_Array) print(type(my_Array))
Output
Following is the output of the above code −
<class 'list'> Array: [20 30 40 50] <class 'numpy.ndarray'>
Example : Creating an numpy array Using Scalar
The numpy.array() function returns 0-dimensional array when the scalar value is passed as an argument.
In the following example, we have passed 75, which is a scalar value in the numpy.array() function as an object −
#importing numpy module import numpy as np #defined scalar my_int = 75 print(type(my_int)) #passed scalr an argument my_Array = np.array(my_int) #printing numpy array print("Array:",my_Array) print(type(my_Array))
Output
Following is the output of the above code −
<class 'int'> Array: 75 <class 'numpy.ndarray'>
Example: Creating N-Dimensional NumPy Array
The ndmin argument in the numpy.array() function is used to specify the minimum number of dimensions for the resulting NumPy array. For example, to create a 3-dimensional array, set ndmin to 3 −
#importing numpy module import numpy as np #defined list my_list1=[2,8,9,6] my_list2=[12,38,79,56] my_list3=[100, 200, 300, 400] #passed list an argument my_Array = np.array([my_list1, my_list2, my_list3],ndmin=3) #printing numpy array print("3D Array -") print(my_Array)
Output
Following is the output of the above code −
3D Array - [[[ 2 8 9 6] [ 12 38 79 56] [100 200 300 400]]]