Course Content

Ultimate NumPy

## Ultimate NumPy

3. Commonly used NumPy Functions

# Creation Functions for 1D Arrays

Besides basic array creation by **explicitly** specifying the elements, `numpy`

also allows **automatic** array creation using special functions. Here are two of the most common functions for creating **exclusively** 1D arrays:

`arange()`

;`linspace()`

.

## arange()

The `numpy.arange()`

function is similar to Python's built-in `range()`

function; however, it returns an `ndarray`

. Essentially, it creates an array with **evenly spaced** elements within a specified interval.

Here are its three most important parameters and their roles:

`start`

:- Default value:
`0`

; - Represents the
**first element**of the array.

- Default value:
`stop`

:- No default value;
- Defines the
**endpoint**, which is not included in the array.

`step`

:- Default value:
`1`

; - Specifies the
**increment**added to each subsequent element.

- Default value:

Explanation:

- The first element of the array is determined by
`start`

; - Each subsequent element is calculated by adding
`step`

to the previous element; - This process continues until the value reaches or exceeds
`stop`

(which is not included in the array).

Let’s see this function in action:

For `array_1`

, we only set the `stop`

parameter to `11`

. For `array_2`

, we set both `start`

to `1`

and `stop`

to `11`

. For `array_3`

, we specified all three parameters with `step=2`

.

## linspace()

While `arange()`

can work with **real numbers**, it is often better to use `numpy.linspace()`

for this purpose. With `linspace()`

, instead of the `step`

parameter, there is a `num`

parameter used to specify the **number of samples** (numbers) within a given interval (default is `50`

).

Let’s see how we can use this function:

As you can see, everything is quite simple here.

## Endpoint

Let’s focus on the `endpoint`

boolean parameter. Its default value is `True`

, meaning the `stop`

value is **inclusive**. Setting it to `False`

**excludes** the `stop`

value, thus making the step smaller and shifting the interval. Let's take a look at `array_inclusive`

and `array_exclusive`

for comparison:

When `endpoint=True`

, the interval [0, 1] is divided into **4 equal segments** and includes the **endpoint** itself (`1`

), resulting in a step size of (1 - 0) / 4 = 0.25. This generates the values: `[0, 0.25, 0.5, 0.75, 1]`

.

When `endpoint=False`

, the interval [0, 1) is divided into **5 equal segments** since the endpoint is **excluded**, resulting in a step size of (1 - 0) / 5 = 0.2. This generates the values: `[0, 0.2, 0.4, 0.6, 0.8]`

.

Note

You can always learn more about these functions in their documentation: arange, linspace.

Task

- Use the
`arange()`

function to create the`even_numbers`

array. - Specify the arguments in the correct order to create an array of
**even**numbers from`2`

to`21`

exclusive. - Use the appropriate function to create the
`samples`

array, which allows specifying the number of values within an interval. - Specify the first three arguments in the correct order to create an array of
`10`

equally spaced numbers between`5`

and`6`

. - Set the rightmost keyword argument so that
`6`

is not included in the`samples`

array.

Everything was clear?

# Creation Functions for 1D Arrays

Besides basic array creation by **explicitly** specifying the elements, `numpy`

also allows **automatic** array creation using special functions. Here are two of the most common functions for creating **exclusively** 1D arrays:

`arange()`

;`linspace()`

.

## arange()

The `numpy.arange()`

function is similar to Python's built-in `range()`

function; however, it returns an `ndarray`

. Essentially, it creates an array with **evenly spaced** elements within a specified interval.

Here are its three most important parameters and their roles:

`start`

:- Default value:
`0`

; - Represents the
**first element**of the array.

- Default value:
`stop`

:- No default value;
- Defines the
**endpoint**, which is not included in the array.

`step`

:- Default value:
`1`

; - Specifies the
**increment**added to each subsequent element.

- Default value:

Explanation:

- The first element of the array is determined by
`start`

; - Each subsequent element is calculated by adding
`step`

to the previous element; - This process continues until the value reaches or exceeds
`stop`

(which is not included in the array).

Let’s see this function in action:

For `array_1`

, we only set the `stop`

parameter to `11`

. For `array_2`

, we set both `start`

to `1`

and `stop`

to `11`

. For `array_3`

, we specified all three parameters with `step=2`

.

## linspace()

While `arange()`

can work with **real numbers**, it is often better to use `numpy.linspace()`

for this purpose. With `linspace()`

, instead of the `step`

parameter, there is a `num`

parameter used to specify the **number of samples** (numbers) within a given interval (default is `50`

).

Let’s see how we can use this function:

As you can see, everything is quite simple here.

## Endpoint

Let’s focus on the `endpoint`

boolean parameter. Its default value is `True`

, meaning the `stop`

value is **inclusive**. Setting it to `False`

**excludes** the `stop`

value, thus making the step smaller and shifting the interval. Let's take a look at `array_inclusive`

and `array_exclusive`

for comparison:

When `endpoint=True`

, the interval [0, 1] is divided into **4 equal segments** and includes the **endpoint** itself (`1`

), resulting in a step size of (1 - 0) / 4 = 0.25. This generates the values: `[0, 0.25, 0.5, 0.75, 1]`

.

When `endpoint=False`

, the interval [0, 1) is divided into **5 equal segments** since the endpoint is **excluded**, resulting in a step size of (1 - 0) / 5 = 0.2. This generates the values: `[0, 0.2, 0.4, 0.6, 0.8]`

.

Note

You can always learn more about these functions in their documentation: arange, linspace.

Task

- Use the
`arange()`

function to create the`even_numbers`

array. - Specify the arguments in the correct order to create an array of
**even**numbers from`2`

to`21`

exclusive. - Use the appropriate function to create the
`samples`

array, which allows specifying the number of values within an interval. - Specify the first three arguments in the correct order to create an array of
`10`

equally spaced numbers between`5`

and`6`

. - Set the rightmost keyword argument so that
`6`

is not included in the`samples`

array.

Everything was clear?