Search code examples
numpyseabornnumpy-random

Explanation required in seaborn tutorial


I am learning seaborn from http://seaborn.pydata.org/tutorial/aesthetics.html

In the import section,please explain this line

np.random.seed(sum(map(ord, "aesthetics")))

What this line does and please explain each element in this line.

In plotting offset sine wave how to define this

plt.plot(x, np.sin(x + i * .5) * (7 - i) * flip)

Solution

  • The importatnt thing first: This line np.random.seed(sum(map(ord, "aesthetics"))) is completely irrelevant for seaborn to work. So in principle you don't have to wory about it at all.

    ord gives the byte represenation of a character

    map applies a function to every item of an interable

    sum sums up the elements of an iterable.

    So map(ord, "aesthetics") will give a list of numbers, [97, 101, 115, 116, 104, 101, 116, 105, 99, 115] which when summed up, give 1069.

    This number is then fed to np.random.seed. It is a seed for numpy's random number generator. By specifying the seed, you make sure that any random numbers drawn afterwards are based on this seed.

    The point of this is to make random numbers reproducible. Having specified the seed allows me to know that when generating a random number like np.random.randint(10) the result will be 4 (for seed 1069).

    This is extremely useful to make examples reproducible, and it's the reason they use it in the seaborn tutorial to make sure that the plots generated from random numbers are actually the same everywhere.

    One could of course argue that using this command is more confusing than it would confuse people to see different plots when reproducing the tutorial, but that's a different question I guess.