ndarray- An n-dimensional array of homogeneous data types

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

FUNCTIONDESCRIPTION
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.arrangeThis function creates a sequences of evenly spaced values within a specified range.
Numpy.linspaceThis function creates arrays with a specified number of elements in the array, and spaced equally between the specified beginning and end values
Numpy.identityThe 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:

ParameterDescriptionExample
StartStart is the starting value of the array (Optional). If not specified, start will default to zero.
StopStop is the end of the array.  The array creation will stop just before the specified stop value.
StepStep (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.

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 [startstop].

Syntax of linspace():

numpy.linspace(startstopnum=50endpoint=Trueretstep=Falsedtype=Noneaxis=0*device=None)

Keyword Arguments:

ParameterDescriptionExample
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).
retsteIf True, stop is the last sample.   By default, retstep = False
endpointIf True, stop is included as the last value. If False, stop is excluded. By default, endpoint=True.
dtypeType of output array (optional)
axisIf 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.
numpy.linspace() Function

The following program shows the functionality of the linspace() function.

General Array Creation Functions

The ndarray provides a few array creation functions, e.g., numpy.onesnumpy.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(shapedtype=Noneorder=’C’*device=Nonelike=None)

Return value:

The function returns a new array of the given shape and type, filled with ones.

Parameters:

ParameterDescriptionExample
shape : int or sequence of intsShape of the new arrayTuple e.g. (2,3) or 2
dtype : data-type(Optional) The desired data-type for the arraye.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 :Optionalstr
numpy.ones() Function

The program example shows how to generate an array filled with 1’s.

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(shapedtype=Noneorder=’C’*device=Nonelike=None)

Return Value.

numpy.zeros() returns a new array of given shape and type, filled with zeros.

Parameters:

ParameterDescriptionExample
shape: int or tuple of intsShape of the new array. An integer for a 1D array or a tuple for multi-dimensional arrayse.g., (2, 3) or 2.
dtype: data-type(Optional) The desired data-type for the array elementse.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 :Optionalstr

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.

Verified by MonsterInsights