问题标签 [recarray]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
python - Python numpy recarray:可以使用指针算法获得不同字段的视图吗?
我有以下形式的 numpy 结构化数组:
我现在想在这个数组中生成't'
与'x'
or 或'y'
. 通常的语法会创建一个副本:
这并不意外,因为选择两个字段是“花式索引”,此时 numpy 放弃并制作副本。由于我的实际记录很大,我想不惜一切代价避免复制。
在 numpy 的跨步内存模型中无法访问所需的元素是完全不正确的。查看内存中的各个字节:
可以找出必要的步幅:
显然,v
在没有创建副本的情况下指向内存中的正确位置。不幸的是,numpy 不允许我将这些内存位置重新解释为原始数据类型:
有没有办法欺骗 numpy 进行这种转换,以便创建一个v_view
等效于的数组v_copy
,但没有制作副本?也许直接工作v.__array_interface__
,就像在做的那样np.lib.stride_tricks.as_strided()
?
python - Numpy 均值结构化数组
假设我有一个结构化的学生数组(字符串)和测试分数(整数),其中每个条目是特定学生在特定测试中获得的分数。自然,每个学生在这个数组中都有多个条目。
例子
如何轻松计算每个学生的平均分数?换句话说,我如何在“分数”维度中取数组的平均值?我想做
并让 Numpy 返回
但 Numpy 抱怨
有没有一种 Numpy 式的方法可以轻松做到这一点?我认为这可能涉及查看具有不同 dtype 的结构化数组。任何帮助,将不胜感激。谢谢。
编辑
python - numpy数组中的索引日期时间
我有一个大致如下的numpy数组:
我希望能够根据第一列(即使用日期时间对象)对数据进行索引,这样我就可以访问特定年/月/日的数据,如下所示:
这显然是行不通的。我唯一能想到的就是添加额外的列(例如“年”列),所以这会起作用:
似乎是一种相当低效的做事方式(并且会复制大量数据) - 特别是如果我想对所有其他时间间隔进行索引......有没有更好的方法来做到这一点?
提前致谢。
python - 在 numpy recarray 中找到关闭的元素
我试图从numpy
记录数组no1
中找到最接近于记录数组中的值的所有值no2
(记录数组具有不同数量的值)
可以说no1
有字段:
('electrode', 'i4'), ('no_of_interest_time', 'i4'), ('time', 'f8')
其中time
具体事件的时间和no_of_interest_time
索引事件应单独分析。每个事件都有一个这样的编号,并且多个事件可以共享相同的编号。electrode
保存记录事件的电极的索引(位置)。
no2
具有相同的字段,但拥有不同的事件。
对于 recarray 中的每个事件no2
,我想从no1
相同类型(no_of_interest_time
)和位置(electrode
)的 recarray 中找到最近的事件。
我可以使用 for 循环解决它的方式如下所示,但我正在寻找更优雅的解决方案:
我将不胜感激任何建议。提前致谢!!
python - 将层次结构添加到 numpy 结构化数组
我想采用具有多个命名字段的现有数组,并创建一个新数组(或将其更改到位),其中一个字段的分层 dtype 等于原始 dtype。那是,
这样newarray['old']
在形状和结构上与oldarray
这是一个例子:
但这给了我所有的零:
如果我尝试制作副本,我也会遇到同样的问题:
另外:有人知道为什么会这样吗?
python - 两个recarrays的numpy.concatenate导致转置形状?
假设我有两个具有相同 dtype 但形状不同的 numpy 数组 x1 和 x2:x1.dtype = dtype([('fmv', '<f4'), ('delta', '<f4'), ('rho', '<f4', (5,))])
x2.dtype = dtype([('fmv', '<f4'), ('delta', '<f4'), ('rho', '<f4', (5,))])
x1.shape = (10L, 1L)
x2.shape = (10L, 6L)
我想在轴 1 上连接这两个数组:
y = np.concatenate((x1,x2), axis=1)
这会导致:
y.shape = (10L, 7L)
好
y['rho'].shape = (5L, 10L, 7L)
坏
为什么 rho 字段的形状被转置了?我期待 (10, 7, 5)
更新:
x1.strides = (28L, 28L)
x2.strides = (168L, 28L)
y.strides = (28L, 280L)
我制作了一个小脚本,您可以运行它来复制结果:
import numpy as np
x = np.zeros((5,3), dtype=np.dtype([('field1','<f8'),('field2','<f8',4)]))
A1 = np.concatenate((np.array(x[0,0], ndmin=1), np.ravel(x[:,1:], order='C')), axis=0)
B1 = np.concatenate((np.tile(np.array(A1[0],ndmin=2), (5,1)), np.reshape(A1[1:], (5,2), order='C')), axis=1)
A2 = np.ravel(x, order='C')
B2 = np.reshape(A2, (5,3), order='C')
B1['field2'].shape = (4L, 5L, 3L)
坏
B2['field2'].shape = (5L, 3L, 4L)
好
python - 如何 hstack numpy 记录的数组?
[这篇文章的早期版本的标题不准确“如何将一列添加到 numpy 记录数组中?” 早期标题中提出的问题已经部分回答,但这个答案并不是这篇文章早期版本的正文所要求的。我已经改写了标题,并对帖子进行了大量编辑,以使区别更加清晰。我还解释了为什么我前面提到的答案不符合我的要求。]
假设我有两个numpy
数组x
和y
,每个数组由r个“记录”(又名“结构化”)数组组成。设x
be ( r , c x ) 的形状和y
be ( r , c y ) 的形状。我们还假设x.dtype.names
和之间没有重叠y.dtype.names
。
例如,对于r = 2、c x = 2 和c y = 1:
我想“水平”连接x
并y
产生一个新的记录数组z
,具有形状(r,c x + c y)。此操作不应该修改x
或根本不应该修改y
。
一般来说,z = np.hstack((x, y))
不会这样做,因为dtype
' 在x
并且y
不一定匹配。例如,继续上面的例子:
现在,有一个函数,numpy.lib.recfunctions.append_fields
看起来它可能会做一些接近我正在寻找的事情,但我无法从中得到任何东西:我尝试过的所有事情要么因错误而失败,要么产生不同于我想要得到的东西。
Can someone please show me explicitly the code (using n.l.r.append_fields
or otherwise1) that would generate, from the x
and y
defined in the example above, a new array of records, z
, equivalent to the horizontal concatenation of x
and y
, and do so without modifying either x
or y
?
I assume that this will require only one or two lines of code. Of course, I am looking for code that does not require building z
, record by record, by iterating over x
and y
. Also, the code may assume that x
and y
have the same number of records, and that there is no overlap between x.dtype.names
and y.dtype.names
. Other than this, the code I'm looking for should know nothing about x
and y
. Ideally, it should be agnostic also about the number of arrays to join. IOW, leaving out error checking, the code I'm looking for could be the body of a function hstack_rec
so that the new array z
would be the result hstack_rec((x, y))
.
1...although I have to admit that, after my so-far perfect record of failure with numpy.lib.recfunctions.append_fields
, I've become a bit curious about how this function could be used at all, irrespective of its relevance to this post's question.
python - Python Numpy recarray 双向排序
我有一个结构化的 numpy 数组,我按顺序对其进行排序。它工作得很好,但只有一个方向!
降序:
和
上升:
my_order 的顺序类似于[col1,col2,-col3,col4,-col5,...,colN]
,对于某些列,我想像 col1,col2 和 colN 一样将其按升序排序,而对于其他列,则按 col3 和 col5 (minus signal) 降序排序。在这个例子中,我想先按 col1 升序对数组进行排序,然后按 col2 升序,然后按 col3 降序,然后按 col4 升序,然后按 col5 降序,依此类推……我该怎么做?
谢谢
python - 更改对象类型的 recarray 列的 dtype
我有一个 csv 文件,其中两列(v3 和 v7)对于所有观察都是空白的:
我正在使用 csv2rec 函数将其读入 python (epd-7.0-2):
当我尝试增加第三或第七行中的值时,我收到一个错误(其他列不会发生):
RuntimeError:无法在对象数组上调用 setfield
所以我尝试更改类型,只是遇到其他错误:
另一个问题是我也无法更改数组中的其他列:
想法?谢谢,
python - numpy 记录数组与时间?
我正在尝试将XML
文件读入NumPy
记录数组。时间是祖鲁时间,u'2013-06-06T17:47:38Z'
,其他列是floats
。
时间和时间floats
都可以转换为NumPy
数组。但是,如果我尝试制作 a recordarray
,它会以多种方式失败(这可能表明我不知道如何创建记录数组):
创建recordarray
包含时间的正确方法是什么?
(keys
是一个列表,datadict
是dtype
字典)