简体   繁体   English

如何在numpy中将ndarray的dtype更改为自定义的?

[英]How to change the dtype of a ndarray to custom one in numpy?

I made a dtype that is: 我做了一个dtype:

mytype = np.dtype([('a',np.uint8), ('b',np.uint8), ('c',np.uint8)])

so the array using this dtype: 所以使用这个dtype的数组:

test1 = np.zeros(3, dtype=mytype)

test1 is: test1是:

array([(0, 0, 0), (0, 0, 0), (0, 0, 0)],
      dtype=[('a', '|u1'), ('b', '|u1'), ('c', '|u1')])

Now I have test2: 现在我有test2:

test2 = np.array([[1,2,3], [4,5,6], [7,8,9]])

When I use test2.astype(mytype) , the result is not what I want to be: 当我使用test2.astype(mytype) ,结果不是我想要的结果:

array([[(1, 1, 1), (2, 2, 2), (3, 3, 3)],
       [(4, 4, 4), (5, 5, 5), (6, 6, 6)],
       [(7, 7, 7), (8, 8, 8), (9, 9, 9)]],
      dtype=[('a', '|u1'), ('b', '|u1'), ('c', '|u1')])

I want the result to be: 我希望结果如下:

array([(1, 2, 3), (4, 5, 6), (7, 8, 9)],
      dtype=[('a', '|u1'), ('b', '|u1'), ('c', '|u1')])

Is there any way? 有什么办法吗? Thanks. 谢谢。

You can use the fromarrays method of numpy.core.records (see documentation ): 您可以使用fromarrays方法(请参阅文档 ):

np.rec.fromarrays(test2.T, mytype)
Out[13]: 
rec.array([(1, 2, 3), (4, 5, 6), (7, 8, 9)], 
      dtype=[('a', '|u1'), ('b', '|u1'), ('c', '|u1')])

The array has to be transposd first because the functions regards the rows of the array as the columns of the structured array in the output. 必须首先转置数组,因为函数将数组的行视为输出中结构化数组的列。 See also this question: Converting a 2D numpy array to a structured array 另请参阅此问题: 将2D numpy数组转换为结构化数组

Because all the fields are the same type, you can also use: 因为所有字段都是相同的类型,您还可以使用:

>>> test2.astype(np.uint8).view(mytype).squeeze(axis=-1)
array([(1, 2, 3), (4, 5, 6), (7, 8, 9)], 
      dtype=[('a', 'u1'), ('b', 'u1'), ('c', 'u1')])

The squeeze is needed because test2 is 2d, but you wanted a 1d result 需要挤压因为test2是2d,但你想要1d的结果

When creating the array, if the input iterable contains tuples (which are guaranteed to be immutable) instead of lists (which are guaranteed not to be) then it will automatically take the input in the way you desire so long as the number of items in each tuple equals the number of fields in the structure: 在创建数组时,如果输入iterable包含元组(保证是不可变的)而不是列表(保证不是),那么只要输入的项目数量,它就会自动以你想要的方式输入每个元组等于结构中的字段数:

In[7]: test2 = np.array([(1,2,3), (4,5,6), (7,8,9)], dtype = mytype)

In[8]: test2
Out[8]: 
array([(1, 2, 3), (4, 5, 6), (7, 8, 9)],
      dtype=[('a', 'u1'), ('b', 'u1'), ('c', 'u1')])

There is no need to go to np.rec etc for just this. 没有必要去np.rec等。 If however the input iterable contains lists and not tuples, then numpy doesn't take the fields one for one as you expect and does the data duplication. 但是,如果输入iterable包含列表而不包含元组,则numpy不会按预期一对一地获取字段并进行数据复制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM