将不同的数据类型存储在一个 NumPy 数组中? [英] Store different datatypes in one NumPy array?

查看:31
本文介绍了将不同的数据类型存储在一个 NumPy 数组中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个不同的数组,一个是字符串,另一个是整数.我想将它们连接成一个数组,其中每一列都具有原始数据类型.我当前执行此操作的解决方案(见下文)将整个数组转换为 dtype = string,这似乎内存效率很低.

combined_array = np.concatenate((A, B), axis = 1)

A.dtype = stringB.dtype = int 时,是否可以在combined_array 中多重dtypes?

解决方案

一种方法可能是使用 记录数组.列"不会像标准 numpy 数组的列,但对于大多数用例,这已经足够了:

<预><代码>>>>a = numpy.array(['a', 'b', 'c', 'd', 'e'])>>>b = numpy.arange(5)>>>记录 = numpy.rec.fromarrays((a, b), names=('keys', 'data'))>>>记录rec.array([('a', 0), ('b', 1), ('c', 2), ('d', 3), ('e', 4)],dtype=[('keys', '|S1'), ('data', '<i8')])>>>记录['键']rec.array(['a', 'b', 'c', 'd', 'e'],dtype='|S1')>>>记录['数据']数组([0, 1, 2, 3, 4])

请注意,您还可以通过指定数组的数据类型对标准数组执行类似操作.这称为结构化数组":

<预><代码>>>>arr = numpy.array([('a', 0), ('b', 1)],dtype=([('keys', '|S1'), ('data', 'i8')]))>>>阿尔数组([('a', 0), ('b', 1)],dtype=[('keys', '|S1'), ('data', '<i8')])

不同之处在于记录数组还允许对单个数据字段进行属性访问.标准结构化数组没有.

<预><代码>>>>记录.keyschararray(['a', 'b', 'c', 'd', 'e'],dtype='|S1')>>>arr.keys回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中AttributeError: 'numpy.ndarray' 对象没有属性 'keys'

I have two different arrays, one with strings and another with ints. I want to concatenate them, into one array where each column has the original datatype. My current solution for doing this (see below) converts the entire array into dtype = string, which seems very memory inefficient.

combined_array = np.concatenate((A, B), axis = 1)

Is it possible to mutiple dtypes in combined_array when A.dtype = string and B.dtype = int?

解决方案

One approach might be to use a record array. The "columns" won't be like the columns of standard numpy arrays, but for most use cases, this is sufficient:

>>> a = numpy.array(['a', 'b', 'c', 'd', 'e'])
>>> b = numpy.arange(5)
>>> records = numpy.rec.fromarrays((a, b), names=('keys', 'data'))
>>> records
rec.array([('a', 0), ('b', 1), ('c', 2), ('d', 3), ('e', 4)], 
      dtype=[('keys', '|S1'), ('data', '<i8')])
>>> records['keys']
rec.array(['a', 'b', 'c', 'd', 'e'], 
      dtype='|S1')
>>> records['data']
array([0, 1, 2, 3, 4])

Note that you can also do something similar with a standard array by specifying the datatype of the array. This is known as a "structured array":

>>> arr = numpy.array([('a', 0), ('b', 1)], 
                      dtype=([('keys', '|S1'), ('data', 'i8')]))
>>> arr
array([('a', 0), ('b', 1)], 
      dtype=[('keys', '|S1'), ('data', '<i8')])

The difference is that record arrays also allow attribute access to individual data fields. Standard structured arrays do not.

>>> records.keys
chararray(['a', 'b', 'c', 'd', 'e'], 
      dtype='|S1')
>>> arr.keys
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'numpy.ndarray' object has no attribute 'keys'

这篇关于将不同的数据类型存储在一个 NumPy 数组中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆