TypeError: 数据类型 ‘>’ 无法使用 numpy 中的 dtype 理解

huangapple go评论81阅读模式
英文:

TypeError: data type '>' not understood using dtype from numpy

问题

首先,我看到了类似的问题,但没有任何帮助我。

我正在尝试对元组列表进行排序,并根据我获得的元组列表转换数据类型。

例如,如果我有一个元组列表,每个元组都构建如下:

(ID, Grade, Height)

A = [(123, 23, 67), (234, 67, 45)]

我有一个类型列表如下:

[(ID, int), (Grade, 's15'), (Height, float)]

现在我了解到 's15' 是 NumPy 中的数据类型,但我似乎无法使用它。我尝试从文档中复制:

import numpy as np
dt = np.dtype(('>14'))

但我得到的只是这个错误:

dt = np.dtype(('>14'))
TypeError: 数据类型 '> ' 无法理解

我复制的文档链接如下:

NumPy 数据类型文档

还有通用的转换器可以用来转换成任何给定的类型吗?

英文:

first I saw similar questions but nothing helped me.
I'm trying to sort list of tuples, and convert the data types inside the tuple,
convert it according to a list of tuples I get.
for example, if I have a list of tuple, every tuple is built like

(ID,Grade,Height)

A = [(123,23,67),(234,67,45)]

and I have a list of type like that:

[(ID,int),(grade,'s15'),(height,float)]

now I read that 's15' is a dtype from bumpy, but I can't seem to use it.
I tried to copy from the docs:

import numpy as np
dt = np.dtype(('>14'))

but all I get is this error:

dt = np.dtype(('>14'))
TypeError: data type '>' not understood

the docs I copied from:

https://numpy.org/doc/stable/reference/arrays.dtypes.html

and is there a generic converter I can use to convert to any type I'm given?

答案1

得分: 1

我认为你可能忽视了你所指的文档。
你使用了
dt = np.dtype(('>14'))
这是 >14(十四)...

但事实上文档清楚地提到了
dt = np.dtype('>i4')

这是 i4 而不是 1(一)

此外,根据文档,>< 指定了每种 dtype 的上限/下限,例如 >i 将是大端整数(请参阅 Endianess

然后之后的数字会指示给 dtype 的字节数(请参阅 docs

最后,S 表示“零终止字节”

根据你的描述,你的老师想要“大端 ~128 位零终止字节”

此外,
dt = np.dtype(('>S15'))
也可以正常工作。

希望这能解决你的问题

英文:

I think you maybe overlooked the documentation you are referring.
You used

dt = np.dtype((&#39;&gt;14&#39;))

which is >14 (fourteen)...

But in fact the documentation clearly mentions

dt = np.dtype(&#39;&gt;i4&#39;)

which is i4 not 1 (one)

Also based on the docs &gt; or &lt; specifies upper/lower bound for each dtype, for example &gt;i would be big endian integer (see Endianess)

And the number after that would indicate number of bytes given to the dtype (see docs)

Finally the S indicates Zero terminated bytes

Based on your description, your teacher wants Upper endian ~128 bit Zero terminated bytes

Furthermore,

dt = np.dtype((&#39;&gt;S15&#39;))

works fine.

I hope this fixes your issue

huangapple
  • 本文由 发表于 2023年6月1日 20:19:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76381785.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定