Goal: To speculate and generate random numbers using numpy library
目标: 使用numpy库推测并生成随机数
Random Number Generation: Random number generation in very important in the field of machine learning. It is used to initialize weights in almost every machine learning algorithm.
随机数生成 :随机数生成在机器学习领域中非常重要。 它几乎在每种机器学习算法中都用于初始化权重。
So few functions used for machine learning algorithms from numpy library:
numpy库中用于机器学习算法的函数很少:
numpy.random.rand()
numpy.random.rand()
It take shape of the array as its argument and then generate random numbers and fill whole the array with the random numbers that lies in between 0 and 1. The distribution of random numbers follows uniform distribution.
它以数组的形状作为参数,然后生成随机数,并用0到1之间的随机数填充整个数组。随机数的分布遵循均匀分布。
numpy.random.randint()
numpy.random.randint()
It takes two arguments(low and high). It generates random integer between low and high in which low is inclusive and high is exclusive. It follows discrete uniform distribution.
它需要两个参数(低和高)。 它在低和高之间生成随机整数,其中低是包含在内的,而高是排斥在外的。 它遵循离散均匀分布。
numpy.random.randn()
numpy.random.randn()
It takes shape of the array as its argument and generate random numbers in the form of gaussian distribution with mean as 0 and variance as 1. It follows standard normal distribution.
它以数组的形状作为参数,并以高斯分布的形式生成随机数,其均值为0,方差为1。它遵循标准正态分布。
numpy.random.random()
numpy.random.random()
It takes size as its argument and generate random number random number lying between 0 and 1. It follows continuous random distribution.
它以大小为参数,并生成介于0和1之间的随机数随机数。它遵循连续随机分布。
numpy.random.multivariate()
numpy.random.multivariate()
It primarily takes three arguments(mean of individual feature in form of matrix,co -variance matrix and last argument is number of datapoints). For generating data for more than one feature, mean and variance matrix must be of higher dimension. It follows multivariate normal distribution.
它主要接受三个参数(矩阵形式,协方差矩阵形式的单个特征的均值,最后一个参数是数据点的数量)。 为了生成多个特征的数据,均值和方差矩阵必须具有较高的维数。 它遵循多元正态分布。
Python implementation:
Python实现:
import numpy as np
print("###########random.rand()############")
A = np.random.rand(2,5)
print(A)
print("###########random.randint()############")
B = np.random.randint(2,17)
print(B)
print("###########random.randn()############")
C = np.random.randn(2,5)
print(C)
print("###########random.random()############")
D = np.random.random((2,5))
print(D)
print("###########random.multivariate_normal()############")
E = np.random.multivariate_normal([1.0,5.0], [[1.0,2.0],[2.0,1.0]],5)
print(E)
Output
输出量
###########random.rand()############
[[0.87736653 0.75351615 0.06455974 0.36414861 0.04139118]
[0.41138255 0.10342316 0.05800631 0.12752116 0.33958441]]
###########random.randint()############
12
###########random.randn()############
[[ 0.01895673 0.50055148 0.12352832 -0.35232071 0.03695278]
[ 2.02632408 0.94237563 0.60807025 -0.37935715 1.45447358]]
###########random.random()############
[[0.57192619 0.85141271 0.49857667 0.62128599 0.39234191]
[0.72266235 0.05779006 0.99732815 0.27651905 0.14774923]]
###########random.multivariate_normal()############
/home/main.py:16: RuntimeWarning: covariance is not positive-semidefinite.
E = np.random.multivariate_normal([1.0,5.0], [[1.0,2.0],[2.0,1.0]],5)
[[ 2.27370346 4.71914942]
[-0.222617 4.50092221]
[-0.38584754 4.88753041]
[ 2.2530275 5.5017934 ]
[-0.13875541 3.25742664]]
翻译自: https://www.includehelp.com/python/generate-random-number-using-numpy-library.aspx
本文介绍了在机器学习中使用numpy库生成随机数的方法,包括numpy.random.rand()、numpy.random.randint()、numpy.random.randn()和numpy.random.random()函数。这些函数分别用于生成在0到1之间均匀分布的随机数、指定范围内的离散随机整数、标准正态分布的随机数以及0到1之间连续分布的随机数。

2700

被折叠的 条评论
为什么被折叠?



