python - 在 Python 中使用网格搜索创建维度为 n * 3 的矩阵
问题描述
我想要一个像下面这样的矩阵,3列,n行。每行总和为一。
[[0, 0, 1], [0, 0.1, 0.9], [0.1, 0.1, 0.8], [0.1, 0.2, 0.7] ...]
有这样做的图书馆吗?
解决方案
您可以使用itertools.combinations_with_replacement
从 0.0 到 1.0 之间的 11 个插槽中选择 2 个分区:
from itertools import combinations_with_replacement
[[n / 10 for n in (a, b - a, 10 - b)] for a, b in combinations_with_replacement(range(11), 2)]
这将返回:
[[0.0, 0.0, 1.0],
[0.0, 0.1, 0.9],
[0.0, 0.2, 0.8],
[0.0, 0.3, 0.7],
[0.0, 0.4, 0.6],
[0.0, 0.5, 0.5],
[0.0, 0.6, 0.4],
[0.0, 0.7, 0.3],
[0.0, 0.8, 0.2],
[0.0, 0.9, 0.1],
[0.0, 1.0, 0.0],
[0.1, 0.0, 0.9],
[0.1, 0.1, 0.8],
[0.1, 0.2, 0.7],
[0.1, 0.3, 0.6],
[0.1, 0.4, 0.5],
[0.1, 0.5, 0.4],
[0.1, 0.6, 0.3],
[0.1, 0.7, 0.2],
[0.1, 0.8, 0.1],
[0.1, 0.9, 0.0],
[0.2, 0.0, 0.8],
[0.2, 0.1, 0.7],
[0.2, 0.2, 0.6],
[0.2, 0.3, 0.5],
[0.2, 0.4, 0.4],
[0.2, 0.5, 0.3],
[0.2, 0.6, 0.2],
[0.2, 0.7, 0.1],
[0.2, 0.8, 0.0],
[0.3, 0.0, 0.7],
[0.3, 0.1, 0.6],
[0.3, 0.2, 0.5],
[0.3, 0.3, 0.4],
[0.3, 0.4, 0.3],
[0.3, 0.5, 0.2],
[0.3, 0.6, 0.1],
[0.3, 0.7, 0.0],
[0.4, 0.0, 0.6],
[0.4, 0.1, 0.5],
[0.4, 0.2, 0.4],
[0.4, 0.3, 0.3],
[0.4, 0.4, 0.2],
[0.4, 0.5, 0.1],
[0.4, 0.6, 0.0],
[0.5, 0.0, 0.5],
[0.5, 0.1, 0.4],
[0.5, 0.2, 0.3],
[0.5, 0.3, 0.2],
[0.5, 0.4, 0.1],
[0.5, 0.5, 0.0],
[0.6, 0.0, 0.4],
[0.6, 0.1, 0.3],
[0.6, 0.2, 0.2],
[0.6, 0.3, 0.1],
[0.6, 0.4, 0.0],
[0.7, 0.0, 0.3],
[0.7, 0.1, 0.2],
[0.7, 0.2, 0.1],
[0.7, 0.3, 0.0],
[0.8, 0.0, 0.2],
[0.8, 0.1, 0.1],
[0.8, 0.2, 0.0],
[0.9, 0.0, 0.1],
[0.9, 0.1, 0.0],
[1.0, 0.0, 0.0]]
推荐阅读
- javascript - 如何修改上传功能的前端代码
- flutter - 没有 Firebase 项目的 Flutter Admob
- javascript - 为什么使用 JavaScript 的 find() 方法返回未定义?
- c# - 从同一项目中的另一个 XAML 窗口访问 CoreWebView2 时为空
- objective-c - 同步运行 NSTask
- c# - C#将数据对象添加到现有项目中的所有服务
- reactjs - 如何从对象数组中搜索?并通过单击列名按升序和降序对数据进行排序?
- reactjs - 如何将css变量添加到createMuiTheme
- javascript - 如何使用 requests 和 Beautiful Soup 来抓取使用 javascript 的网站?
- python - AttributeError:模块'QuantLib._QuantLib'在python中没有属性'delete_SwigPyIterator'