Array Creation Functions
NumPy offers several functions to create arrays with initial placeholder content.
Some of these functions are listed in the table below:
Table: Array Creation Functions
| FUNCTION | DESCRIPTION |
| Function zeros | The function zeros creates an array full of zeros. |
| Function ones | The function ones creates an array full of ones. |
| Function empty | The function empty creates an array whose initial content is random and depends on the state of the memory. |
| Numpy.arrange | This function creates a sequences of evenly spaced values within a specified range. |
| Numpy.linspace | This function creates arrays with a specified number of elements in the array, and spaced equally between the specified beginning and end values |
| Numpy.identity | The NumPy identity array is a square matrix with ones on the main diagonal and zeros elsewhere. |
These functions are further described below:
Numpy.arange Function
The numpy.arange() function creates an array of evenly spaced values within a specified range.
Numpy.arange() can handle both integer and floating-point numbers. It allows negative steps to generate descending sequences.
Uses: This function is used for generating sequences for loops and mathematical calculations.
For details of the arange() function, click on the link below:
https://numpy.org/doc/stable/reference/generated/numpy.arange.html
Return value:
numpy.arange() returns a NumPy array of evenly spaced values within a specified range.
Numpy.arange() syntax:
Array1 = np.array([start], stop [, step], dtype = none)
Parameters:
| Parameter | Description | Example |
| Start | Start is the starting value of the array (Optional). If not specified, start will default to zero. | |
| Stop | Stop is the end of the array. The array creation will stop just before the specified stop value. | |
| Step | Step (Optional): Spacing between values. If not specified, Step defaults to 1. |
The program below uses the reshape() function, which is discussed in a later section.
Program Example: numpy.arange() Function
Numpy.linspace Function
Numpy.linspace() function creates arrays with a specified number of elements in the array, and spaced equally between the specified beginning and end values. NumPy will calculate the spacing between the numbers automatically.
The linspace() function provides precise control over numerical ranges.
For more details refer to the link below.
https://numpy.org/doc/stable/reference/generated/numpy.linspace.html
Return value:
linspace() function returns num evenly spaced numbers over a specified interval calculated over the interval [start, stop].
Syntax of linspace():
numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0, *, device=None)
Keyword Arguments:
| Parameter | Description | Example |
| start: | The starting value of the sequence. (optional). Default: start = 0 | |
| stop: | stop: The end value of the sequence | |
| num: | num: Number of samples to generate. Default is 50. Must be non-negative (optional). | |
| retste | If True, stop is the last sample. By default, retstep = False | |
| endpoint | If True, stop is included as the last value. If False, stop is excluded. By default, endpoint=True. | |
| dtype | Type of output array (optional) | |
| axis | If start and stop are arrays, axis specifies on what axis will the values be added. If axis = 0, value is added to front, if axis = -1 value is added at the end. |
The following program shows the functionality of the linspace() function.
Program Example: numpy.linspace() Function
General Array Creation Functions
The ndarray provides a few array creation functions, e.g., numpy.ones, numpy.zeros to define arrays based upon the desired shape. The ndarray creation functions can create arrays:
- Of any dimension by specifying the number of dimensions and
- Length along that dimension in a tuple or list.
Numpy.ones() Function
The numpy.ones() function is used to create an array filled with ones. This function is particularly useful for initializing arrays in mathematical computations or algorithms.
For details refer to the link below:
https://numpy.org/doc/stable/reference/generated/numpy.ones.html
Syntax:
numpy.ones(shape, dtype=None, order=’C’, *, device=None, like=None)
Return value:
The function returns a new array of the given shape and type, filled with ones.
Parameters:
| Parameter | Description | Example |
| shape : int or sequence of ints | Shape of the new array | Tuple e.g. (2,3) or 2 |
| dtype : data-type | (Optional) The desired data-type for the array | e.g., numpy.int8. Default is numpy.float64. |
| Order : {‘C’, ‘F’} | (Optional) Store multi-dimensional data in row-major (C-style) or column-major (F-style) order in memory. | Default: C |
| Device : | Optional | str |
The program example shows how to generate an array filled with 1’s.
Program Example: numpy.ones() Function
numpy.zeros() Function
The numpy.zeros() function is used to create an array filled with zeros. It is used for creating placeholders before filling them with actual data.
This function is particularly useful for initializing arrays in mathematical computations or algorithms, where you may not have initial values.
For more details, refer to the link below:
https://numpy.org/doc/stable/reference/generated/numpy.zeros.html#numpy.zeros
numpy.zeros Syntax:
numpy.zeros(shape, dtype=None, order=’C’, *, device=None, like=None)
Return Value.
numpy.zeros() returns a new array of given shape and type, filled with zeros.
Parameters:
| Parameter | Description | Example |
| shape: int or tuple of ints | Shape of the new array. An integer for a 1D array or a tuple for multi-dimensional arrays | e.g., (2, 3) or 2. |
| dtype: data-type | (Optional) The desired data-type for the array elements | e.g., numpy.int8. Default is numpy.float64. |
| order : {‘C’, ‘F’}, optional, | (Optional) Defines memory layout. Whether to store multi-dimensional data in row-major (C-style) or column-major (F-style) order in memory. | default: ‘C’ |
| Device : | Optional | str |
Program Example: numpy.zeros() Function
Numpy.identity() Function
The identity array can be created using numpy.identity() function, which takes the size of the matrix as an argument. The identity array is always square i.e., it has the same number of rows and columns. It has ones on the main diagonal (from the top left to the bottom right) and zeros elsewhere.
When multiplied by any matrix of compatible dimensions, it returns the original matrix.
Identity() function is used in linear algebra computations to represent a matrix that doesn’t change a vector when multiplied by it.
Syntax:
numpy.identity(n, dtype=None)
n = dimension of the square array.
Return value:
identity() [ndarray] returns an n x n array with its main diagonal set to one, and all other elements set to 0.
You can create an identity array in NumPy using the numpy.identity() function as in the program below.
Program Example: numpy.identity() Function
The following program shows identity() function usage.