创建具有某些约束的2D Numpy数组
我想创建一个大小n,n
的2维数数组 m (Square Matrix m
是),以及以下约束:
- 每行的总和等于
- 一个元素均在0到1之间。
- 每行元素的 。
例如,对于正方形矩阵 m = np.Array([[[[0.88,0.12],[0.13,0.87]]))
- (奖励)我希望每行的条目都遵循一些高斯类似的分布,这些发行版的峰值,对于行<代码> i ,位于元素
m [i,i]
。
在因此,线程提出了类似的问题。但是,在那里进行回答,我无法找到一种方法。这是一个搜索问题,我确实知道它可能被称为优化问题。但是,我想知道这些约束是否可以满足而无需一些专门的求解器。
I would like to create a 2 dimensional numpy array M
of size n,n
(a square matrix M
that is) with the following constraints:
- The sum of each row equals to one
- The elements of each row are all between 0 and 1
- The value of row
i
that dominates is located at entryM[i,i]
.
For example, for a square matrix it would be something likeM = np.array([[0.88,0.12],[0.13,0.87]])
- (Bonus) Ideally I want the entries of each row to follow some Gaussian like distribution whose peak, for row
i
, is located at elementM[i,i]
.
In this SO thread a similar question is asked. However, playing with the responses there I was not able to find a way to do it. This is a search problem, and I do understand it might be formulated as an optimization problem. However, I am wondering if these constraints can satisfied without the need of some specialized solver.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
1)和2):
For 1) and 2):
我从每个坐标的行数中减去列数,然后使用它们来计算高斯函数的值,最后将其乘以随机数组并归一化。
它可能不是那么随机,但是很有可能满足您的要求:
I subtract the number of columns from the number of rows per coordinate, and then use them to calculate the value of the Gaussian function, and finally multiply it by a random array and normalize it.
It may not be so random, but it has a high probability of meeting your requirements: