“浮动"对象无法切片 [英] 'float' object is unsliceable

查看:118
本文介绍了“浮动"对象无法切片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试生成两类随机2D点,一类的平均值为[1,1],另一类的平均值为[-1,-1].我已经编写了一个函数,但是出现了一个无法弄清的错误.我用谷歌搜索,但没有找到任何东西.这是我的功能:

I'm trying to generate two classes of random 2D points, one having a mean of [1,1] and the other a mean of [-1,-1]. I have written a function but I get an error that can't figure out. I googled it but didn't find anything. Here's my 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

这是当我键入gen_arti()时的错误消息:

and here's the error message when I 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 is unsliceable

推荐答案

在Python 3中,使用/运算符进行除法运算始终会进行浮点除法,即使运算符两侧的数字均为整数也是如此.在多个地方,您正在计算nbex / 2,并将结果作为参数传递给numpy需要一个整数.

In Python 3, division using the / operator always does floating point division, even if the numbers on both sides of the operator are integers. In several places you're computing nbex / 2, and passing the result as an argument where numpy expects an integer.

具体地说,np.random.multivariate的最后一个参数应该是intinttuple.您传入的是float,即使float的值实际上是整数(例如5.0),它也不会接受.您还向np.ones传递了float,但是该函数似乎可以正常处理(它忽略了输入数字的任何小数部分).

Specifically, np.random.multivariate's last argument is supposed to be either an int or a tuple of ints. You're passing in a float, which it won't accept, even if the float's value is actually an integer (e.g. 5.0). You're also passing a float to np.ones, but that function seems to handle it OK (it ignores any fractional part of the input number).

最基本的解决方法是使用//运算符显式执行整数除法.用nbex // 2替换您拥有nbex / 2的每个地方,它应该可以正常工作.

The most basic fix for this is to explicitly perform integer division using the // operator. Replace each place you have nbex / 2 with nbex // 2 and it should work as you intended it to.

请注意,由//执行的整数除法将始终选择底值(即向下舍入,朝负无穷大).如果要在某些情况下以不同的方式四舍五入,则可能需要用/进行除法,然后将浮点结果转换为用round的整数(这将舍入一个介于两个整数之间的中间值的值)一个是偶数)或math.ceil(将始终取整).

Note that the integer division performed by // will always pick the floor value (i.e. it rounds down, towards negative infinity). If you want to round differently in some situations, you may want to do your division with / and then convert the float result to an integer with round (which will round a value that is half way between two integers to which ever one is even) or math.ceil (which will always round up).

这篇关于“浮动"对象无法切片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆