【问题标题】:How can I create a array using the random function whose sum is 1 in python using nympy?如何使用 nympy 在 python 中使用总和为 1 的随机函数创建一个数组?
【发布时间】:2022-01-24 18:30:43
【问题描述】:

处理一项要求我创建一个包含总和为 1 的随机数的数组的任务。

实际的问题陈述如下所示:

get_initial_weights

This function should have one parameter, an integer m. This function should return a matrix with 1
row and m columns containing random values, each between zero and one. The sum of these m
values should be equal to one.

【问题讨论】:

    标签: python numpy random


    【解决方案1】:

    您可以使用random.random 创建大小为(1,m) 的矩阵,然后将其除以数组的总和以将总和归一化为1。

    t = np.random.random((1,m))
    t = t / t.sum()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-21
      • 1970-01-01
      • 1970-01-01
      • 2012-12-29
      • 1970-01-01
      相关资源
      最近更新 更多