English 中文(简体)
第64号浮线至喷雾
原标题:Convert ndarray from float64 to integer

页: 1 我将阵列变成了一批愤怒。 我应如何这样做?

<>代码>int(> 赢得了t work,因为它说,它可以将其转换成体。 改变<条码>d 类型的外地本身显然不做任何工作,因为实际的墨迹已经改变。 在谷歌或文件中,我似乎找不到什么东西——这是怎样做的最好方法?

最佳回答

使用<代码>。

>>> a = numpy.array([1, 2, 3, 4], dtype=numpy.float64)
>>> a
array([ 1.,  2.,  3.,  4.])
>>> a.astype(numpy.int64)
array([1, 2, 3, 4])

See documentation

问题回答

虽然<条码>作为类型可能是“最佳”选择,但也有几种其他方法将其转换成一个分类阵列。 采用<代码>arr的Im 实例如下:

>>> import numpy as np
>>> arr = np.array([1,2,3,4], dtype=float)
>>> arr
array([ 1.,  2.,  3.,  4.])

The int* functions from NumPy

>>> np.int64(arr)
array([1, 2, 3, 4])

>>> np.int_(arr)
array([1, 2, 3, 4])

The NumPy *array functions themselves:

>>> np.array(arr, dtype=int)
array([1, 2, 3, 4])

>>> np.asarray(arr, dtype=int)
array([1, 2, 3, 4])

>>> np.asanyarray(arr, dtype=int)
array([1, 2, 3, 4])

The astype method (that was already mentioned but for completeness sake):

>>> arr.astype(int)
array([1, 2, 3, 4])

请注意,通过<条码>int作为<条码>的模版或<条码>,将出现一种基于你的平台的缺省分类。 例如,在Windows上,它将在64bit int32上,与64bit Zhu其int<64>/code>。 如果您需要一种具体的分类类型,并想避免平台的“模糊”,那么你应当使用相应的NumPy类型,如np.int32np.int64

还进行了真正有益的讨论,将现有的阵列转换成。 单体阵列的内型转换。 如果你对复制贵国的阵列(即<条码>的类型>)表示担心,则肯定会核对这一联系。

所有我都使用过。

numpyfloat = (1.0, 2.0, 4.0)
a = numpy.array(numpyfloat, dtype=numpy.int)

那是





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...