随机创建数组

  • 语法格式
numpy.random.random(size=None)
  • numpy.random.randint()
该方法有三个参数 low、high、size 三个参数。默认 high 是 None,如果只有 low,那范围就是[0,low)。如果有 high,范围就是[low,high)。
  • 代码
 1 #导入模块
 2 import numpy as np 
 3 
 4 def randomTest():
 5     #使用random创建一维数组
 6     a = np.random.random(size = 5)
 7     print(a)
 8     print(type(a))
 9     print("************************************************")
10 
11     #创建二维数组
12     b = np.random.random(size = (3,4))
13     print(b)
14     print("************************************************")
15 
16     #创建三维数组
17     c = np.random.random(size = (2,3,4))
18     print(c)
19     print("************************************************")
20 
21 #随机整数
22 def randomintTest():
23     #生成0-5之间的随机整数(一维)
24     a = np.random.randint(6,size=10)
25     print(a)
26     print(type(a))
27     print("************************************************")
28 
29     #生成5-10之间的随机整数
30     b = np.random.randint(5,11,size=(4,3))
31     print(b)
32     print("************************************************")
33 
34     #生成5-10之间的随机数(三维)
35     c = np.random.randint(5,11,size=(2,4,3))
36     print(c)
37     print("************************************************")
38 
39     #dtype的使用
40     d = np.random.randint(10,size=5)
41     print("默认的dtype:",d.dtype)
42     print("************************************************")
43     e = np.random.randint(10,size=5,dtype=np.int64)
44     print("默认的dtype:",e.dtype)
45     print("************************************************")
46 
47 #创建标准的正态分布
48 def randnTest():
49     a = np.random.randn(4)
50     print(a)
51     print("************************************************")
52 
53     #创建二维的
54     b = np.random.randn(2,3)
55     print(b)
56     print("************************************************")
57 
58     #创建三维
59     c = np.random.randn(2,3,4)
60     print(c)
61     print("************************************************")
62 
63 #创建指定期望、方差的正态分布
64 def normalTest():
65     a = np.random.normal(size=5) #默认标准正态
66     print(a)
67     print("************************************************")
68 
69     #指定期望、方差
70     b = np.random.normal(loc=2,scale=3,size=(3,4))
71     print(b)
72     print("************************************************")
73 
74 
75 #调用
76 randomTest()
77 randomintTest()
78 randnTest()
79 normalTest()
 1 [0.65621982 0.80241202 0.80278345 0.57916965 0.70550304]
 2 <class 'numpy.ndarray'>
 3 ************************************************
 4 [[0.38511708 0.26239824 0.54494516 0.68058588]
 5  [0.59061136 0.99706398 0.80945098 0.80474533]
 6  [0.93491649 0.88597054 0.82931126 0.84172102]]
 7 ************************************************
 8 [[[0.42270519 0.83245331 0.02412723 0.44118108]
 9   [0.62360098 0.67016951 0.27050596 0.5549117 ]
10   [0.54453702 0.98018924 0.59094346 0.78222762]]
11 
12  [[0.46777695 0.74599711 0.60321822 0.25997951]
13   [0.37863074 0.2460353  0.60913763 0.42009367]
14   [0.77539277 0.07962959 0.98295345 0.78839267]]]
15 ************************************************
16 [0 5 5 2 3 3 3 5 3 5]
17 <class 'numpy.ndarray'>
18 ************************************************
19 [[10  8  9]
20  [ 5  8  6]
21  [10  5  6]
22  [ 6  7 10]]
23 ************************************************
24 [[[ 6  7 10]
25   [ 9  6  7]
26   [ 5 10  5]
27   [ 5  6  8]]
28 
29  [[ 5  8  7]
30   [ 9  9  9]
31   [ 8  8 10]
32   [ 6 10  9]]]
33 ************************************************
34 默认的dtype: int32
35 ************************************************
36 默认的dtype: int64
37 ************************************************
38 [-0.25476872  0.05729687 -0.95788614  0.04493104]
39 ************************************************
40 [[-0.0907436   1.3717756   2.15047082]
41  [ 0.74286161  1.57349613  0.1994152 ]]
42 ************************************************
43 [[[-1.8716447  -0.86870397 -0.292213    0.75890965]
44   [-0.56005917  1.04064364 -0.25263234  0.47086664]
45   [ 1.17985958  1.49773884  0.57591949 -0.25138537]]
46 
47  [[ 0.67782473 -1.46843923  0.24180541  0.19258284]
48   [-0.71265102  1.55964285  0.02499205  1.11449573]
49   [ 0.30099944  1.87730456  2.98227812 -2.25366232]]]
50 ************************************************
51 [-0.92424356  1.01699903 -1.02297162 -2.85417789  0.81376924]
52 ************************************************
53 [[-2.09414409  3.05514439 -1.01112304  1.24207097]
54  [ 3.74584855  7.75877175  6.92722856  2.23980584]
55  [ 4.68006021  0.36103857  3.85915139  1.62842631]]
56 ************************************************

 

posted @ 2020-03-27 21:39  小他_W  阅读(347)  评论(0编辑  收藏  举报