- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在 Python 中,我有一个列表和一个 numpy 数组。
我想将数组乘以列表,这样我得到一个数组,其中第 3 维表示输入数组乘以列表的每个元素。因此:
in_list = [2,4,6]
in_array = np.random.rand(5,5)
result = ...
np.shape(result) ---> (3,5,5)
其中 (0,:,:) 是输入数组乘以列表 (2) 的第一个元素;(1,:,:) 是输入数组乘以列表的第二个元素 (4) 等。
我觉得这个问题会通过广播来回答,但我不确定如何解决这个问题。
最佳答案
你想要np.multiply.outer
. outer
方法是为任何 NumPy“ufunc”定义的,包括乘法。这是一个演示:
In [1]: import numpy as np
In [2]: in_list = [2, 4, 6]
In [3]: in_array = np.random.rand(5, 5)
In [4]: result = np.multiply.outer(in_list, in_array)
In [5]: result.shape
Out[5]: (3, 5, 5)
In [6]: (result[1, :, :] == in_list[1] * in_array).all()
Out[6]: True
正如您所建议的,广播提供了另一种解决方案:如果您将 in_list
转换为长度为 3
的一维 NumPy 数组,则可以重新整形为形状为 的数组code>(3, 1, 1)
,然后与 in_array
的乘法将适本地广播:
In [9]: result2 = np.array(in_list)[:, None, None] * in_array
In [10]: result2.shape
Out[10]: (3, 5, 5)
In [11]: (result2[1, :, :] == in_list[1] * in_array).all()
Out[11]: True
关于python - 如何将 numpy 数组乘以列表以获得多维数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46348464/
我的数据库表是: author book repeat ------ ----
众所周知,我们可以简化 SELECT * FROM A WHERE X=1 OR X=4 OR X=9 如下: SELECT * FROM A WHERE X IN (1,4,9) “多维”条件是否有
我在大学时正在编写 Android 应用程序。 用户被询问必须回答的问题。 我通过服务器的 HTTP 请求/响应获取问题。它们以字符串形式传递。该字符串看起来像这样,有两个问题: (requested
我怎样才能在 C++ 中使用 vector 制作这样的表格: 65 A 66 B 67 C 我是用这样的动态二维数组完成的: int** ary = new int*[2]; for (int
运行时: if (data.custaccount.webaddress) { alert('found it'); } 我得到了错误 data.custaccount is undefined
如何按名称对对象数组进行排序。 数组构造数组的例子: object[] o = new object[items.Count+(row-exists)]; int i = 0;
如何创建二维 vector ?我知道在二维数组中,我可以这样表达: a[0][1]=98; a[0][2]=95; a[0][3]=99; a[0][4]=910; a[1][0]=98; a[1][
这是我的第一个问题。 我有很多组数据。它们中的每一个都应该在 DataFrame 中呈现。我试图通过将 DataFrame 作为多维元组的一项来实现这一点,例如: data[0][1].Glucose
有人可以建议改进我对多维 lstm 的实现吗? 它非常慢并且使用大量内存。 class MultiDimentionalLSTMCell(tf.nn.rnn_cell.RNNCell): """ Ad
我已为我在另一个基于 WPF 的应用程序中使用的 Azure 移动服务添加了到 MVC 站点的连接。 我不太熟悉如何通过移动服务在 MVC 中显示数据,找不到任何很好的示例来展示如何在页面上(例如在网
这样写对吗?有没有更好的写法呢?这与我正在使用的真实数据类似,我想确保将对象或数组正确嵌套在 JSON 文件中。 var data = [ { "department": "I
我有一个HashMap当我调用 .toString() 时,它看起来像这样: {somekey=false, anotherKey=someString, thirdKey={nestedKey=he
是否有更 pythonic 的方式来执行以下操作: import numpy as np def diagonal(A): (x,y,y) = A.shape diags = []
我有以下结构: import java.util.LinkedHashMap; ... LinkedHashMap level0 = new LinkedHashMap(); LinkedHashMa
如何访问 Arraylist 中的整数数组(两者均可调整大小)? 到目前为止我有这个代码: List vertices_passed = new ArrayList(); 我想进入vertices_p
我想知道 MiniZinc 语言中是否可以有(多维)数组的数组。 确实,我想解决 worker 的时间表问题。我的目标是检查它们是否每周至少有 1 天可用。每个 worker 都由一个整数索引,我每周
这个问题已经有答案了: how is axis indexed in numpy's array? (5 个回答) 已关闭 3 年前。 我还没有理解 NumPy 中多维数组中的轴之间的区别。你能给我解
我试图 $.post 包含一些表单数据以及数组中的一些其他数据。我可以将数组插入表单数据的其余部分,但我丢失了数组中的键,并且数据仅表示为字符串,我想我要求的是多维数组? 这是当前的响应字符串: ar
我是 numpy 新手,试图理解 here 中的以下示例。我无法理解的输出 >>> palette[image] 当索引数组 a 是多维时,单个索引数组引用 a 的第一个维度。以下示例通过使用调色板将
这个问题已经有答案了: JavaScript associative array to JSON (5 个回答) JSON.stringify doesn't work with normal Jav
我是一名优秀的程序员,十分优秀!