将不同的数据类型存储在一个NumPy数组中?

时间:2022-07-21 21:17:41

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.

我有两个不同的数组,一个是字符串,另一个是整数。我想将它们连接到一个数组中,其中每列都具有原始数据类型。我目前的解决方案(见下文)将整个数组转换为dtype = string,这看起来非常低效。

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

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?

当A.dtype = string和B.dtype = int时,是否可以在combined_array中多次使用dtypes?

2 个解决方案

#1


29  

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:

一种方法可能是使用记录数组。 “列”将不像标准numpy数组的列,但对于大多数用例,这就足够了:

>>> 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'

#2


4  

A simple solution: convert your data to object 'O' type

一个简单的解决方案:将数据转换为对象'O'类型

z = np.zeros((2,2), dtype='U2')
o = np.ones((2,1), dtype='O')
np.hstack([o, z])

creates the array:

创建数组:

array([[1, '', ''],
       [1, '', '']], dtype=object)

#1


29  

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:

一种方法可能是使用记录数组。 “列”将不像标准numpy数组的列,但对于大多数用例,这就足够了:

>>> 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'

#2


4  

A simple solution: convert your data to object 'O' type

一个简单的解决方案:将数据转换为对象'O'类型

z = np.zeros((2,2), dtype='U2')
o = np.ones((2,1), dtype='O')
np.hstack([o, z])

creates the array:

创建数组:

array([[1, '', ''],
       [1, '', '']], dtype=object)