Various types of arrays can be created in Python using the NumPy library. You have to know the ways of creating a NumPy array before using the linspace() function in Python. Sometimes we need to create the array with evenly spaced or non-evenly spaced numbers. Both evenly spaced and non-evenly spaced arrays with a range of numbers can be created using the linspace() function. It is a useful function for numerical calculation. How the linspace() function can be used in the python script has been shown in this tutorial.
Syntax
The syntax of linspace() function is shown below:
The function can take seven arguments. The purposes of all arguments are described below:
- start: It is the mandatory argument that sets the starting value of the sequence.
- stop: It is a mandatory argument that sets the end value of the sequence.
- num: It is an optional argument that sets the number of samples to generate. Its default value is 50.
- endpoint: It is an optional argument, and if it is set to True, then the last value of the array will be set based on the stop value. Its default value is True.
- retstep: It is an optional argument, and if it is set to True, then the step and the samples will be returned. Its default value is False.
- dtype: It is an optional argument, and it is used to set the data type of the array values. Its default value is None.
- axis: It is an optional argument, and it defines the axis in the array to store the samples. Its default value is 0.
Use of linspace() function
Different uses of the linspace() function are shown in this part of the tutorial using multiple examples.
Example-1: Using mandatory arguments of linspace() function
The following example shows the way to create a one-dimensional array with evenly spaced numbers using the linspace() function. Two mandatory arguments of this function are in this example. An array with a range of evenly spaced fractional numbers will be generated by the linspace() function where the first number will be 10, and the last number will be 20.
import numpy as np
# Create NumPy array with evenly spaced values
np_array = np.linspace(10, 20)
# Print the output
print("The output of the linspace() function is:\n", np_array)
Output:
The following output will appear after executing the above script.
Example-2: Using num argument of linspace() function
The following example shows the use of the num argument of the linspace() function. The positive numbers are used for start and stop values in the first linspace() function. This function will generate an array of 10 evenly spaced numbers for assigning 10 to num argument. The negative numbers are used for start and stop values in the second linspace() function. This function will generate an array of 15 evenly spaced numbers for assigning 15 to num argument.
import numpy as np
# Create NumPy array with 10 evenly spaced values
np_array = np.linspace(10, 20, num=10)
# Print the output of the array
print("The output of linspace with 10 numbers:\n", np_array)
# Create NumPy array with 15 evenly spaced values
np_array = np.linspace(-15, -5, num=15)
# Print the output of the array
print("The output of linspace with 15 numbers:\n", np_array)
Output:
The following output will appear after executing the above script.
Example-3: Using dtype argument of linspace() function
The following example shows the use of the dtype argument of the linspace() function. int64 is set to dtype argument of linspace() function to create an array with the set of 15 evenly spaced large integer values. The starting value of the array will be 15, and the end value will be 35.
import numpy as np
# Create evenly spaced NumPy array with step
np_array = np.linspace(15, 35, 15, dtype=np.int64)
# Print the array
print("The output of the linspace:\n", np_array)
Output:
The following output will appear after executing the above script.
Example-4: Using endpoint argument of linspace() function
The following example shows the use of the endpoint argument of the linspace() function to set the last value of the array that will be returned by this function. The default value of the endpoint function is True, and it sets the stop value as the last value of the returned array. If the value of the endpoint is False, then the last value of the array will be calculated in different ways, and the last value will be less than the stop value.
import numpy as np
# Create evenly spaced NumPy array with stop value
np_array = np.linspace(15, 35, 15)
print("The output of linspace without endpoint:\n", np_array)
# Create evenly spaced NumPy array with stop value and endpoint
np_array = np.linspace(15, 35, 15, endpoint=False)
print("\nThe output of linspace with endpoint:\n", np_array)
Output:
The following output will appear after executing the above script.
Example-5: Using retstep argument of linspace() function
The following example shows the use of the retstep argument of the linspace() function. The default value of this function is False. If the value of this argument is set to True, then the linspace() function returns the step value with the array.
import numpy as np
# Call linspace with retstep
np_array, step = np.linspace(-5, 5, 20, retstep=True)
# Print the array
print("The output of the linspace() function is:\n", np_array)
# Print the step value
print("\nThe step value is:\n", step)
Output:
The following output will appear after executing the above script.
Example-6: Using non-scalar values for the start and stop arguments
The following example shows how the non-scalar values, such as arrays, can be used as the start and stop argument values of the linspace() function to generate the array. This script will create a two-dimensional array of 5 rows and 4 columns.
import numpy as np
# Call linspace() function with start and stop arrays
np_array = np.linspace(start=[10, 30, 50, 70], stop=[100, 200, 300,400], num=5)
# Print the array
print("The output of the linspace() function is:\n", np_array)
Output:
The following output will appear after executing the above script.
Conclusion
The uses of different arguments of the linspace() function have been explained in this tutorial using simple examples to help the readers know the purpose of this function and apply it in their script properly.