【发布时间】:2021-12-24 04:28:47
【问题描述】:
假设有 i 个客户 (i = 1,...,5),他们对好的 D_i 的需求可能是低、中或高。我有以下带有需求数据的 NumPy 矩阵:
import numpy as np
# Client demand
demand = np.array([[150, 160, 170],
[100, 120, 135],
[250, 270, 300],
[300, 325, 350],
[600, 700, 800]])
现在,我想获得所有客户的结果的联合分布。由于每个客户端有 3 个(统计上独立的)事件,总共有 5 个客户端,因此有 3^5 = 243 种可能的组合。获得一个新矩阵的最有效方法是什么,该矩阵给出每个客户 i 和场景 j (j=1,...243) 的所有需求数据?
编辑:
我发现 np.meshgrid 可以满足我的要求,但它似乎只需要表示网格坐标的一维数组,因此无法输入 NumPy 矩阵:
import numpy as np
# Client demand
demand = np.array([[150, 160, 170],
[100, 120, 135],
[250, 270, 300],
[300, 325, 350],
[600, 700, 800]])
# working
scenarios = np.array(np.meshgrid([150, 160, 170],
[100, 120, 135],
[250, 270, 300],
[300, 325, 350],
[600, 700, 800]
)).T.reshape(-1,5)
print(scenarios.shape)
# not working
np.array(np.meshgrid(demand)).T.reshape(-1,5)
【问题讨论】:
-
你试过什么?请向我们展示您的尝试,也许有人可以帮助您提供更有效的解决方案。
标签: python numpy iteration combinations