Contenido del Curso
Ultimate NumPy
Ultimate NumPy
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:
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)
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:
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)
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:
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)
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
- Use the
arange()
function to create theeven_numbers
array. - Specify the arguments in the correct order to create an array of even numbers from
2
to21
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 between5
and6
. - Set the rightmost keyword argument so that
6
is not included in thesamples
array.
¡Gracias por tus comentarios!
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:
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)
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:
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)
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:
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)
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
- Use the
arange()
function to create theeven_numbers
array. - Specify the arguments in the correct order to create an array of even numbers from
2
to21
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 between5
and6
. - Set the rightmost keyword argument so that
6
is not included in thesamples
array.
¡Gracias por tus comentarios!
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:
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)
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:
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)
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:
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)
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
- Use the
arange()
function to create theeven_numbers
array. - Specify the arguments in the correct order to create an array of even numbers from
2
to21
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 between5
and6
. - Set the rightmost keyword argument so that
6
is not included in thesamples
array.
¡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:
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:
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)
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:
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)
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:
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)
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
- Use the
arange()
function to create theeven_numbers
array. - Specify the arguments in the correct order to create an array of even numbers from
2
to21
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 between5
and6
. - Set the rightmost keyword argument so that
6
is not included in thesamples
array.