[SciPy-User] numpy.random.choice throws AttributeError when called with a list of objects.

Garvin Haslett g_haslett at yahoo.co.uk
Sat Feb 16 17:15:21 EST 2013


Sebastian Berg <sebastian <at> sipsolutions.net> writes:

> 
> On Fri, 2013-02-15 at 19:13 +0000, Garvin Haslett wrote:
> > The code below results in:
> > AttributeError: SgCoord instance has no attribute 'ndim'
> > 
> > Given that an example in the documentation operates on an array of strings,
> > I'd expect numpy.random.choice to work with an array of objects. Is this 
> > not the case?
> > 
> 
> Hey,
> 
> Yes you are right, it is a bug by me. 
...
> However, it will work perfectly fine for any other size or dtype
...
> Regards,
> 
> Sebastian
> 
> > -------------------------------
> > 
> > import numpy as np
> > 
> > class SgCoord():
> >     "A 2D coordinate"
> >     def __init__(self, x = 0, y = 0):
> >         self.x = x
> >         self.y = y
> >         
> > if __name__ == '__main__':
> >     l = []
> >     l.append(SgCoord(1,1))
> >     np.array(l)
> >     np.random.choice(l)
> > 
> > _______________________________________________
> > SciPy-User mailing list
> > SciPy-User <at> scipy.org
> > http://mail.scipy.org/mailman/listinfo/scipy-user
> > 
> 

Hi Sebastian,
Many thanks for your prompt reply and I can confirm that modifying the call
to include an explicit size = 1 parameter works around this issue.
Since I have your attention I'd like to clarify if there are any advantages
to using numpy.random.choice over random.choice.
Is it (numpy.random.choice)
(i) more efficient?
(ii) a more rigourous random distribution?
Regards,
Garvin.




More information about the SciPy-User mailing list