Python: Get random value from a array dimension
Question: I am trying to get a random element from an array's single dimension in
Python. So in the case below, I would like to retrieve any one of the 5
floats.

    
    
    ar = rand(1, 5)
    
    ar = array([[ 0.29889882,  0.84955019,  0.52989055,  0.57220576,  0.16841406]])
    

I have been able to retrieve a float if there are 5 elements and only one
dimension `(ar = rand(5, 1)),`

using:

    
    
    ar[randrange(0, p.size)]
    

but how do I get a value from an array from a single dimension?


Answer: Assuming you are referring to `numpy.array`, you can use the following:

    
    
    >>> import numpy as np
    >>> np.array([[ 0.29889882, 0.84955019, 0.52989055, 0.57220576, 0.16841406]])
    array([[ 0.29889882,  0.84955019,  0.52989055,  0.57220576,  0.16841406]])
    >>> 
    >>> 
    >>> ar = np.array([[ 0.29889882, 0.84955019, 0.52989055, 0.57220576, 0.16841406]])
    >>> ar[:, random.randint(0,4)]
    array([ 0.29889882])
    >>> ar[:, random.randint(0,4)]
    array([ 0.52989055])
    

