I have quite a simple scenario where I'd like to test whether both elements of a two-dimensional array are (separately) members of a larger array - for example:
full_array = np.array(['A','B','C','D','E','F'])
sub_arrays = np.array([['A','C','F'],
['B','C','E']])
np.isin(full_array, sub_arrays)
Run Code Online (Sandbox Code Playgroud)
This gives me a single dimension output:
array([ True, True, True, False, True, True])
Run Code Online (Sandbox Code Playgroud)
showing whether elements of full_array are present in either of the two sub-arrays. I'd like instead a two-dimensional array showing the same thing for each of the two elements …
我必须通过广播对 2 个数组求和。这是第一个:
a = [0 1 2 3]
Run Code Online (Sandbox Code Playgroud)
这是第二个:
A = [[ 0 1 2 3 4 5]
[ 6 7 8 9 10 11]
[12 13 14 15 16 17]
[18 19 20 21 22 23]]
Run Code Online (Sandbox Code Playgroud)
这是我到目前为止尝试过的代码:
a = [0 1 2 3]
Run Code Online (Sandbox Code Playgroud)
但是当我运行时,它会抛出以下错误:ValueError: operands could not be broadcast together with shapes (4,) (4,6)
怎么解决呢?
我试图将一个 3D 数组乘以一个 1D 数组,这样沿着第 3 个(深度:d)维度的每个 2D 数组的计算方式如下:
1D_array[d]*2D_array
我最终得到一个看起来像这样的数组,比如:
[[
[1,1]
[1,1]]
[
[2,2]
[2,2]]
[
[3,3]
[3,3]]]
Run Code Online (Sandbox Code Playgroud)
这将是将 np.ones((3,2,2)) 与 [1,2,3] 正确相乘的结果。
我已经尝试了一段时间,无论我似乎做什么,我都无法得到这个结果,只是主题的变化。我该如何正确地执行此操作?
谢谢你的帮助。
python arrays numpy multidimensional-array array-broadcasting
必须有一些“Python化”的方式来做到这一点,但我不认为np.place,np.insert或者np.put是什么我要找的。我想A用较小的 3D 数组中的值替换大型 3D 数组中的值B,从[i,j,k]较大数组中的位置开始。看图:
我想输入类似A[i+, j+, k+] = B或np.embed(B, A, (i,j,k)) 之类的内容,但当然这些都是不对的。
编辑:哦,有这个。所以我应该修改问题以询问这是否是最好的方法(其中“最佳”意味着笔记本电脑上 500x500x50 的浮点数组最快):
s0, s1, s2 = B.shape
A[i:i+s0, j:j+s1, k:k+s2] = B
Run Code Online (Sandbox Code Playgroud)
有什么区别numpy.add(a,b)和a+b添加两个ndarrays A和B是什么时候?文档说这numpy.add是“在阵列广播方面相当于 x1 + x2”。. 但我不明白这意味着什么,因为numpy.add(numpy.array([1,2,3]),4)也有效。
我有一个 3x1 点向量代表某条线的起点,还有一个 3x1 点向量代表某条线的终点。我想沿着这两个点连接的线对任意数量的点进行采样。
np.linspace 正是我所需要的,但在一维。有没有类似的功能可以扩展到3维?
谢谢
我从一个 2D 数组开始,想将它广播到一个 3D 数组(例如,从灰度图像到 rgb 图像)。这是我使用的代码。
>>> img_grey = np.random.randn(4, 4)
>>> img_rgb = np.broadcast_to(np.expand_dims(img_grey, axis=-1), (4, 4, 3))
Run Code Online (Sandbox Code Playgroud)
这将创建一个img_rgb按预期工作的数组:3 个彩色通道(最后一个维度),每个切片都等于原始灰度图像。但是,如果我这样做
>>> img_rgb[0, 0, 0] = 0.
ValueError: assignment destination is read-only
Run Code Online (Sandbox Code Playgroud)
我无法更改 rgb 图像!
数组具有以下维度:
dists: (500,5000)
train: (5000,)
test:(500,)
为什么前两个语句会抛出错误而第三个语句正常?
dists += train + test错误: ValueError: operands could not be broadcast together with shapes (5000,) (500,)
dists += train.reshape(-1,1) + test.reshape(-1,1)错误: ValueError: operands could not be broadcast together with shapes (5000,1) (500,1)
dists += train + test.reshape(-1,1)
这工作正常!为什么会发生这种情况?
我正在 Julia 中开发一个 Web-API 访问包,遵循相当标准的 REST。它有很多方法,例如:
post_foo(cred::ServiceCred, x, y)
get_foo(cred::ServiceCred, x, y)
Run Code Online (Sandbox Code Playgroud)
有时我想一次向 API 发布多个内容(或者从中获取多个内容)。假设我有多个y我想要的post_foo
既然我已经做到了Base.broadcastable(cred::ServiceCred) = Ref(cred),我可以做:
post_foo.(cred, "id123", ["a", "b", "c"])
Run Code Online (Sandbox Code Playgroud)
从界面的角度来看,这很有效,而且非常好。我什至可以这样做:
post_foo.(cred, ["id1", "id2", "id3"], ["a", "b", "c"])
Run Code Online (Sandbox Code Playgroud)
使其post_foo(cred, "id1", "a")依次进行等操作。
但因为这些是网络请求,所以我花了很多时间等待返回响应。如果我使用异步处理,它会快得多,因为它会将它们全部发送出去,并且不会阻塞,直到没有任何内容可发送。然后远程服务器将并行处理它们,并将所有响应返回。相当容易做到asyncmap。
但这不太好:
ansyncmap(["a", "b", "c"]) do y
post_foo(cred, "id123", y)
end
Run Code Online (Sandbox Code Playgroud)
于是我开始思考。BroadcastStyle如果我为自己设置一个自定义,ServiceCred使得所有处理都异步解析,然后不会阻塞直到最后,该怎么办?我认为它甚至可能能够颠覆融合机制以进行更复杂的调用,因此如果事情很好地链接在一起,那么一旦输入到达,所有请求都会被触发(但这可能要求有点高)。
假设我有一个xshape 的numpy 数组[1,5]。我想沿轴 0 扩展它,使得生成的数组y具有形状 [10,5] 并且对于每个 i 都y[i:i+1,:]等于x。
如果x是一个 pytorch 张量我可以简单地做
y = x.expand(10,-1)
Run Code Online (Sandbox Code Playgroud)
但是 numpy 中没有expand,并且那些看起来像它(expand_dims和repeat)的行为似乎不像它。
例子:
y = x.expand(10,-1)
Run Code Online (Sandbox Code Playgroud) numpy ×9
python ×6
arrays ×2
python-3.x ×2
async-await ×1
asynchronous ×1
julia ×1
pytorch ×1
scipy ×1