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.
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:
-
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:
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)
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
).
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)
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
:
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.
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.
Swipe to show code editor
- Use the
arange()
function to create theeven_numbers
array. - Specify the arguments 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 a given interval. - Specify the first three arguments to create an array of
10
equally spaced numbers between5
and6
. - Ensure
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.
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:
-
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:
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)
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
).
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)
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
:
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.
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.
Swipe to show code editor
- Use the
arange()
function to create theeven_numbers
array. - Specify the arguments 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 a given interval. - Specify the first three arguments to create an array of
10
equally spaced numbers between5
and6
. - Ensure
6
is not included in thesamples
array.
¡Gracias por tus comentarios!