'How do I generate sequence of numbers in logarithmic scale?

How do I generate sequence of numbers in logarithmic scale [0.0001,1]. Is there an inbuilt function in python that takes in range, number of values to produce.



Solution 1:[1]

I came here as I had the same question. @MarkDickinson provided guidance as a comment rather than an answer, so I thought I'd post as an answer what I tried. I took a look at the numpy function mentioned and found an alternative I wanted to share. I'm using numpy version 1.17.4 with Python 3.7.5.

First import numpy

import numpy as np

Then use the numpy.logspace() function. It will generate logarithmically spaced numbers with endpoints included by default. You will need to decide how many steps you want. In this example, I selected 10 to generate 10 total steps from 0.0001 to 1 inclusive.

np.logspace(start=4, stop=0, base=0.1, num=10)
array([1.00000000e-04, 2.78255940e-04, 7.74263683e-04, 2.15443469e-03,
       5.99484250e-03, 1.66810054e-02, 4.64158883e-02, 1.29154967e-01,
       3.59381366e-01, 1.00000000e+00])

I find the input parameters a bit counter-intuitive as I have to first convert to exponential notation when using the logspace() function.

An alternative that makes it easier to just put in the specific range of numbers from the OP is numpy.geomspace()

np.geomspace(start=0.0001, stop=1, num=10)
array([1.00000000e-04, 2.78255940e-04, 7.74263683e-04, 2.15443469e-03,
       5.99484250e-03, 1.66810054e-02, 4.64158883e-02, 1.29154967e-01,
       3.59381366e-01, 1.00000000e+00])

End result is identical, however the geomspace option allows for the numbers exactly as specified in the OP.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 magiclantern