Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Creation Functions for 1D Arrays | NumPy Basics
Ultimate NumPy
course content

Contenido del Curso

Ultimate NumPy

Ultimate NumPy

1. NumPy Basics
2. Indexing and Slicing
3. Commonly used NumPy Functions
4. Math with NumPy

book
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.

For example, if the specified interval is from 0 to 10 with a step size of 2, the resulting array would be: [0, 2, 4, 6, 8].

Here are its three most important parameters and their roles:

  1. start:

    • Default value: 0;
    • Represents the first element of the array.
  2. stop:

    • No default value;
    • Defines the endpoint, which is not included in the array.
  3. step:

    • Default value: 1;
    • Specifies the increment added to each subsequent element.
12345678910
import numpy as np # Creating an array of integers from 0 to 11 exclusive with step=1 array_1 = np.arange(11) print(array_1) # Creating an array of integers from 1 to 11 exclusive with step=1 array_2 = np.arange(1, 11) print(array_2) # Creating an array of integers from 0 to 11 exclusive with step=2 array_3 = np.arange(0, 11, 2) print(array_3)
copy

linspace()

While arange() can work with real numbers, numpy.linspace() is preferred over numpy.arange() for this purpose because arange() can produce unexpected results due to floating-point precision errors when calculating steps. In contrast, linspace() generates a specific number of evenly spaced points within an interval, ensuring accuracy and consistency.l.

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).

1234567
import numpy as np # Generating 5 equally spaced values between 0 and 1 (inclusive) array_1 = np.linspace(0, 1, 5) print('Example 1:', array_1) # Generating 7 equally spaced values between -1 and 1 (inclusive) array_2 = np.linspace(-1, 1, 7) print('Example 2:', array_2)
copy

Endpoint

The endpoint parameter determines whether the stop value is included. By default, it's True (inclusive). Setting it to False excludes the stop value, slightly reducing the step size.

Here's a comparison of array_inclusive and array_exclusive:

1234567
import numpy as np # Generating 5 equally spaced values between 0 and 1 (inclusive) array_inclusive = np.linspace(0, 1, 5) print('Endpoint = True:', array_inclusive) # Generating 5 equally spaced values between 0 and 1 (exclusive) array_exclusive = np.linspace(0, 1, 5, endpoint=False) print('Endpoint = False:', array_exclusive)
copy

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.

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.

Note

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

Tarea
test

Swipe to show code editor

  1. Use the arange() function to create the even_numbers array.
  2. Specify the arguments to create an array of even numbers from 2 to 21 exclusive.
  3. Use the appropriate function to create the samples array, which allows specifying the number of values within a given interval.
  4. Specify the first three arguments to create an array of 10 equally spaced numbers between 5 and 6.
  5. Ensure 6 is not included in the samples array.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 1. Capítulo 4
toggle bottom row

book
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.

For example, if the specified interval is from 0 to 10 with a step size of 2, the resulting array would be: [0, 2, 4, 6, 8].

Here are its three most important parameters and their roles:

  1. start:

    • Default value: 0;
    • Represents the first element of the array.
  2. stop:

    • No default value;
    • Defines the endpoint, which is not included in the array.
  3. step:

    • Default value: 1;
    • Specifies the increment added to each subsequent element.
12345678910
import numpy as np # Creating an array of integers from 0 to 11 exclusive with step=1 array_1 = np.arange(11) print(array_1) # Creating an array of integers from 1 to 11 exclusive with step=1 array_2 = np.arange(1, 11) print(array_2) # Creating an array of integers from 0 to 11 exclusive with step=2 array_3 = np.arange(0, 11, 2) print(array_3)
copy

linspace()

While arange() can work with real numbers, numpy.linspace() is preferred over numpy.arange() for this purpose because arange() can produce unexpected results due to floating-point precision errors when calculating steps. In contrast, linspace() generates a specific number of evenly spaced points within an interval, ensuring accuracy and consistency.l.

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).

1234567
import numpy as np # Generating 5 equally spaced values between 0 and 1 (inclusive) array_1 = np.linspace(0, 1, 5) print('Example 1:', array_1) # Generating 7 equally spaced values between -1 and 1 (inclusive) array_2 = np.linspace(-1, 1, 7) print('Example 2:', array_2)
copy

Endpoint

The endpoint parameter determines whether the stop value is included. By default, it's True (inclusive). Setting it to False excludes the stop value, slightly reducing the step size.

Here's a comparison of array_inclusive and array_exclusive:

1234567
import numpy as np # Generating 5 equally spaced values between 0 and 1 (inclusive) array_inclusive = np.linspace(0, 1, 5) print('Endpoint = True:', array_inclusive) # Generating 5 equally spaced values between 0 and 1 (exclusive) array_exclusive = np.linspace(0, 1, 5, endpoint=False) print('Endpoint = False:', array_exclusive)
copy

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.

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.

Note

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

Tarea
test

Swipe to show code editor

  1. Use the arange() function to create the even_numbers array.
  2. Specify the arguments to create an array of even numbers from 2 to 21 exclusive.
  3. Use the appropriate function to create the samples array, which allows specifying the number of values within a given interval.
  4. Specify the first three arguments to create an array of 10 equally spaced numbers between 5 and 6.
  5. Ensure 6 is not included in the samples array.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 1. Capítulo 4
Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
We're sorry to hear that something went wrong. What happened?
some-alt