Linspace

Linspace gives evenly spaced samples.

Syntax:

numpy.linspace(start, stop, num, endpoint)

Here,

  • Start: Starting value of the sequence
  • Stop: End value of the sequence
  • Num: Number of samples to generate. Default is 50
  • Endpoint: If True (default), stop is the last value. If False, stop value is not included.

Example:

For instance, it can be used to create 10 values from 1 to 5 evenly spaced.

import numpy as np
np.linspace(1.0, 5.0, num=10)

Output:

array([1.        , 1.44444444, 1.88888889, 2.33333333, 2.77777778,       3.22222222, 3.66666667, 4.11111111, 4.55555556, 5.        ])			

If you do not want to include the last digit in the interval, you can set endpoint to false

np.linspace(1.0, 5.0, num=5, endpoint=False)			

Output:

array([1. , 1.8, 2.6, 3.4, 4.2])

LogSpace

LogSpace returns even spaced numbers on a log scale. Logspace has the same parameters as np.linspace.

Syntax:

numpy.logspace(start, stop, num, endpoint)

Example:

np.logspace(3.0, 4.0, num=4)	

Output:

array([ 1000. ,  2154.43469003,  4641.58883361, 10000.        ])			

Finaly, if you want to check the size of an array, you can use itemsize

x = np.array([1,2,3], dtype=np.complex128)
x.itemsize			

Output:

16

The x element has 16 bytes.

Summary

Below, a summary of the essential functions used with NumPy

Objective Code
Create a linear space linspace
Create a log space logspace