其他方式创建数组

 1 #导入numpy模块
 2 import numpy as np 
 3 #zeros
 4 def zerosTest():
 5     a = np.zeros(5)
 6     print(a)
 7     #指定类型
 8     b = np.zeros((5,),dtype=int)
 9     print(b)
10     #创建二维数组
11     c = np.zeros((3,4))
12     print(c)
13     print('********************************************')
14 
15 #ones
16 def oneTest():
17     a = np.ones(10)
18     print(a)
19     b = np.ones((2,5),dtype=int)
20     print(b)
21     print('********************************************')
22 
23 #empty
24 def emptyTest():
25     a = np.empty(8)
26     print(a)
27     b = np.empty((3,4))
28     print(b)
29     print('********************************************')
30 
31 #linespace
32 def linespaceTest():
33     a = np.linspace(1,10,11)
34     print(a)
35     b = np.linspace(5,20,5,endpoint=False) 
36     print(b)
37     print('********************************************')
38 
39 #logspace
40 def logspaceTest():
41     a = np.logspace(0,9,10,base=2)
42     print(a)
43     print('********************************************')
44 
45 zerosTest()
46 oneTest()
47 emptyTest()
48 linespaceTest()
49 logspaceTest()
 1 [0. 0. 0. 0. 0.]
 2 [0 0 0 0 0]
 3 [[0. 0. 0. 0.]
 4  [0. 0. 0. 0.]
 5  [0. 0. 0. 0.]]
 6 ********************************************
 7 [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
 8 [[1 1 1 1 1]
 9  [1 1 1 1 1]]
10 ********************************************
11 [ 0.00000000e+000  0.00000000e+000  0.00000000e+000  0.00000000e+000
12   0.00000000e+000  4.66397970e-321 -1.73027284e+181 -3.36359056e-224]
13 [[0. 0. 0. 0.]
14  [0. 0. 0. 0.]
15  [0. 0. 0. 0.]]
16 ********************************************
17 [ 1.   1.9  2.8  3.7  4.6  5.5  6.4  7.3  8.2  9.1 10. ]
18 [ 5.  8. 11. 14. 17.]
19 ********************************************
20 [  1.   2.   4.   8.  16.  32.  64. 128. 256. 512.]
21 ********************************************

 

posted @ 2020-03-29 21:52  小他_W  阅读(125)  评论(0编辑  收藏  举报