Creating Array using linspace()

Vandana Srivastava
2 min readJul 22, 2024

If you want linearly and evenly spaced values in the specified interval between two values then we use linspace.

Syntax:

numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0).

Return 1-D array evenly having spaced numbers over a specified interval. Values are equally spaced.

By default 50 is the maximum value we are going to get.

Here’s a breakdown of its parameters:

  • start: The starting value of the sequence.
  • stop: The end value of the sequence.
  • num: (Optional) The number of evenly spaced samples to generate. Default is 50.
  • endpoint: (Optional) If True (default), stop is the last sample. If False, it is not included.
  • retstep: (Optional) If True, return (samples, step), where step is the spacing between samples.
  • dtype: (Optional) The type of the output array.
  • axis: (Optional) The axis in the result along which the samples are stored. Default is 0.

Example-

In the above example, 0 and 1 get divided into 2 parts and give the output. It always includes start and end values.

In this example, np.linspace(0,1) generates an array of by default 50 numbers starting at 0 and ending at including 1

Note- np.linspace() produces a one-dimensional array.

However, you can reshape the resulting array to create higher-dimensional arrays if needed. As given below-

Uses:

The function is often used in scientific computing, data analysis, and engineering to generate a sequence of numbers for plotting, simulations, and other numerical tasks.

That’s it for now, let’s deep dive into NumPy in my upcoming blogs.

And can learn more from my Github profile:

Also Matplotlib learnings from github:

Pandas Learnings from github:

Check some of my other blogs in this series:

--

--

No responses yet