What is Ndarray Object in NumPy?
An ndarray object is the core data structure used in the NumPy library for Python. Ndarray stands for "n-dimensional array" and represents a multidimensional (1D,2D and 3D), homogeneous array of elements of the same type, such as integers, floating-point numbers, or even other objects. It provides a powerful and efficient way to store and manipulate large amounts of numerical data, particularly in scientific computing and data analysis applications.
The ndarray object is similar to Python's built-in list data type, but with many additional features and optimizations for numerical computations. It allows for fast and efficient element-wise operations, array broadcasting, and vectorized computations, which can greatly improve the performance of numerical algorithms.
In addition to its core data storage and manipulation capabilities, the ndarray object also provides many other useful functions and methods for array creation, manipulation, indexing, slicing, reshaping, sorting, searching, and more.
The numpy.array() function creates a new ndarray object from an input data sequence. Here is the syntax of the numpy.array() function with all its parameters explained:
numpy.array(object, dtype=None, copy=True, order='K', subok=False, ndmin=0)
-
object: This parameter is required and represents the input data sequence that the ndarray should be created from. It can be a list, tuple, array-like object, or any other iterable. -
dtype: This parameter is optional and specifies the data type of the elements in the ndarray. If not provided, NumPy will try to infer the data type from the input data sequence. You can specify a data type using a NumPy dtype object, such asnp.int32ornp.float64. -
copy: This parameter is optional and determines whether the ndarray should be a copy of the input data sequence or a view into the original data. If set toTrue, a copy will be made. If set toFalse, a view will be created if possible. -
order: This parameter is optional and specifies the memory layout of the ndarray. The default value is'K', which means that NumPy will choose the best layout based on the input data and the hardware platform. Other possible values include'C'(for row-major, or "C-style" order) and'F'(for column-major, or "Fortran-style" order). -
subok: This parameter is optional and determines whether the returned object should be a subclass of ndarray (if set toTrue) or a plain ndarray (if set toFalse). The default value isFalse. -
ndmin: This parameter is optional and specifies the minimum number of dimensions that the resulting ndarray should have. If not specified, the number of dimensions will be inferred from the input data sequence. If specified, the resulting ndarray will have at leastndmindimensions, with any missing dimensions being added as singleton dimensions.
How to create an array using NumPy array or ndarray?
To create an array using ndarray in NumPy, you can use the numpy.array() function, which takes an input sequence and returns a new ndarray object containing the data. Here's how you can create 1D, 2D, and 3D arrays using ndarray in NumPy:
Create One-Dimensional or 1D NumPy Array
A one dimensioal or 1D array is a simple list of elements, which can be created by passing a list, tuple, or any array-like object to the numpy.array() function. Always use [ ] to define elements of array. If you are creating one dimensional array then array will have single [ ] inside np.array( ) . For example:
Program to Create One-Dimensional or 1D array in NumPy
# One Dimensional Array in NumPy
import numpy as np
arr1d = np.array([1, 2, 3, 4, 5])
print(arr1d)
# Output: [1 2 3 4 5]
Create Two-Dimensional or 2D NumPy Array
A two dimensional or 2D array is a matrix of elements, which can be created by passing a list of lists to the numpy.array() function. Each inner list represents a row in the matrix. In this data is stored in the form of rows and coloumns. For example:
Program to Create Two-Dimensional or 2D array in NumPy
# Two Dimensional Array in NumPy 3 rows and 3 coloumns
import numpy as np
arr2d = np.array([[1, 2, 3],[4, 5, 6], [7, 8, 9]]) // 2d array having 3 lists as rows
print(arr2d)
# Output: [[1 2 3]
# [4 5 6]
# [7 8 9]]
Create Three-Dimensional or 3D NumPy Array
A three dimensional or 3D array is a cube of elements, which can be created by passing a list of lists of lists to the numpy.array() function. Each innermost list represents a "layer" in the cube. For example:

Program to Create Three Dimensional or 3D array in NumPy
# create Three Dimensional Array in NumPy
import numpy as np
arr3d = np.array([[[1,2,3],[4,5,6],[7,8,9]], [[11,12,13], [14,15,16],[17,18,19]]])
print(arr3d)
# Output: [[[1 2 3]
# [4 5 6]
# [7 8 9]]
# [[11 12 13]
# [14 15 16]
# [17 18 19]]]