标签: structured-array

为什么仅在分配给变量时才创建此memoryview会引发ValueError?

Pythons memoryview支持datetime64timedelta.好.但是,当我尝试创建一个memoryview包含一个datetime64或的结构化数组时timedelta,它似乎可以工作...... 除非我将它分配给一个变量!

In [19]: memoryview(zeros(10, dtype=[("A", "m8[s]")]))
Out[19]: <memory at 0x7f1d455d6048>

In [20]: x = memoryview(zeros(10, dtype=[("A", "m8[s]")]))
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
ValueError: cannot include dtype 'm' in a buffer

In [21]: x = _19

In [22]: x
Out[22]: <memory at 0x7f1d455d6048>
Run Code Online (Sandbox Code Playgroud)

这严重挑战了我对Python基本工作方式的理解.怎么能f()x = f()不同,考虑到(1)IPythons REPL分配输出到_19反正,和(2)的函数/类memoryview没有知道什么是来电者会用它的输出做的呢?

我正在运行Python 3.4.1上的代码,numpy 1.10.0.dev + fbcc24f,在Linux 2.6.32-431.23.3.el6.x86_64,Scientific Linux发行版6.6上.


编辑

在Python …

python numpy read-eval-print-loop memoryview structured-array

19
推荐指数
1
解决办法
653
查看次数

Numpy中没有结构化数组的二元运算符?

好的,所以在浏览了numpy结构化数组的教程后,我能够创建一些简单的例子:

from numpy import array, ones
names=['scalar', '1d-array', '2d-array']
formats=['float64', '(3,)float64', '(2,2)float64']
my_dtype = dict(names=names, formats=formats)
struct_array1 = ones(1, dtype=my_dtype)
struct_array2 = array([(42., [0., 1., 2.], [[5., 6.],[4., 3.]])], dtype=my_dtype)
Run Code Online (Sandbox Code Playgroud)

(我的预期用例将有三个以上的条目,并将使用非常长的1d阵列.)因此,一切顺利,直到我们尝试执行一些基本的数学运算.我得到以下所有错误:

struct_array1 + struct_array2
struct_array1 * struct_array2
1.0 + struct_array1
2.0 * struct_array2
Run Code Online (Sandbox Code Playgroud)

显然,即使是最简单的结构化数组也不支持简单的运算符(+, - ,*,/).或者我错过了什么?我是否应该看一些其他的包裹(并且不要说熊猫,因为它完全过度杀戮)?这似乎是一个显而易见的能力,所以我有点傻眼.但是很难在网上找到任何有关此问题的喋喋不休.这不会严重限制结构化数组的有用性吗?为什么有人会使用结构数组而不是数组打包到dict中?是否存在技术上的原因导致这种情况难以解决?或者,如果正确的解决方案是执行重载的艰巨工作,那么如何在保持操作快速的同时完成?

python numpy binary-operators structured-array

8
推荐指数
1
解决办法
530
查看次数

numpy:如何一次填充结构化数组中的多个字段

非常简单的问题:我有一个带有多个列的结构化数组,我只想用其他预先存在的数组填充其中一些(但不止一个).

这就是我正在尝试的:

strc = np.zeros(4, dtype=[('x', int), ('y', int), ('z', int)])
x = np.array([2, 3])
strc[['x', 'y']][0] = x
Run Code Online (Sandbox Code Playgroud)

这给了我这个未来的警告:

main:1:FutureWarning:Numpy检测到您(可能)正在写入numpy.diagonal返回的数组或者选择记录数组中的多个字段.这段代码可能会在未来的numpy版本中破解 - 有关详细信息,请参阅numpy.diagonal或arrays.indexing参考文档.快速解决方法是制作一个明确的副本(例如,执行arr.diagonal().copy()或arr [['f0','f1']].copy()).

但即使这是一个警告,结构化数组也不会被填充.到目前为止,我正在迭代两个数组并且它可以工作,但我猜这是非常低效的.有没有更好的办法?

python numpy structured-array

7
推荐指数
1
解决办法
1427
查看次数

从列表创建结构化数组

我有一个简单的元素列表,我正试图从中制作一个结构化数组.

这种天真的方法失败了:

y = np.array([1,2,3], dtype=[('y', float)])
TypeError: expected an object with a buffer interface
Run Code Online (Sandbox Code Playgroud)

将每个元素放在一个元组中:

# Manuel way
y = np.array([(1,), (2,), (3,)], dtype=[('y', float)])
# Comprehension
y = np.array([tuple((x,)) for x in [1,2,3]], dtype=[('y', float)])
Run Code Online (Sandbox Code Playgroud)

如果我先从列表中创建一个数组,它也可以工作:

y = np.array(np.array([1,2,3]), dtype=[('y', float)])
Run Code Online (Sandbox Code Playgroud)

我有点不解.numpy如果提供一个简单的列表,后者如何工作却无法解决问题呢?

推荐的方式是什么?创建该中间体array可能不会产生很大的性能影响,但这不是次优的吗?

我也很惊讶那些不起作用:

# All lists
y = np.array([[1,], [2,], [3,]], dtype=[('y', float)])
TypeError: expected an object with a buffer interface
# All tuples
y = np.array(((1,), (2,), (3,)), dtype=[('y', float)]) …
Run Code Online (Sandbox Code Playgroud)

python numpy structured-array

7
推荐指数
1
解决办法
1138
查看次数

结构化2D Numpy Array:设置列名和行名

我正在尝试找到一种很好的方法来获取一个2d numpy数组并将列和行名称作为结构化数组附加.例如:

import numpy as np

column_names = ['a', 'b', 'c']
row_names    = ['1', '2', '3']

matrix = np.reshape((1, 2, 3, 4, 5, 6, 7, 8, 9), (3, 3))

# TODO: insert magic here

matrix['3']['a']  # 7
Run Code Online (Sandbox Code Playgroud)

我已经能够使用像这样设置列:

matrix.dtype = [(n, matrix.dtype) for n in column_names]
Run Code Online (Sandbox Code Playgroud)

这让我做,matrix[2]['a']但现在我想重命名行,所以我可以做matrix['3']['a'].

python arrays numpy structured-array

7
推荐指数
1
解决办法
9225
查看次数

如何在Numpy中屏蔽记录数组的元素?

我理解如何创建一个掩码数组,我想在记录数组中使用掩码,以便我可以使用命名属性访问这些数据.当我从一个蒙版数组创建一个记录数组时,掩码似乎"丢失"了:

>>> data = np.ma.array(np.ma.zeros(30, dtype=[('date', '|O4'), ('price', '<f8')]),mask=[i<10 for i in range(30)])
>>> data
masked_array(data = [(--, --) (--, --) (--, --) (--, --) (--, --) (--, --) (--, --) (--, --)
(--, --) (--, --) (0, 0.0) (0, 0.0) (0, 0.0) (0, 0.0) (0, 0.0) (0, 0.0) (0, 0.0) (0, 0.0) (0, 0.0) (0, 0.0) (0, 0.0) (0, 0.0) (0, 0.0) (0, 0.0)
(0, 0.0) (0, 0.0) (0, 0.0) (0, 0.0) (0, 0.0) (0, 0.0)],
         mask = [(True, …
Run Code Online (Sandbox Code Playgroud)

python numpy structured-array masked-array

6
推荐指数
1
解决办法
1815
查看次数

更改 numpy 结构化数组数据类型名称和格式

我正在用 numpy 中的结构化数组做一些工作(我最终将转换为 Pandas 数据帧)。

现在,我通过读取一些数据(实际上是对一些数据进行内存映射)然后通过用户指定的约束对其进行过滤来生成这个结构化数组。然后,我想将这些数据从我读入的形式(所有内容都是 int 以节省我从中读取的文件中的空间)转换为更有用的格式,以便我可以进行一些单位转换(即向上转换为一个浮点数)。

在改变结构化数据类型的过程中,我注意到了一个有趣的工件(或其他东西)。假设读取数据会产生与以下创建的相同的结构化数组(请注意,在实际代码中,dtype 更长且更复杂,但这对于 mwe 来说就足够了):

import numpy as np

names = ['foo', 'bar']
formats = ['i4', 'i4']

dtype = np.dtype({'names': names, 'formats': formats})

data = np.array([(1, 2), (3, 4)], dtype=dtype)
print(data)
print(data.dtype)
Run Code Online (Sandbox Code Playgroud)

这造成

[(1, 2) (3, 4)]
[('foo', '<i4'), ('bar', '<i4')]
Run Code Online (Sandbox Code Playgroud)

作为结构化数组

现在,假设我想将这两个 dtype 上转换为 double,同时重命名第二个组件。这似乎应该很容易

names[1] = 'baz'

formats[0] = np.float
formats[1] = np.float

dtype_new = np.dtype({'names': names, 'formats': formats})

data2 = data.copy().astype(dtype_new)

print(data2)
print(data2.dtype)
Run Code Online (Sandbox Code Playgroud)

但结果出乎意料

(1.0, 0.0) (3.0, 0.0)]
[('foo', '<f8'), …
Run Code Online (Sandbox Code Playgroud)

python numpy python-3.x structured-array

5
推荐指数
1
解决办法
4289
查看次数

将作为矩阵的numpy数组字段值拆分为列向量

我有以下numpy结构化数组:

x = np.array([(22, 2, -1000000000.0, [1000,2000.0]), (22, 2, 400.0, [1000,2000.0])],
dtype=[('f1', '<i4'), ('f2', '<i4'), ('f3', '<f4'), ('f4', '<f4',2)])
Run Code Online (Sandbox Code Playgroud)

如您所见,字段'f4'是一个矩阵:

In [63]: x['f4']
Out[63]: 
array([[ 1000.,  2000.],
       [ 1000.,  2000.]], dtype=float32)
Run Code Online (Sandbox Code Playgroud)

我的最终目标是拥有一个只有矢量的numpy结构化数组.我想知道如何将'f4'分成两个字段('f41'和'f42'),其中每个字段代表矩阵的列.

In [67]: x
Out[67]: 
array([(22, 2, -1000000000.0, 1000.0, 2000.0),
       (22, 2, 400.0, 1000.0, 2000.0)], 
      dtype=[('f1', '<i4'), ('f2', '<i4'), ('f3', '<f4'), ('f41', '<f4'), ('f42', '<f4')])
Run Code Online (Sandbox Code Playgroud)

此外,我想知道是否有可能在使用修改数组的操作或最小化原始数据的复制时实现这一点.

python numpy structured-array

5
推荐指数
1
解决办法
74
查看次数

使用 np.savetxt 和 header 保存结构化 numpy 数组

我有一个以下形式的结构数组

output = np.zeros(names.size, dtype=[('name', 'U32'), ('r', float),('m',float)])
Run Code Online (Sandbox Code Playgroud)

然后我尝试使用 np.savetxt 将其保存到 csv 文件中。我想知道是否有办法也可以将每列的标签保存为 csv 文件的标题?

先感谢您。

python numpy save structured-array

5
推荐指数
1
解决办法
7531
查看次数

Numpy,将列添加到现有结构化数组

我有一个起始数组:

[(1, [-112.01268501699997, 40.64249414272372])
 (2, [-111.86145708699996, 40.4945008710162])]
Run Code Online (Sandbox Code Playgroud)

其中第一列是int,第二列是带有浮点数的元组.我需要添加一个名为USNG的字符串列.

然后我创建一个结构化的numpy数组:

dtype = numpy.dtype([('USNG', '|S100')])
x = numpy.empty(array.shape, dtype=dtype)
Run Code Online (Sandbox Code Playgroud)

我想将x numpy数组附加到现有数组以添加新列,以便我可以为每一行输出一些信息.当我执行以下操作时:

numpy.append(array, x, axis=1)# I've also tried vstack and hstack
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

'TypeError: invalid type promotion'
Run Code Online (Sandbox Code Playgroud)

有关为什么会发生这种情况的任何建议?

谢谢

python numpy python-2.7 structured-array

4
推荐指数
2
解决办法
5103
查看次数

切片一个 numpy 结构化一维​​数组以获取记录的一部分

我有一个 numpy 一维结构化数组,我只想获取一条记录的一部分。我试图弄清楚如何分割这种类型的请求。这是我的代码:

summary_stat_list = ['mean', 'variance', 'median', 'kurtosis', 'skewness']
model_summary_stats = np.zeros(5,dtype=[('statistic',
                                                       'object'),
                                           ('f1', 'float'),
                                           ('f2', 'float'),
                                           ('f3', 'float'),
                                           ('m1', 'float'),
                                           ('m2', 'float'),
                                           ('m3', 'float'),
                                           ('t3', 'float'),
                                           ('t2', 'float'),
                                           ('t1', 'float'),
                                           ('prom1', 'float'),
                                           ('prom2', 'float')])
for r in range(model_summary_stats.shape[0]):
    model_summary_stats['statistic'][r] = summary_stat_list[r]
Run Code Online (Sandbox Code Playgroud)

现在,该数组如下所示:

[('mean', 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0)
('variance', 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0)
('median', 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0) …
Run Code Online (Sandbox Code Playgroud)

python arrays numpy structured-array

3
推荐指数
1
解决办法
1324
查看次数

将 numpy 数组转换为结构化数组

假设我有以下数组:

arr = np.array([[1,2], [3,4]], dtype='u1')
Run Code Online (Sandbox Code Playgroud)

我想把它转换成这样的结构化数组:

strarr = np.array([(1,2), (3,4)], dtype=[('a', 'u1'), ('b', 'u1')])
Run Code Online (Sandbox Code Playgroud)

如果我只是尝试

arr.astype([('a', 'u1'), ('b', 'u1')])
Run Code Online (Sandbox Code Playgroud)

它返回

>>> array([[(1, 1), (2, 2)],
       [(3, 3), (4, 4)]], dtype=[('a', 'u1'), ('b', 'u1')])
Run Code Online (Sandbox Code Playgroud)

如何转换数组以便它使用一行的所有元素来填充字段(前提是数字匹配)而不是复制每个元素?

python arrays numpy python-3.x structured-array

3
推荐指数
1
解决办法
220
查看次数