Numpy Arrays within the numerical range

This section of the tutorial illustrates how the numpy arrays can be created using some given specified range.

Numpy.arrange

It creates an array by using the evenly spaced values over the given interval. The syntax to use the function is given below.

snippet
numpy.arrange(start, stop, step, dtype)

It accepts the following parameters.

  1. start: The starting of an interval. The default is 0.
  2. stop: represents the value at which the interval ends excluding this value.
  3. step: The number by which the interval values change.
  4. dtype: the data type of the numpy array items.

group

Example

snippet
import numpy as np
arr = np.arange(0,10,2,float)
print(arr)

Output:

Output
[0. 2. 4. 6. 8.]

Example

snippet
import numpy as np
arr = np.arange(10,100,5,int)
print("The array over the given range is ",arr)

Output:

Output
The array over the given range is [10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95]

NumPy.linspace

It is similar to the arrange function. However, it doesn?t allow us to specify the step size in the syntax.

Instead of that, it only returns evenly separated values over a specified period. The system implicitly calculates the step size.

The syntax is given below.

snippet
numpy.linspace(start, stop, num, endpoint, retstep, dtype)

It accepts the following parameters.

  1. start: It represents the starting value of the interval.
  2. stop: It represents the stopping value of the interval.
  3. num: The amount of evenly spaced samples over the interval to be generated. The default is 50.
  4. endpoint: Its true value indicates that the stopping value is included in the interval.
  5. rettstep: This has to be a boolean value. Represents the steps and samples between the consecutive numbers.
  6. dtype: It represents the data type of the array items.

Example

snippet
import numpy as np
arr = np.linspace(10, 20, 5)
print("The array over the given range is ",arr)

Output:

Output
The array over the given range is [10. 12.5 15. 17.5 20.]

Example

snippet
import numpy as np
arr = np.linspace(10, 20, 5, endpoint = False)
print("The array over the given range is ",arr)

Output:

Output
The array over the given range is [10. 12. 14. 16. 18.]

numpy.logspace

It creates an array by using the numbers that are evenly separated on a log scale.

The syntax is given below.

snippet
numpy.logspace(start, stop, num, endpoint, base, dtype)

It accepts the following parameters.

  1. start: It represents the starting value of the interval in the base.
  2. stop: It represents the stopping value of the interval in the base.
  3. num: The number of values between the range.
  4. endpoint: It is a boolean type value. It makes the value represented by stop as the last value of the interval.
  5. base: It represents the base of the log space.
  6. dtype: It represents the data type of the array items.

Example

snippet
import numpy as np
arr = np.logspace(10, 20, num = 5, endpoint = True)
print("The array over the given range is ",arr)

Output:

Output
The array over the given range is [1.00000000e+10 3.16227766e+12 1.00000000e+15 3.16227766e+17 1.00000000e+20]

Example

snippet
import numpy as np
arr = np.logspace(10, 20, num = 5,base = 2, endpoint = True)
print("The array over the given range is ",arr)

Output:

Output
The array over the given range is [1.02400000e+03 5.79261875e+03 3.27680000e+04 1.85363800e+05 1.04857600e+06]
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +