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

Sebastian Berg sebastian@sipsolutions....
Fri Feb 15 13:35:52 CST 2013


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. It is due to code ensuring that
you get an array if you give size=() being wrong for object arrays.
Anyway it means it fails for size=None and object arrays right now, so
thanks for reporting.

However, it will work perfectly fine for any other size or dtype, so I
hope it is not a big deal for you, as it will give you no trouble if
your real application draws multiple items at once.

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@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
> 




More information about the SciPy-User mailing list