我在Python 2.7中运行Numpy 1.6,并且有一些我从另一个模块获得的一维数组。我想把这些数组并打包成一个结构化数组,这样我就可以按名称索引原始的1D数组。我无法弄清楚如何将1D阵列放入2D阵列并使dtype访问正确的数据。我的MWE如下:
>>> import numpy as np
>>>
>>> x = np.random.randint(10,size=3)
>>> y = np.random.randint(10,size=3)
>>> z = np.random.randint(10,size=3)
>>> x
array([9, 4, 7])
>>> y
array([5, 8, 0])
>>> z
array([2, 3, 6])
>>>
>>> w = np.array([x,y,z])
>>> w.dtype=[('x','i4'),('y','i4'),('z','i4')]
>>> w
array([[(9, 4, 7)],
[(5, 8, 0)],
[(2, 3, 6)]],
dtype=[('x', '<i4'), ('y', '<i4'), ('z', '<i4')])
>>> w['x']
array([[9],
[5],
[2]])
>>>
>>> u = np.vstack((x,y,z))
>>> u.dtype=[('x','i4'),('y','i4'),('z','i4')]
>>> u
array([[(9, 4, 7)],
[(5, 8, 0)],
[(2, 3, 6)]],
dtype=[('x', '<i4'), ('y', '<i4'), ('z', '<i4')])
>>> u['x']
array([[9],
[5],
[2]])
>>> v = np.column_stack((x,y,z))
>>> v
array([[(9, 4, 7), (5, 8, 0), (2, 3, 6)]],
dtype=[('x', '<i4'), ('y', '<i4'), ('z', '<i4')])
>>> v.dtype=[('x','i4'),('y','i4'),('z','i4')]
>>> v['x']
array([[9, 5, 2]])
正如您所看到的,虽然我的原始x
数组包含[9,4,7]
,但我无法尝试堆叠数组,然后按'x'
索引返回原始x
数组。有办法做到这一点,还是我错了?
答案 0 :(得分:9)
一种方法是
wtype=np.dtype([('x',x.dtype),('y',y.dtype),('z',z.dtype)])
w=np.empty(len(x),dtype=wtype)
w['x']=x
w['y']=y
w['z']=z
请注意,randint返回的每个数字的大小取决于你的平台,所以在我的机器上有一个int64,而不是int32,即'i4',它是'i8'。另一种方式更便携。
答案 1 :(得分:3)
您想使用np.column_stack
:
import numpy as np
x = np.random.randint(10,size=3)
y = np.random.randint(10,size=3)
z = np.random.randint(10,size=3)
w = np.column_stack((x, y, z))
w = w.ravel().view([('x', x.dtype), ('y', y.dtype), ('z', z.dtype)])
>>> w
array([(5, 1, 8), (8, 4, 9), (4, 2, 6)],
dtype=[('x', '<i4'), ('y', '<i4'), ('z', '<i4')])
>>> x
array([5, 8, 4])
>>> y
array([1, 4, 2])
>>> z
array([8, 9, 6])
>>> w['x']
array([5, 8, 4])
>>> w['y']
array([1, 4, 2])
>>> w['z']
array([8, 9, 6])
答案 2 :(得分:0)
您可能希望查看numpy的记录数组以供此用途:
“Numpy提供了强大的功能来创建结构或记录数组。这些数组允许通过结构或结构字段来操作数据。”
以下是记录数组的文档: http://docs.scipy.org/doc/numpy/user/basics.rec.html
您可以使用变量名作为字段名称。
答案 3 :(得分:0)
要以所选答案为基础,可以使此过程动态化:
设置
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<!-- Nav tabs -->
<ul class="nav nav-tabs">
<li class="nav-item">
<a class="nav-link active" data-toggle="tab" href="#home">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" data-toggle="tab" href="#menu1">Baby computer Man</a>
</li>
<li class="nav-item">
<a class="nav-link" data-toggle="tab" href="#menu2">Menu 2</a>
</li>
</ul>
<!-- Tab panes -->
<div class="tab-content">
<div class="tab-pane container active" id="home">A "Hello, World!" program generally is a computer program that outputs or displays the message "Hello, World!". Such a program is very simple in most programming languages, and is often used to illustrate the basic syntax of a programming language. It is often the first program written by people learning to code.</div>
<div class="tab-pane container fade" id="menu1">The Manchester Baby, also known as the Small-Scale Experimental Machine, was the world's first electronic stored-program computer. It was built at the University of Manchester, UK, by Frederic C. Williams, Tom Kilburn, and Geoff Tootill, and ran its first program on 21 June 1948, seventy-one years ago</div>
<div class="tab-pane container fade" id="menu2">...</div>
</div>
</div>
</body>
解决问题
# First, let's build a structured array
rows = [
("A", 1),
("B", 2),
("C", 3),
]
dtype = [
("letter", str, 1),
("number", int, 1),
]
arr = np.array(rows, dtype=dtype)
# Then, let's create a standalone column, of the same length:
rows = [
1.0,
2.0,
3.0,
]
dtype = [
("float", float, 1)
]
new_col = np.array(rows, dtype=dtype)
希望有帮助
答案 4 :(得分:-1)
使用字典
#!/usr/bin/env python
import numpy
w = {}
for key in ('x', 'y', 'z'):
w[key] = np.random.randint(10, size=3)
print w