简体   繁体   English

如何使用随机 function 创建一个数组,其总和为 1 在 python 中使用 nympy?

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

Working on an assignment which requires me to create an array with random numbers whose sum is 1.处理一项要求我创建一个包含总和为 1 的随机数的数组的任务。

The actual problem statement looks something like this:实际的问题陈述如下所示:

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.

You can use random.random to create your matrix of size (1,m) , and then divide it by the sum of the array to normalize the sum to 1.您可以使用random.random创建大小为(1,m)的矩阵,然后将其除以数组的总和以将总和归一化为 1。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何创建一个以元组为参数的 function,然后在不使用 sum() 的情况下找到元组中整数的总和? - How can I create a function with a tuple as the parameter and then find the sum of the integers in the tuple without using sum()? 我如何在Python中使用随机数生成随机数? - How can I generate random numbers in python using random? 如何使用for循环在python中创建数组? - How can I create an array in python using a for loop? 如何创建一个总和为x的随机整数向量列表 - How to create a list of random integer vector whose sum is x 我如何在python中创建一个数组,其元素为2个数组 - How can I make an array in python whose elements are 2 array 随机 numpy 数组,其值介于 -1 和 1 之间且总和为 1 - random numpy array whose values are between -1 and 1 and sum to 1 如何使用两个随机变量列表在python中创建点列表(二维数组) - How can I create a list of points (two dimensional arrays) in python using two lists of random variables 如何找到两个总和等于Python中目标总和的索引? - How can I find the indices of any two of the numbers, whose sum is equal to the target sum in Python? 在python中使用turtle函数时,如何生成随机点颜色? - When using the turtle function in python, how can I generate a random dot color? 如何使用 Python 函数对嵌套列表中的每个列表求和? - How can I sum each list within a nested list using a Python function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM