[SciPy-User] numpy.random.choice throws AttributeError when called with a list of objects.
Sebastian Berg
sebastian@sipsolutions....
Sat Feb 16 16:49:33 CST 2013
On Sat, 2013-02-16 at 22:15 +0000, Garvin Haslett wrote:
> 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.
If your example is close to your real code. Probably there is no reason
for using numpy here. (on top of it, you have conversions from list to
array that np.random.choice will do)
> Is it (numpy.random.choice)
> (i) more efficient?
If you draw a single element it is probably slower not faster (I did not
try), if you draw many it will be much faster. When compared with
random.sample this is even more so. With replace=False (random.sample
equivalent) drawing few samples when no `p` is given is not efficiently
implemented (I think it would be better to improve numpy there, and best
soon to give fewer people a headache about their random number state,
but someone has to put in some brains and time for that).
> (ii) a more rigourous random distribution?
The algorithm is the same I think, but if you need reproducible results,
it is easier to use a np.random.RandomState object and avoiding the
python functions.
In the longer run the numpy function should probably get an axis keyword
as well as another feature.
Btw: https://github.com/numpy/numpy/issues/2992 fixes the issue
Regards,
Sebastian
> Regards,
> Garvin.
>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
More information about the SciPy-User
mailing list