[英]Float Array is not converted to int: Python
I have a float numpy array x, which contains values like, 0, .5, 1, 1.5,etc.我有一个 float numpy 数组 x,其中包含 0、.5、1、1.5 等值。 I want to convert the float values into integers based on some equation and store them in a new array, newx.
我想根据某个方程将浮点值转换为整数,并将它们存储在一个新数组 newx 中。 I did this,
我这样做了,
newx=np.zeros(x.shape[0])
for i in range (x.shape [0]):
newx[i]= ((2*x[i]) +1)
print(newx, v)
However, when printing xnew, I get values like但是,在打印 xnew 时,我得到的值如下
(array([ 1., 2., 3., 4., 5., 6., 7., 8., 9.,
10., 11., 12., 13., 14., 15., 16., 17., 18.])
newx must be used in some process, and it must be integer, when I want to use it in that process, I get an error stating that it must be of integer or Boolean type. newx 必须在某个进程中使用,并且它必须是整数,当我想在该进程中使用它时,我收到一个错误,指出它必须是整数或布尔类型。 Can anyone please tell me what mistake I've done?
谁能告诉我我犯了什么错误?
Thank You.谢谢你。
Numpy is specifically designed for array manipulation. Numpy 专为数组操作而设计。 Try not to iterate over a numpy array like you did.
尽量不要像你那样迭代一个 numpy 数组。 You can read about how numpy datatypes are a little different than inbuilt datatypes.
您可以阅读有关 numpy 数据类型与内置数据类型有何不同的信息。 This leads to much higher run times.
这会导致更长的运行时间。
Anyways Here is a working code for your problem无论如何这是您的问题的工作代码
newx=x*2+1
newx=numpy.int16(newx) # as easy as this. ;)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.