为什么NumPy中的full函数不能使用dtype=str?

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

Why full function in NumPy can't take dtype=str

问题

我为您提供翻译后的部分,不包括代码:

为什么我看到的只有一个字母而不是“cube”?
当我没有输入dtype="str"时,它正常工作。
我的问题是为什么?

结果:

array([[['c', 'c', 'c'],
        ['c', 'c', 'c'],
        ['c', 'c', 'c']],

       [['c', 'c', 'c'],
        ['c', 'c', 'c'],
        ['c', 'c', 'c']],

       [['c', 'c', 'c'],
        ['c', 'c', 'c'],
        ['c', 'c', 'c']]], dtype='<U1')
英文:

Why i see just one letter instead of "cube"
When i didn't type dtype="str" it worked.
My question is why?

code:

np.full((3,3,3),"cube",dtype=str)

results:

array([[['c', 'c', 'c'],
        ['c', 'c', 'c'],
        ['c', 'c', 'c']],

       [['c', 'c', 'c'],
        ['c', 'c', 'c'],
        ['c', 'c', 'c']],

       [['c', 'c', 'c'],
        ['c', 'c', 'c'],
        ['c', 'c', 'c']]], dtype='<U1')

答案1

得分: 1

np.full((3,3,3),"cube",dtype=str)

等同于

np.full((3,3,3),"cube",dtype='U1')

它将只取字符串的第一个元素。


您可以通过以下方式获得完整的结果:

np.full((3,3,3),"cube",dtype=object)

或者

np.full((3,3,3),"cube",dtype='U4') # 'U4'表示cube有4个字母,您也可以使用U>4,比如U5、U6等。

或者只需移除 dtype=str。移除后将取整个字符串的长度。

np.full((3,3,3),"cube")

英文:
np.full((3,3,3),"cube",dtype=str)

is equivalent to

np.full((3,3,3),"cube",dtype='U1')

It will just take the first element of the string.


You can get full result by:

np.full((3,3,3),"cube",dtype=object)

or

np.full((3,3,3),"cube",dtype='U4')  # U4 as you have 4 letters in cube, you can do any U>4 like U5, U6, etc.

or just remove dtype=str. Upon removing it will take the complete length of the string.

np.full((3,3,3),"cube")

答案2

得分: 1

以下是要翻译的代码部分:

if dtype is None:
    fill_value = asarray(fill_value)
    dtype = fill_value.dtype
a = empty(shape, dtype, order)
multiarray.copyto(a, fill_value, casting='unsafe')

np.asarray 可以推断出 cube 的填充值的 U4 数据类型。

但是 np.empty(shape, 'str') 生成了 U1 数据类型。它对于 fill_value 没有关于数据类型的信息。

所以,这就是为什么在没有指定数据类型的情况下,它会推断需要足够大的字符串来容纳4个字符,但是没有指定数据类型时,它只分配了1个字符的空间。

英文:

Part of the source code for full is (see the [source] link in docs):

 if dtype is None:
        fill_value = asarray(fill_value)
        dtype = fill_value.dtype
    a = empty(shape, dtype, order)
    multiarray.copyto(a, fill_value, casting='unsafe')

np.asarray can deduce a 'U4' dtype for the fill value of 'cube'.

But np.empty(shape, 'str') produces a 'U1' dtype. It has no information about fill_value.

So that's why, without your dtype, it deduces that it needs a large enough string to hold 4 characters, but without it, it just allocates space for 1 character.

答案3

得分: 0

那应该可以运行

import numpy as np
np.full((3,3,3),"cube",dtype='U4')
英文:

That should work

import numpy as np
np.full((3,3,3),"cube",dtype='U4')

答案4

得分: 0

np.full((3,3,3), "cube")

Syntax:

numpy.full(shape, fill_value, dtype=None, order='C')

英文:

Dude, just remove ( dtype : it's optional ).

Code :

np.full( (3,3,3), "cube" ) 

Syntax :

numpy.full(shape, fill_value, dtype=None, order='C')

huangapple
  • 本文由 发表于 2023年7月6日 21:03:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76629149.html
匿名

发表评论

匿名网友

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

确定