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

bookCreation 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:

  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.

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:

123456789101112
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) print('-' * 35) # Creating an array of integers from 1 to 11 exclusive with step=1 array_2 = np.arange(1, 11) print(array_2) print('-' * 35) # Creating an array of integers from 0 to 11 exclusive with step=2 array_3 = np.arange(0, 11, 2) print(array_3)
copy

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:

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

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:

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

Tarea

  1. Use the arange() function to create the even_numbers array.
  2. Specify the arguments in the correct order 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 an interval.
  4. Specify the first three arguments in the correct order to create an array of 10 equally spaced numbers between 5 and 6.
  5. Set the rightmost keyword argument so that 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

bookCreation 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:

  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.

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:

123456789101112
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) print('-' * 35) # Creating an array of integers from 1 to 11 exclusive with step=1 array_2 = np.arange(1, 11) print(array_2) print('-' * 35) # Creating an array of integers from 0 to 11 exclusive with step=2 array_3 = np.arange(0, 11, 2) print(array_3)
copy

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:

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

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:

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

Tarea

  1. Use the arange() function to create the even_numbers array.
  2. Specify the arguments in the correct order 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 an interval.
  4. Specify the first three arguments in the correct order to create an array of 10 equally spaced numbers between 5 and 6.
  5. Set the rightmost keyword argument so that 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

bookCreation 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:

  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.

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:

123456789101112
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) print('-' * 35) # Creating an array of integers from 1 to 11 exclusive with step=1 array_2 = np.arange(1, 11) print(array_2) print('-' * 35) # Creating an array of integers from 0 to 11 exclusive with step=2 array_3 = np.arange(0, 11, 2) print(array_3)
copy

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:

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

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:

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

Tarea

  1. Use the arange() function to create the even_numbers array.
  2. Specify the arguments in the correct order 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 an interval.
  4. Specify the first three arguments in the correct order to create an array of 10 equally spaced numbers between 5 and 6.
  5. Set the rightmost keyword argument so that 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!

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:

  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.

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:

123456789101112
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) print('-' * 35) # Creating an array of integers from 1 to 11 exclusive with step=1 array_2 = np.arange(1, 11) print(array_2) print('-' * 35) # Creating an array of integers from 0 to 11 exclusive with step=2 array_3 = np.arange(0, 11, 2) print(array_3)
copy

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:

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

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:

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

Tarea

  1. Use the arange() function to create the even_numbers array.
  2. Specify the arguments in the correct order 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 an interval.
  4. Specify the first three arguments in the correct order to create an array of 10 equally spaced numbers between 5 and 6.
  5. Set the rightmost keyword argument so that 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
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
some-alt