{"id":8703,"date":"2021-06-12T10:56:47","date_gmt":"2021-06-12T05:26:47","guid":{"rendered":"https:\/\/python-programs.com\/?p=8703"},"modified":"2021-11-22T18:53:28","modified_gmt":"2021-11-22T13:23:28","slug":"numpy-linspace-create-same-sized-samples-over-an-interval-in-python","status":"publish","type":"post","link":"https:\/\/python-programs.com\/numpy-linspace-create-same-sized-samples-over-an-interval-in-python\/","title":{"rendered":"numpy.linspace() | Create same sized samples over an interval in Python"},"content":{"rendered":"
In this article we will discus about how to create a Numpy array of evenly spaced samples over a range using Numpy module provides a function To use Numpy we have to include the below module i.e.<\/p>\n Let’s see below two examples to understand the concept.<\/p>\n When we will pass Create a Numpy array of evenly spaced samples over a range using numpy.linspace() In this article we will discus about how to create a Numpy array of evenly spaced samples over a range using numpy.linspace(). Numpy.linspace() Numpy module provides a function numpy.linspace() to create a evenly spaced samples over a specified interval i.e. Syntax: numpy.linspace(start, …<\/p>\nnumpy.linspace()<\/code>.<\/p>\n
Numpy.linspace()<\/h2>\n
numpy.linspace()<\/code> to create a evenly spaced samples over a specified interval i.e.<\/p>\n
Syntax: numpy.<\/span>linspace<\/span>(<\/span>start, stop, num=<\/span>50<\/span>, endpoint=<\/span>True<\/span>, retstep=<\/span>False<\/span>, dtype=<\/span>None<\/span>)<\/span><\/pre>\n
\n
import<\/span> numpy <\/span>as<\/span> np<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n
\n
<\/a>Example-1 : Create 5 evenly spaced samples in interval [10, 50]<\/h3>\n
import numpy as np\r\n# Create 5 evenly spaced samples in interval [10, 50]\r\ntemp_arr = np.linspace(10,50,5)\r\nprint(temp_arr)<\/pre>\n
Output :\r\n[10. 20. 30. 40. 50.]<\/pre>\n<\/div>\n
import numpy as np\r\n# Create 5 evenly spaced samples in interval [10, 50]\r\ntemp_arr = np.linspace(10,50,5,dtype=np.int32)\r\nprint(temp_arr)<\/pre>\n
Output :\r\n[10 20 30 40 50]<\/pre>\n<\/div>\n
<\/a>Example-2 : Get the Step size from numpy.linspace()<\/h3>\n
retstep=True<\/code>\u00a0in
numpy.linspace().<\/code><\/p>\n
import numpy as np\r\n# Create 5 evenly spaced samples in interval [10, 50]\r\ntemp_arr, step = np.linspace(10,50,5,dtype=np.int32,retstep=True)\r\nprint('Printing the Numpy Array contents: ')\r\nprint(temp_arr)\r\nprint('The step size between two samples : ', step)<\/pre>\n
Output :\r\nContents of the Numpy Array : \r\n[10 20 30 40 50]\r\nStep size between two elements : 10.0<\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"