random.sample in Python 3 (jupyter notebook)
Question: When using Canopy I can do

    
    
    from scipy import *
    import pylab as py
    import random
    
    aa = random.sample(arange(1,4,0.5),1)
    

whereas in the Jupyter notebook it complaints with the following:

    
    
      ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    <ipython-input-59-e33979a68ee1> in <module>()
    ----> 1 cc = random.sample(arange(1,4,0.5), 1)
    
    /u/rscratch/bm485/anaconda3/lib/python3.5/random.py in sample(self, population, k)
    309             population = tuple(population)
    310         if not isinstance(population, _Sequence):
    --> 311             raise TypeError("Population must be a sequence or set.  For dicts, use list(d).")
    312         randbelow = self._randbelow
    313         n = len(population)
    
    TypeError: Population must be a sequence or set.  For dicts, use list(d).
    

How can I fix this?


Answer: Please avoid star imports, wherever possible. It is often not clear what you
actually imported and shadowed in the end.

    
    
    import random
    from scipy import *
    print(random)
    

Gives:

    
    
    <module 'numpy.random' from '/Users/ch/miniconda/envs/sci34/lib/python3.4/site-packages/numpy/random/__init__.py'>
    

Whereas

    
    
    from scipy import *
    import random
    print(random)
    

prints:

    
    
    <module 'random' from '/Users/ch/miniconda/envs/sci34/lib/python3.4/random.py'>
    

Takehome message:

Prefer `import scipy as sp` or better `from scipy import <subpackage>` to
avoid shadowing your random function. Thanks @ev-br for the remark.

* * *

If you are dealing with numpy's ndarrays, prefer the methods in `numpy.random`
over the counter parts in python's random module. The issue you see here is
that python and numpy have a different definition what a sequence is. For more
details see the discussion here: <https://github.com/numpy/numpy/issues/2776>

What you want is probably
[`np.random.choice`](http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.choice.html#numpy-
random-choice):

    
    
    numpy.random.choice(a, size=None, replace=True, p=None)
    

