python 3.x - 'float' object is unsliceable -
i'm trying generate 2 classes of random 2d points, 1 having mean of [1,1] , other mean of [-1,-1]. have written function error can't figure out. googled didn't find anything. here's function :
def gen_arti_bis(nbex=10,centerx=1,centery=1,sigma=0.1,epsilon=0.02): xpos=np.random.multivariate_normal([centerx,centery],np.diag([sigma,sigma]),nbex/2) xneg=np.random.multivariate_normal([-centerx,-centery],np.diag([sigma,sigma]),nbex/2) data=np.vstack((xpos,xneg)) y=np.hstack((np.ones(nbex/2),-np.ones(nbex/2))) return data,y
and here's error message when type gen_arti():
traceback (most recent call last): file "<ipython-input-64-a173cf922dac>", line 1, in <module> gen_arti_bis() file "<ipython-input-63-da8720093c11>", line 2, in gen_arti_bis xpos=np.random.multivariate_normal([centerx,centery],np.diag([sigma,sigma]),nbex/2) file "mtrand.pyx", line 4308, in mtrand.randomstate.multivariate_normal (numpy/random/mtrand/mtrand.c:23108) typeerror: 'float' object unsliceable
in python 3, division using /
operator floating point division, if numbers on both sides of operator integers. in several places you're computing nbex / 2
, , passing result argument numpy expects integer.
specifically, np.random.multivariate
's last argument supposed either int
or tuple
of int
s. you're passing in float
, won't accept, if float's value integer (e.g. 5.0
). you're passing float
np.ones
, function seems handle ok (it ignores fractional part of input number).
the basic fix explicitly perform integer division using //
operator. replace each place have nbex / 2
nbex // 2
, should work intended to.
note integer division performed //
pick floor value (i.e. rounds down, towards negative infinity). if want round differently in situations, may want division /
, convert float result integer round
(which round value half way between 2 integers ever 1 even) or math.ceil
(which round up).
Comments
Post a Comment