Python numpy—16、(16)和(16,)之间的形状差异 问题:
请在下面的代码中帮助理解问题的原因,并建议相关文章进行研究 背景 据我所知,包含子数组的具有多个字段的numpy结构化类型定义为:Python numpy—16、(16)和(16,)之间的形状差异 问题:,python,numpy,Python,Numpy,请在下面的代码中帮助理解问题的原因,并建议相关文章进行研究 背景 据我所知,包含子数组的具有多个字段的numpy结构化类型定义为: the_type = np.dtype( [ # ndarray (<name>, <numpy dtype>, <numpy shape>) # (name, dtype, shape) ] ) array([('x', 'y', [
the_type = np.dtype(
[ # ndarray
(<name>, <numpy dtype>, <numpy shape>) # (name, dtype, shape)
]
)
array([('x', 'y', ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'], [1., 2.])],
dtype=[('first', '<U16'), ('middle', '<U16'), ('last', '<U10', (16,)), ('grades', '<f8', (2,))])
16
和(16)
都是(16,)
。它是正确的还是取决于数据类型“hoge”[3]->“e”
,那么为什么(16,)是一个错误呢dt=np.dtype(
[
('first',np.unicode,16),#正常,无警告
('middle',np.unicode_16)),#正常且无警告
('last',np.unicode_16,),#1 dt=np.dtype(
2 [
3(“第一”,np.16),
4('middle',np.unicode_uu16)),
5('last',np.unicode_u16,),
ValueError:泛型类型元组中的itemsize无效
更新
我理解我误解了数据类型。在这种情况下,不需要形状而是长度。正如错误所示,
('first',np.unicode,16)
中的第三个位置被解释为元组元素类型的大小。因此,first
被定义为大小为16的unicode字段
('middle',np.unicode_u16))
也可以工作,因为(16)
的计算结果只是16
,括号是多余的。因此,middle
将与first
一样
但是,('last',np.unicode_u16,)
会导致错误,因为您正在将一个元组
作为只有一个维度的元组元素类型的项大小
传递。(16,)
只能理解为元组,不能自动计算为标量,而np.dtype
需要标量作为np.unicode\ucode>字段的itemsize
如果您的目标是定义一个字段,该字段采用16个长度(例如10)的unicode值数组,则可以使用:
dt = np.dtype(
[
('first', np.unicode_, 16),
('middle', np.unicode_, (16)),
('last', 'U10', (16,)),
('grades', np.float64, (2,))
]
)
然后您可以定义一个数组,如:
a = np.array([('x','y',
['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'],
[1.0, 2.0])], dt)
a
将被定义为:
the_type = np.dtype(
[ # ndarray
(<name>, <numpy dtype>, <numpy shape>) # (name, dtype, shape)
]
)
array([('x', 'y', ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'], [1., 2.])],
dtype=[('first', '<U16'), ('middle', '<U16'), ('last', '<U10', (16,)), ('grades', '<f8', (2,))])
你写道
“我相信16和(16)都是(16,)”
这是错误的
在python中,有一个名为tuple
x = (1, 56, 7, 9, 13)
x = (1 + 5) * 9
x = ( 6 ) * 9
x = 6 * 9
x = 54
下面的代码提供了一个元组的示例
x = (1, 56, 7, 9, 13)
x = (1 + 5) * 9
x = ( 6 ) * 9
x = 6 * 9
x = 54
以下代码不创建包含数字16的元组
y = (16)
print(type(y))
# <class 'int'>
所以…(16)
不是一个元组
16
是整数。16
不是元组
(16)
也是一个整数。(16)
不是一个元组
(16,)
是一个元组
忽略以下代码中除容器和for循环之外的所有内容。
尝试在(5)
上循环。结果与在(1、2、3)
上进行for循环截然不同
(5)
不是容器。
(5)
有括号,表示某些数学运算的顺序
请注意,(1+2)
可能看起来像括号内的不止一个东西
x = ((1 + 2) * 7)
x = ((3) * 7)
x = (3 * 7)
x = (21)
x = 21
x = int.__mul__(int.__add__(1, 2), 7)
(1+2)
实际上只是括号内的一个对象
x = ((1 + 2) * 7)
x = ((3) * 7)
x = (3 * 7)
x = (21)
x = 21
x = int.__mul__(int.__add__(1, 2), 7)
加号(+
)是函数int.\uuuuu add\uuuuu
返回的输出
即使它看起来像很多东西,mathy括号内的所有内容最终都会压缩为括号内的一个值
x = ((1 + 2) * 7)
x = ((3) * 7)
x = (3 * 7)
x = (21)
x = 21
x = int.__mul__(int.__add__(1, 2), 7)
根据上面代码中的警告,我相信16和(16)都是(16,)。它是正确的还是取决于数据类型
在Python中,16
是一个整数文本,(16)
是一个Python括号表达式,其计算结果为值16
。(请记住,用括号括住表达式时,这样做是为了控制运算符的求值顺序,而不是将表达式转换为元组。例如,在表达式(2+3)中/2
,包围2+3
的括号不会生成元组;相反,它们只用于确保+
运算符在/
运算符之前得到求值
在Python中,(16,)
肯定是一个元组,因此它不等同于16
或(16)
我认为Unicode字符串在Python中被称为“hoge”[3]->“e”
不,在Python中,Unicode字符串不是数组。您能够对Unicode字符串执行索引操作并不一定使其成为数组。因此,您也可以对dict
执行[]
操作,dict
也不是数组
那么为什么(16,)是一个错误呢
在numpy
中,当您将字段指定为unicode字符串时,numpy
需要知道该字符串中将包含多少unicode字符。(numpy
仅支持固定长度字符串作为自定义dtype
的字段)。换句话说,您需要告诉numpy
unicode字符串的长度是多少。当然,这必须是一个简单的整数16
,而不是元组(16,)
顺便说一句,如果不指定unicode字符串字段的长度,则不会有任何错误,因为numpy
将假定该字段为零长度的unicode字符串;在为字符串字段赋值时会出现错误。字符串长度与字段形状不同
这是一个包含两个字段的数组,一个字段具有字符串数据类型,另一个字段为数字:
In [148]: np.array([('abc',2),('defg',5)], dtype=[('x','U10'),('y',int)] )
Out[148]: array([('abc', 2), ('defg', 5)], dtype=[('x', '<U10'), ('y', '<i8')])
In [149]: _.shape
Out[149]: (2,)
In [150]: __['x']
Out[150]: array(['abc', 'defg'], dtype='<U10')
但是如果我提供一个n
In [148]: np.array([('abc',2),('defg',5)], dtype=[('x','U10'),('y',int)] )
Out[148]: array([('abc', 2), ('defg', 5)], dtype=[('x', '<U10'), ('y', '<i8')])
In [149]: _.shape
Out[149]: (2,)
In [150]: __['x']
Out[150]: array(['abc', 'defg'], dtype='<U10')
In [151]: np.array([('abc',2),('defg',5)], dtype=[('x','U',10),('y',int)] )
Out[151]: array([('abc', 2), ('defg', 5)], dtype=[('x', '<U10'), ('y', '<i8')])
In [152]: np.array([('abc',[2,3]),('defg',[5,4])], dtype=[('x','U',10),('y',int,2)] )
Out[152]:
array([('abc', [2, 3]), ('defg', [5, 4])],
dtype=[('x', '<U10'), ('y', '<i8', (2,))])
In [153]: _['y'] # shape (2,2)
Out[153]:
array([[2, 3],
[5, 4]])
In [155]: np.array([(['abc','xuz'],),(['defg','foo'],)], dtype=[('x','U10',2)] )
Out[155]: array([(['abc', 'xuz'],), (['defg', 'foo'],)], dtype=[('x', '<U10', (2,))])
In [156]: _['x']
Out[156]:
array([['abc', 'xuz'],
['defg', 'foo']], dtype='<U10')
In [158]: np.array([(['abc','xuz'],),(['defg','foo'],)], dtype=[('x',('U',10),2)] )
Out[158]: array([(['abc', 'xuz'],), (['defg', 'foo'],)], dtype=[('x', '<U10', (2,))])
In [166]: np.zeros((1,), dtype=[('x','U',10)])
Out[166]: array([('',)], dtype=[('x', '<U10')])
In [167]: np.zeros((1,), dtype=[('x','U10')])
Out[167]: array([('',)], dtype=[('x', '<U10')])
In [168]: np.zeros((1,), dtype=[('x','U1',10)])
Out[168]:
array([(['', '', '', '', '', '', '', '', '', ''],)],
dtype=[('x', '<U1', (10,))])
In [169]: np.zeros((1,), dtype=[('x','U1',(2,3))])
Out[169]: array([([['', '', ''], ['', '', '']],)], dtype=[('x', '<U1', (2, 3))])
In [170]: _['x']
Out[170]:
array([[['', '', ''],
['', '', '']]], dtype='<U1')
In [171]: _.shape
Out[171]: (1, 2, 3)
In [175]: np.zeros((1,), dtype=[('x','U10')])
Out[175]: array([('',)], dtype=[('x', '<U10')])
In [176]: _['x'].shape
Out[176]: (1,)
In [177]: np.zeros((1,), dtype=[('x','U10',1)])
<ipython-input-177-932c79fbeaf4>:1: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
np.zeros((1,), dtype=[('x','U10',1)])
Out[177]: array([('',)], dtype=[('x', '<U10')])
In [178]: _['x'].shape
Out[178]: (1,)
In [179]: np.zeros((1,), dtype=[('x','U10',(1,))])
Out[179]: array([([''],)], dtype=[('x', '<U10', (1,))])
In [180]: _['x'].shape
Out[180]: (1, 1)
In [183]: np.zeros((1,), dtype=[('x','U10',2)])['x'].shape
Out[183]: (1, 2)
In [184]: np.zeros((1,), dtype=[('x','U10',(2,))])['x'].shape
Out[184]: (1, 2)