生成随机数组的方式

  • Post author:
  • Post category:其他


一. 最直接的方式:用numpy.random模块来生成随机数组

1、np.random.rand 用于生成[0.0, 1.0)之间的随机浮点数, 当没有参数时,返回一个随机浮点数,当有一个参数时,返回该参数长度大小的一维随机浮点数数组,参数建议是整数型,因为未来版本的numpy可能不支持非整形参数。

import numpy as np

np.random.rand(10)

array([ 0.89103033, 0.60550521, 0.13856488, 0.57468244, 0.370697 ,

0.31823162, 0.58358377, 0.97177935, 0.76400592, 0.11269547])

2、np.random.randn该函数返回一个样本,具有标准正态分布。

np.random.randn(10)

array([-0.42625455, -1.86248727, 0.96323332, -0.32809754, -0.79697695,

-0.07145189, 2.89728643, 2.32095237, 1.12925633, -0.39210317])

3、np.random.randint(low[, high, size]) 返回随机的整数,位于半开区间 [low, high)。

np.random.randint(10,size=10)

array([4, 1, 4, 3, 8, 2, 8, 5, 8, 9])

4、random_integers(low[, high, size]) 返回随机的整数,位于闭区间 [low, high]。

np.random.random_integers(5)

5、 np.random.shuffle(x) 类似洗牌,打乱顺序;np.random.permutation(x)返回一个随机排列

arr = np.arange(10)

np.random.shuffle(arr)

arr

[1 7 5 2 9 4 3 6 0 8]

np.random.permutation(10)

array([1, 7, 4, 3, 0, 9, 2, 5, 8, 6])

6、random.randint(low, hight) -> 返回一个位于[low,hight]之间的整数

该函数接受两个参数,这两个参数必须是整数(或者小数位是0的浮点数),并且第一个参数必须不大于第二个参数

import random

random.randint(1,10)

random.randint(1.0, 10.0)

7、random.random() -> 不接受参数,返回一个[0.0, 1.0)之间的浮点数

random.random()

0.5885821552646049

8、random.uniform(val1, val2) -> 接受两个数字参数,返回两个数字区间的一个浮点数,不要求val1小于等于val2

random.uniform(1,5.0)

4.485403087612088

random.uniform(9.9, 2)

5.189511116007191

9、random.randrange(start, stop, step) -> 返回以start开始,stop结束,step为步长的列表中的随机整数,同样,三个参数均为整数(或者小数位为0),若start大于stop时 ,setp必须为负数.step不能是0.

random.randrange(1, 100, 2) #返回[1,100]之间的奇数

random.ranrange(100, 1, -2) #返回[100,1]之间的偶数

10、生成随机数组

方法,使用random.ranident,构造一个列表即可:

import random

def random_list(start,stop,length):

if length>=0:

length=int(length)

start, stop = (int(start), int(stop)) if start <= stop else (int(stop), int(start))

random_list = []

for i in range(length):

random_list.append(random.randint(start, stop))

return random_list

作者:琉璃橙子

链接:https://www.jianshu.com/p/f8347a5eeb77

来源:简书

著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

11

range(start, stop[, step])

参数说明:

start: 计数从 start 开始。默认是从 0 开始。例如range(5)等价于range(0, 5);

stop: 计数到 stop 结束,但不包括 stop。例如:range(0, 5) 是[0, 1, 2, 3, 4]没有5

step:步长,默认为1。例如:range(0, 5) 等价于 range(0, 5, 1)

实例

range(10) # 从 0 开始到 10

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

range(1, 11) # 从 1 开始到 11

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

range(0, 30, 5) # 步长为 5

[0, 5, 10, 15, 20, 25]

range(0, 10, 3) # 步长为 3

[0, 3, 6, 9]

range(0, -10, -1) # 负数

[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]

12

np.arange()

函数返回一个有终点和起点的固定步长的排列,如[1,2,3,4,5],起点是1,终点是6,步长为1。

参数个数情况: np.arange()函数分为一个参数,两个参数,三个参数三种情况

1)一个参数时,参数值为终点,起点取默认值0,步长取默认值1。

2)两个参数时,第一个参数为起点,第二个参数为终点,步长取默认值1。

3)三个参数时,第一个参数为起点,第二个参数为终点,第三个参数为步长。其中步长支持小数

#一个参数 默认起点0,步长为1 输出:[0 1 2]

a = np.arange(3)

#两个参数 默认步长为1 输出[3 4 5 6 7 8]

a = np.arange(3,9)

#三个参数 起点为0,终点为3,步长为0.1 输出[ 0. 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1. 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2. 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9]

a = np.arange(0, 3, 0.1)

————————————————

版权声明:本文为CSDN博主「恰个小茶茶」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/qq_41550480/article/details/89390579



版权声明:本文为qq_45798276原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。