numpy,命名列 [英] numpy, named columns

查看:220
本文介绍了numpy,命名列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于 numpy 的简单问题:

我将100个值加载到向量a中.从这个向量中,我想创建一个包含2列的数组A,其中一列的名称为"C1",第二列的名称为"C2",一列的类型为int32,另一列的类型为int64.一个例子:

I load 100 values to a vector a. From this vector, I want to create an array A with 2 columns, where one column has name "C1" and second one "C2", one has type int32 and another int64. An example:

a = range(100)
A = array(a).reshape( len(a)/2, 2)
# A.dtype = ...?

当我从a创建数组时,如何定义列的类型和名称?

How to define the columns' types and names, when I create array from a?

推荐答案

NumPy结构化数组具有命名列:

NumPy structured arrays have named columns:

import numpy as np

a=range(100)
A = np.array(zip(*[iter(a)]*2),dtype=[('C1','int32'),('C2','int64')])
print(A.dtype)
# [('C1', '<i4'), ('C2', '<i8')]

您可以按以下名称访问列:

You can access the columns by name like this:

print(A['C1'])
# [ 0  2  4  6  8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48
#  50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98]

请注意,将np.arrayzip一起使用会导致NumPy从元组的临时列表中构建数组. Python元组列表比等效的NumPy数组使用更多的内存.因此,如果您的阵列非常大,则可能不希望使用zip.

Note that using np.array with zip causes NumPy to build an array from a temporary list of tuples. Python lists of tuples use a lot more memory than equivalent NumPy arrays. So if your array is very large you may not want to use zip.

相反,给定NumPy数组A,您可以使用ravel()A设置为一维 数组,然后使用view将其转换为结构化数组,然后使用astype将列转换为所需的类型:

Instead, given a NumPy array A, you could use ravel() to make A a 1D array, and then use view to turn it into a structured array, and then use astype to convert the columns to the desired type:

a = range(100)
A = np.array(a).reshape( len(a)//2, 2)
A = A.ravel().view([('col1','i8'),('col2','i8'),]).astype([('col1','i4'),('col2','i8'),])
print(A[:5])
# array([(0, 1), (2, 3), (4, 5), (6, 7), (8, 9)], 
#       dtype=[('col1', '<i4'), ('col2', '<i8')])

print(A.dtype)
# dtype([('col1', '<i4'), ('col2', '<i8')])

这篇关于numpy,命名列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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