- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
看来我迷失在一些可能很愚蠢的事情中。 我有一个 n 维 numpy 数组,我想将它与沿某个维度(可以改变!)的向量(一维数组)相乘。例如,假设我想沿第一个数组的轴 0 将一个二维数组乘以一个一维数组,我可以这样做:
a=np.arange(20).reshape((5,4))
b=np.ones(5)
c=a*b[:,np.newaxis]
很简单,但我想将这个想法扩展到 n 维(对于 a,而 b 始终是 1d)和任何轴。换句话说,我想知道如何在正确的位置生成带有 np.newaxis 的切片。假设 a 是 3d,我想沿 axis=1 相乘,我想生成正确给出的切片:
c=a*b[np.newaxis,:,np.newaxis]
即给定 a 的维数(比如 3)和我要相乘的轴(比如 axis=1),我如何生成和传递切片:
np.newaxis,:,np.newaxis
谢谢。
最佳答案
解决方案代码-
import numpy as np
# Given axis along which elementwise multiplication with broadcasting
# is to be performed
given_axis = 1
# Create an array which would be used to reshape 1D array, b to have
# singleton dimensions except for the given axis where we would put -1
# signifying to use the entire length of elements along that axis
dim_array = np.ones((1,a.ndim),int).ravel()
dim_array[given_axis] = -1
# Reshape b with dim_array and perform elementwise multiplication with
# broadcasting along the singleton dimensions for the final output
b_reshaped = b.reshape(dim_array)
mult_out = a*b_reshaped
步骤演示的示例运行 -
In [149]: import numpy as np
In [150]: a = np.random.randint(0,9,(4,2,3))
In [151]: b = np.random.randint(0,9,(2,1)).ravel()
In [152]: whos
Variable Type Data/Info
-------------------------------
a ndarray 4x2x3: 24 elems, type `int32`, 96 bytes
b ndarray 2: 2 elems, type `int32`, 8 bytes
In [153]: given_axis = 1
现在,我们想沿given axis = 1
执行逐元素乘法。让我们创建 dim_array
:
In [154]: dim_array = np.ones((1,a.ndim),int).ravel()
...: dim_array[given_axis] = -1
...:
In [155]: dim_array
Out[155]: array([ 1, -1, 1])
最后, reshape b
并执行逐元素乘法:
In [156]: b_reshaped = b.reshape(dim_array)
...: mult_out = a*b_reshaped
...:
再次检查whos
信息并特别注意b_reshaped
和mult_out
:
In [157]: whos
Variable Type Data/Info
---------------------------------
a ndarray 4x2x3: 24 elems, type `int32`, 96 bytes
b ndarray 2: 2 elems, type `int32`, 8 bytes
b_reshaped ndarray 1x2x1: 2 elems, type `int32`, 8 bytes
dim_array ndarray 3: 3 elems, type `int32`, 12 bytes
given_axis int 1
mult_out ndarray 4x2x3: 24 elems, type `int32`, 96 bytes
关于python - 沿给定轴将 numpy ndarray 与一维数组相乘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30031828/
在这个例子中,我有一个一维 ndarray 列表,长度为 9,列表有 9 个元素,每个元素有 shape=(2048,) ,所以完全9 * (2048,) , 我得到这些 ndarray来自 mxne
Python/NumPy 中的三种“全部”方法有什么区别?性能差异的原因是什么? ndarray.all() 是否总是三者中最快的? 这是我运行的计时测试: In [59]: a = np.full(
我似乎在 this 中遇到了 XY 问题关于如何判断数组是否共享相同内存的问题。我检查的方式是错误的,我不知道为什么。 举几个例子 test = np.ones((3,3)) test2 = np.a
我在使用 mxnet 进行基本 IO 方面遇到问题。我正在尝试使用 mxnet.io.NDArrayIter 读取内存数据集以在 mxnet 中进行训练。我有下面的代码(为了简洁而精简),它预处理代码
首先,这不是作业问题;而是作业问题。它是与我的工作相关的实际问题的抽象。我真的很感谢所有的意见! 我需要运行类似于下面的计算,按顺序运行数万次,它的计算时间显着影响我的模拟的总持续时间: 在这个抽象中
这个问题在这里已经有了答案: Index multidimensional array with index array (1 个回答) 关闭 2 年前。 我想根据一些坐标从 src ndarray
因此 numpy ndarray 非常方便,因为您只需为任何一维函数 f 和任何 ndarray A 输入 f(A) 即可,它将按元素应用 f。有人告诉我,这也是将函数应用于 ndarray 并避免
我有一个 ndarray 字符串。我想将它转换回 ndarray。我尝试了 newval = np.fromstring(val, dtype=float) 。但它给出了ValueError:字符串大
我正在 python/numpy 中做一些机器学习工作,其中我想用一维 ndarray 索引一个二维 ndarray,这样我就可以得到一个带有索引值的一维数组。 我让它与一些丑陋的代码一起工作,我想知
我想根据某些维度的索引位置数组提取 numpy ndarray 的一部分。让我用一个例子来说明这一点 示例数据 dummy = np.random.rand(5,2,100) X = np.array
这个问题在这里已经有了答案: Find the row indexes of several values in a numpy array (8 个答案) 关闭 2 年前。 我有 a = np.a
我想提取 numpy.ndarray 的第一个轴成为numpy.ndarray的列表. 例如,arr_A包含形状为 (3, 100, 200) 的 numpy ndarray,它将转换为形状为 (10
我一直在尝试用 ndarrays 的 numpy ndarray 转换数组数组。 这是我的数据类型: dt = 'i8,i8,i8,i8,i8,i8,i8,i8,i8,i8,i8,i8,i8,f8,i
我目前正在研究一种可以拆分 numpy.ndarray 的方法进入给定数量的子阵列,只要该数量小于窗口移动的轴。 示例: 给定一个形状为 (15, 40, 3) 的 numpy.ndarray 我想分
我用 arcpy 模块创建了一个 NumPy 结构化数组(称为 arr): arr = arcpy.da.FeatureClassToNumPyArray('MPtest','SHAPE@XYZ',e
是否可以将一个 numpy 数组的特定行的引用存储在另一个 numpy 数组中? 我有一个二维节点数组,例如 nodes = np.array([[1, 2], [2, 3], [3, 4], [4,
我有两个长度相同的 pandas 系列,如下所示: S1 = 0 -0.483415 1 -0.514082 2 -0.515724 3 -0.519375 4
当使用 numpy 时,假设我有一个任意的、以前创建的名为 my_ndarray 的 ndarray。如果可能的话,我希望能够执行以下操作...... my_bytes = my_ndarray.to
我在尝试使用 numpy 打乱多维数组时遇到问题。可以使用以下代码重现该问题: import numpy as np s=(300000, 3000) n=s[0] print ("Allocate"
当您调用 DataFrame.to_numpy() 时,pandas 将找到可以容纳 DataFrame 中所有数据类型的 NumPy 数据类型。但是如何进行反向操作呢? 我有一个“numpy.nda
我是一名优秀的程序员,十分优秀!