问题标签 [numpy-slicing]
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.
arrays - 如何使用列表元素的非整数步长创建网格网格?
我有 2 个独立生成的 x 和 y 坐标列表,a/h
点数介于0
和之间a
。
当a/h
是这样的,以整数为单位0
增加,即。这很好,因为列表中的元素数量可以用作索引。因此,我通常可以创建一个网格网格,以便第三个列表可以与之关联。a
[0,1,2,..,a]
V1
wherepotential(V1)
现在V1
对应于 meshgrid [x, y]
。但是,我正在做一项任务,我需要调查步长如何影响我的问题。结果,如果我有一个非整数的步长,0
即a
现在[0, 0.5, 1,...,a]
我不能做我上面所做的,因为索引现在是非整数。提出错误
我该如何解决这个问题,以便我不依赖元素本身的值作为元素的索引,这样如果列表0.25
之间存在步长0
,例如a
X
这样我就可以拥有
?
numpy-slicing - 切片一个 Numpy 数组给出错误的结果
我有一个 numpy 数组,我试图沿前两列和前两行切片,然后将它们设置为 0。
在我的代码中,您可以看到我尝试这样做的尝试。在将正确数量的数字设置为 0 方面,我遇到了无穷无尽的麻烦。
任何关于它为什么不能正确显示的建议或见解将不胜感激。
python - 计算并返回第二大数和第二小数之间的差
给定一个 Python 列表,计算并返回第二大数和第二小数之间的差。假设列表包含两个或更多元素。功能说明:第二大/最小必须不同于列表中的最大/最小数字。那是:
应该将列表作为输入。
python - 如何使用算法从矩阵中选择列
我正在用 python 编写一个用户定义的函数来有效地从矩阵中提取特定的列块。
我的矩阵是 48 x 16240。数据以某种模式按列组织。
我的目标是从中制作 4 个矩阵。通过选择前 70 列提取第一个矩阵,跳过下一个 210,选择下一个 70,跳过下一个 210,直到矩阵结束。
通过选择第二个 70 列提取第二个矩阵,跳过下一个 210,选择下一个 70,跳过下一个 210,直到矩阵结束。
以与上述相同的方式,通过分别选择第三和第四70列来提取第三和第四矩阵。
可以看出,16240 可以被 70 整除。
有没有办法有效地完成这项工作?
python-3.x - 如何为每一行广播不同的切片?
我正在对一个 numpy 二维数组进行操作,并试图找到某种方法来访问每行的不同切片。最好以这样的方式利用 numpy 广播,即我传递一个切片数组,如下所示:
我知道我上面写的是假的,但你明白了。我想传入一个数组 t = [2,3,4] 以便该数组的每个成员(由 t_i 表示)导致 t_i: (该成员和后面的冒号)。
谢谢
python - 为什么使用数组作为索引会改变多维 ndarray 的形状?
我有一个 4-D NumPy 数组,轴为 x,y,z,t。我想取对应于 t=0 的切片并在 y 轴上排列顺序。
我有以下
我得到 (5, 4, 3) 而不是 (4,5,3)。
相反,当我进入
我得到了预期的(4,5,3)。有人可以解释为什么第一个版本交换前两个维度吗?
pandas - 逻辑操作:从数据框中的列中选择两个值
我有一个数据框如下,
我想从 df2 中选择“a”列等于“二”或“三”的数据,我的代码如下,
谁能告诉我为什么我的代码不起作用?
错误:Series 的真值不明确。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()。
提前致谢!
python - Numpy 切片多维错误产生意外结果
我有一个这种形状的多维数组:
我想使用以下命令从中提取一组特定的值:
所以基本上我需要在第二个维度上迭代每个值来修复第一、第三、第四和第五个值。最后一个维度在循环中。
但是,我的切片和想要在第二维上提取的方式似乎无法正常工作。经过一番调查,我发现形状并没有像预期的那样改变。看来我没有正确理解它:
我发现一些关于多维切片的文章,但没有一篇文章解释了这种行为,或者我误解了它们。
如果有人可以解释为什么数组的形状会如图所示而不是预期的那样发生变化,(200, 3, 3, 114)
以及正确的方法是什么,我将不胜感激!最后我试图得到一个形状数组(200, )
python - 使用索引一维数组切片二维数组
我有一个 (10,24) 的二维数组和一个 (10,) 形状的一维数组。我想使用一维数组对二维数组进行切片,这样我的结果数组将是 (10,24) 但值是从一维数组中的索引开始切片的。
我希望我的结果数组是:
python - Search slice of pandas columns for value occurrence, get indices/mask of occurrences, then use that to index into a second slice of columns
I have a large dataframe (500K rows x 100 cols) and want to do the following search-and-masking operation efficiently, but I can't find the right pandas/numpy incantation; better still if it can be vectorized:
- on each row, the N columns
m1,m2,...,m6
can contain distinct values from 1..9, or else trailing NaNs. (The NaNs are there for a very good reason, to prevent aggregation/taking sum/mean/etc. on nonexistent records when we process the output from this step; it is very strongly desirable that you preserve the NaNs)- distinctness: it is guaranteed that the columns
m<i>
will contain at most one occurrence of each of the values 1..9
- distinctness: it is guaranteed that the columns
- columns
x1,x2,...,x6
are associated with the columnsm<i>
, and contain some integer values - For each possible value
v
in range 1..9 (I will manually sweep v from 1:9 at top-level of my analysis, don't worry about that part), I want to do the following:- on each row where that value
v
occurs in one of them<i>
, find which columnm<i>
equalsv
(either as boolean mask/array/indices/anything else you prefer) - on rows where
v
doesn't occur inm<i>
, preferably I don't want any result for that row, not even NaN - then I want to use that intermediate boolean mask/array/indices/whatever to slice the corresponding value from the
x<i>
(x1,x2,...,x6
) on that row
- on each row where that value
Here's my current code; I tried iloc
, melt
, stack/unstack
, mask
, np.where
, np.select
and other things but can't get the desired result: