簡體   English   中英

如何使用隨機 function 創建一個數組,其總和為 1 在 python 中使用 nympy?

[英]How can I create a array using the random function whose sum is 1 in python using nympy?

處理一項要求我創建一個包含總和為 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.

您可以使用random.random創建大小為(1,m)的矩陣,然后將其除以數組的總和以將總和歸一化為 1。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM