
- 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 - Ndarray Object
NumPy ndarray
The most important object defined in NumPy is an N-dimensional array type called ndarray. It describes a collection of items of the same type, which can be accessed using a zero-based index.
Each item in an ndarray takes the same size of block in the memory and is represented by a data-type object called dtype.
Any item extracted from an ndarray object (by slicing) is represented by a Python object of one of the array scalar types.
Relationship Between ndarray, dtype, and Array Scalar Type
The following diagram shows a relationship between ndarray, the data type object dtype and array scalar types −

Creating an ndarray
An instance of ndarray class can be constructed by different array creation routines. The basic ndarray is created using the array() function in NumPy.
The numpy.array() function creates an ndarray from any object exposing the array interface or from any method that returns an array. Following is the syntax −
numpy.array(object, dtype = None, copy = True, order = None, subok = False, ndmin = 0)
The above constructor takes the following parameters −
Sr.No. | Parameter & Description |
---|---|
1 |
object Any object exposing the array interface method returns an array, or any (nested) sequence. |
2 |
dtype Desired data type of array, optional |
3 |
copy Optional. By default (true), the object is copied |
4 |
order C (row major) or F (column major) or A (any) (default) |
5 |
subok By default, returned array forced to be a base class array. If true, sub-classes passed through |
6 |
ndmin Specifies minimum dimensions of resultant array |
Take a look at the following examples to understand better.
Example: Create a One-dimensional Array
In the following example, we are creating a 1D array using the array() function in NumPy −
import numpy as np a = np.array([1, 2, 3]) print(a)
The output obtained is as follows −
[1, 2, 3]
Example: Create a Multi-dimensional Array
Here, we are creating a 2D array using the array() function in NumPy −
import numpy as np a = np.array([[1, 2], [3, 4]]) print(a)
This will produce the following result −
[[1 2] [3 4]]
Example: Specify Minimum Dimensions
Now, we are specifying the minimum dimensions of the resultant array by passing "ndmin" as an argument to the array() function −
import numpy as np a = np.array([1, 2, 3, 4, 5], ndmin=2) print(a)
Following is the output of the above code −
[[1 2 3 4 5]]
Example: Specify Data Type
In the example below, we are setting the "dtype" parameter as complex in the array() function −
import numpy as np a = np.array([1, 2, 3], dtype=complex) print(a)
After executing the above code, we get the following output −
[1.+0.j 2.+0.j 3.+0.j]
Memory Layout of ndarray
An ndarray object consists of a contiguous block of memory. This means that all elements in the array are stored in a single, continuous segment of memory. This contiguous memory layout allows for easy access and manipulation of array elements.
Indexing Scheme
The indexing scheme maps each item in the ndarray to a specific location in the memory block. The ndarray uses a combination of shape and strides to determine the location of each element.
Shape − The shape of an array is a tuple of integers representing the size of the array along each dimension. For example, the shape of a 2x3 array is (2, 3).
Strides − Strides are the number of bytes to step in each dimension when traversing the array. The strides determine how to move from one element to the next in each dimension.
Row-major and Column-major Orders
The elements in an ndarray can be stored in two primary orders. They are −
-
Row-major Order (C-style) − In row-major order, the last index changes the fastest. This means that elements in the same row are stored next to each other in memory. NumPy uses row-major order by default. For example, in a 2x3 array −
[[1, 2, 3], [4, 5, 6]]
The elements are stored in memory as: 1, 2, 3, 4, 5, 6.
-
Column-major Order (FORTRAN-style) − In column-major order, the first index changes the fastest. This means that elements in the same column are stored next to each other in memory. For example, in a 2x3 array −
[[1, 2, 3], [4, 5, 6]]
The elements are stored in memory as: 1, 4, 2, 5, 3, 6.
Example
Following is a basic example to demonstrate the usage of the memory layout −
import numpy as np # Creating a 2x3 array in row-major order a = np.array([[1, 2, 3], [4, 5, 6]]) print("Array:") print(a) print("Shape:", a.shape) print("Strides:", a.strides)
The shape of the array is (2, 3), indicating it has 2 rows and 3 columns. The strides are (24, 8), meaning that to move to the next row, we need to skip 24 bytes (since each element is an 8-byte integer, and there are 3 columns), and to move to the next column, we need to skip 8 bytes (the size of one integer) as shown in the output below −
Array: [[1 2 3] [4 5 6]] Shape: (2, 3) Strides: (24, 8)