If I have a numpy array x
, I can get its data type by using dtype
like this:
如果我有一个numpy数组x,我可以通过如下的dtype获得它的数据类型:
t = x.dtype
However, that obviously won't work for things like lists. I wonder if there is a standard way of retrieving types for lists and numpy arrays. In the case of lists, I guess this would mean the largest type which fits all of the data. For instance, if
但是,这显然不能用于列表之类的工作。我想知道是否有一种标准的方法来检索列表和numpy数组的类型。在列表的情况下,我想这意味着最大的类型可以满足所有的数据。例如,如果
x = [ 1, 2.2 ]
I would want such a method to return float
, or better yet numpy.float64
.
我希望这样的方法返回浮点数,或者更好的方法是numpi .float64。
Intuitively, I thought that this was the purpose of the numpy.dtype
method. However, that is not the case. That method is used to create a type, not extract a type.
直觉上,我认为这就是麻木的目的。dtype方法。然而,事实并非如此。该方法用于创建类型,而不是提取类型。
The only method that I know of getting a type is to wrap whatever object is passed in with a numpy array, and then get the dtype
:
我所知道的获取类型的唯一方法是使用numpy数组封装传入的任何对象,然后获取dtype:
def dtype(x):
return numpy.asarray(x).dtype
The issue with this approach, however, is that it will copy the array if it is not already a numpy array. In this circumstance, that is extremely heavy for such a simple operation.
但是,这种方法的问题是,如果数组不是numpy数组,它将复制数组。在这种情况下,对于这样一个简单的操作来说,这是非常沉重的。
So is there a numpy
method that I can use which won't require me to do any list copies?
那么有没有一种我可以使用的numpy方法,它不需要我做任何列表拷贝?
EDIT
I am designing a library for doing some geometric manipulations... Conversions between rotation matrices, rotation vectors, quaternions, euler angles, etc.
我正在设计一个库来做一些几何操作……旋转矩阵、旋转向量、四元数、欧拉角之间的转换。
It can easily happen that the user is simply working with a single rotation vector (which has 3 elements). In that case, they might write something like
很容易发生的是,用户只使用一个旋转矢量(它有3个元素)。在这种情况下,他们可能会这样写
q = vectorToQuaternion([ .1, 0, 0 ])
In this case, I would want the output quaternion to be a numpy array of type numpy.float64
. However, sometimes to speed up the calculations, the user might want to use a numpy array of float32's:
在这种情况下,我希望输出四元数是numpy类型为numpi .float64的数组。但是,有时为了加快计算速度,用户可能需要使用float32的numpy数组:
q = vectorToQuaternion(numpy.float32([ .1, 0, 0 ]))
In which case, I think it is natural to expect that the output is the same type.
在这种情况下,我认为期望输出是相同的类型是很自然的。
The issue is that I cannot use the zeros_like
function (or empty_like
, etc) because a quaternion has 4 components, while a vector has 3. So internally, I have to do something like
问题是我不能使用zeros_like函数(或empty_like等),因为一个四元数有4个分量,而一个向量有3个分量。所以在内部,我必须做一些类似的事情
def vectorToQuaternion(v):
q = empty( (4,), dtype = asarray(v).dtype )
...
If there was a way of using empty_like
which extracts all of the properties of the input, but lets me specify the shape of the output, then that would be the ideal function for me. However, to my knowledge, you cannot specify the shape in the call to empty_like
.
如果有一个使用empty_like的方法来提取输入的所有属性,但是让我指定输出的形状,那么这将是我的理想函数。但是,据我所知,您不能在empty_like调用中指定形状。
EDIT
Here are some gists for the class I am talking about, and a test class (so that you can see how I intend to use it).
这里有一些我正在谈论的类的gists和一个测试类(以便您可以看到我打算如何使用它)。
Class: https://gist.github.com/mholzel/c3af45562a56f2210270d9d1f292943a
类:https://gist.github.com/mholzel/c3af45562a56f2210270d9d1f292943a
Tests: https://gist.github.com/mholzel/1d59eecf1e77f21be7b8aadb37cc67f2
测试:https://gist.github.com/mholzel/1d59eecf1e77f21be7b8aadb37cc67f2
1 个解决方案
#1
2
If you really want to do it that way you will probably have to use np.asarray
, but I'm not sure that's the most solid way of dealing with the problem. If the user forgets to add .
and gives [1, 0, 0]
then you will be creating integer outputs, which most definitely does not make sense for quaternions. I would default to np.float64
, using the dtype
of the input if it is an array of some float type, and maybe also giving the option to explicitly pass a dtype:
如果你真的想这么做你可能需要使用np。asarray,但我不确定这是处理问题的最可靠的方式。如果用户忘记添加。给出[1,0,0]然后你将创建整数输出,这对于四元数来说显然是没有意义的。我会默认为np。float64,如果输入是某种浮点类型的数组,则使用输入的dtype,并且可能还提供了显式传递dtype的选项:
import numpy as np
def vectorToQuaternion(v, dtype=None):
if dtype is None:
if isinstance(v, np.ndarray) and np.issubdtype(v.dtype, np.floating):
# Or if you prefer:
if np.issubdtype(getattr(v, 'dtype', np.int), np.floating):
dtype = v.dtype
else:
dtype = np.float64
q = np.empty((4,), dtype=dtype)
# ...
#1
2
If you really want to do it that way you will probably have to use np.asarray
, but I'm not sure that's the most solid way of dealing with the problem. If the user forgets to add .
and gives [1, 0, 0]
then you will be creating integer outputs, which most definitely does not make sense for quaternions. I would default to np.float64
, using the dtype
of the input if it is an array of some float type, and maybe also giving the option to explicitly pass a dtype:
如果你真的想这么做你可能需要使用np。asarray,但我不确定这是处理问题的最可靠的方式。如果用户忘记添加。给出[1,0,0]然后你将创建整数输出,这对于四元数来说显然是没有意义的。我会默认为np。float64,如果输入是某种浮点类型的数组,则使用输入的dtype,并且可能还提供了显式传递dtype的选项:
import numpy as np
def vectorToQuaternion(v, dtype=None):
if dtype is None:
if isinstance(v, np.ndarray) and np.issubdtype(v.dtype, np.floating):
# Or if you prefer:
if np.issubdtype(getattr(v, 'dtype', np.int), np.floating):
dtype = v.dtype
else:
dtype = np.float64
q = np.empty((4,), dtype=dtype)
# ...