numpy的array数据类型(创建)

  1 import numpy as np
  2 
  3 # 创建
  4 # 创建一维数组
  5 a = np.array([1, 2, 3])
  6 print(a)
  7 '''
  8 [1 2 3]
  9 '''
 10 # 创建多维数组
 11 b = np.array([(1, 2, 3), (4, 5, 6)])
 12 print(b)
 13 '''
 14 [[1 2 3]
 15  [4 5 6]]
 16 '''
 17 # 创建等差一维数组
 18 c = np.arange(1, 5, 0.5)
 19 print(c)
 20 '''
 21 [1.  1.5 2.  2.5 3.  3.5 4.  4.5]
 22 '''
 23 # 创建随机数数组
 24 d = np.random.random((2, 2))
 25 print(d)
 26 '''
 27 [[0.65746941 0.09766114]
 28  [0.15024283 0.9212932 ]]
 29  '''
 30 # 创建一个确定起始点和终止点和个数的等差一维数组
 31 ##包含终止点
 32 e = np.linspace(1, 2, 10)
 33 print(e)
 34 '''
 35 [1.         1.11111111 1.22222222 1.33333333 1.44444444 1.55555556 1.66666667 1.77777778 1.88888889 2.        ]
 36  '''
 37 ##不包含终止点
 38 f = np.linspace(1, 2, 10, endpoint=False)
 39 print(f)
 40 '''
 41 [1.  1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9]
 42 '''
 43 # 创建一个全为‘1’的 数组
 44 g = np.ones([2, 3])
 45 print(g)
 46 '''
 47 [[1. 1. 1.]
 48  [1. 1. 1.]]
 49  '''
 50 # 创建一个全为‘0’的数组
 51 h = np.zeros([2, 3])
 52 print(h)
 53 '''
 54 [[0. 0. 0.]
 55  [0. 0. 0.]]
 56  '''
 57 # 创建一个全为'自定义的值'的数组
 58 i = np.full((2, 3), fill_value=21)
 59 print(i)
 60 '''
 61 [[21 21 21]
 62  [21 21 21]]
 63 '''
 64 # 创建一个对角线为‘1’,其他的位置为‘0’
 65 j = np.eye(4)
 66 print(j)
 67 '''
 68 [[1. 0. 0. 0.]
 69  [0. 1. 0. 0.]
 70  [0. 0. 1. 0.]
 71  [0. 0. 0. 1.]]
 72 '''
 73 # 创建一个标准的正态分布
 74 h = np.random.randn(50)
 75 print(h)
 76 '''
 77 [ 0.01250963 -0.7387912   0.34890184  0.45922031  0.69632711  1.45936167
 78  -0.01958069 -0.42200162 -1.59439929 -0.38340785 -0.09423212  0.46495457
 79  -1.07383807  1.26489024  1.50519718  1.21760287 -1.43837182  0.11904866
 80   0.29399612 -1.66294523  1.42131044  0.13073129  0.02832415  1.57078671
 81  -0.96096118  0.1636397   0.25686109  0.92687274 -0.14074038 -0.2355995
 82   0.06471922  0.00188039  0.56639013 -0.12014897 -0.5348929  -0.91173276
 83   1.04026246 -1.39317966 -0.42333174 -0.28924722  1.09360504  0.16879087
 84  -0.4505147   0.38581222 -0.42106339  0.29927751 -0.9056031  -0.86102655
 85  -0.61423026 -0.94604185]
 86 '''
 87 # 创建一个自定义的正态分布
 88 h = np.random.normal(loc=175, scale=0.3, size=50)
 89 print(h)
 90 # loc为位置参数
 91 # scale为尺度参数,值越大离散程度越大
 92 # size为总数据个数
 93 '''
 94 [175.01002617 175.49445311 175.15833447 174.42510606 174.78144183
 95  174.84035925 174.76628391 174.84687069 174.93967239 175.29902946
 96  175.08438032 175.1476928  174.992446   174.87066715 175.02578143
 97  175.03768609 175.20249608 174.96956083 174.62277043 175.59116051
 98  175.59419255 174.74925345 175.44279974 175.07262176 174.91848554
 99  174.90220037 175.19871001 175.04802743 174.71962518 175.07843723
100  174.87821195 174.88255464 175.56090823 174.44660242 175.11230508
101  174.89422801 174.63803226 175.03060753 174.84452539 174.99050179
102  174.9037525  174.90163791 175.42865325 174.76396595 174.99927621
103  175.15771656 174.72123296 175.22466598 174.72349497 174.95927315]
104 '''
105 # 通过函数创建数组
106 k = np.fromfunction(lambda i, j: (i + 1) * (j + 1), (9, 9))
107 print(k)
108 '''
109 [[ 1.  2.  3.  4.  5.  6.  7.  8.  9.]
110  [ 2.  4.  6.  8. 10. 12. 14. 16. 18.]
111  [ 3.  6.  9. 12. 15. 18. 21. 24. 27.]
112  [ 4.  8. 12. 16. 20. 24. 28. 32. 36.]
113  [ 5. 10. 15. 20. 25. 30. 35. 40. 45.]
114  [ 6. 12. 18. 24. 30. 36. 42. 48. 54.]
115  [ 7. 14. 21. 28. 35. 42. 49. 56. 63.]
116  [ 8. 16. 24. 32. 40. 48. 56. 64. 72.]
117  [ 9. 18. 27. 36. 45. 54. 63. 72. 81.]]
118  '''

 

转载于:https://www.cnblogs.com/xshan/p/9866189.html

你可能感兴趣的:(python)