python运算学习之Numpy ------ 认识数组、数组的创建

  导入Numpy库,约定熟成的导入方式为 import numpy as np:

1 import numpy as np
2 
3 print(np.__version__)

Out[1]:
  1.14.3

创建数组:

  创建常规数组,测试numpy的属性,如:ndim、shape、itemsize、size、data

 1 arr_test = np.array([1, 2], dtype='f4')
 2 print(arr_test)
 3 
 4 arr_three = np.array(
 5     [
 6         [
 7             [1.2, 3.4, 5],
 8             [0.6, 0.9, 2.6]
 9 
10         ],
11         [
12             [3.4, 5.6, 8.3],
13             [2.0, 3.6, 5.3]
14         ]
15     ]
16 )
17 print("数组的维数:", arr_three.ndim)
18 print("数组的形状:", arr_three.shape)
19 print("数组每个元素占几个字节:", arr_three.itemsize)
20 print("数组的大小,即元素个数:", arr_three.size)
21 print("数组的缓冲区对象:", arr_three.data)
22 print(arr_three)

Out[2]:

  [1. 2.]
  数组的维数: 3
  数组的形状: (2, 2, 3)
  数组每个元素占几个字节: 8
  数组的大小,即元素个数: 12
  数组的缓冲区对象: <memory at 0x000001F33A60B138>
  [[[1.2 3.4 5. ]
   [0.6 0.9 2.6]]

   [[3.4 5.6 8.3]
   [2. 3.6 5.3]]]

  使用方法创建常见数组:如 zeros、ones、empty、zeros_like、ones_like、empty_like 等

 1 arr_ones = np.ones((3, 3))  # ones(shape, dtype=None, order='C')
 2 print(arr_ones)
 3 arr_zero = np.zeros((3, 3), dtype='i8')
 4 print(arr_zero)
 5 arr_empty = np.empty((3, 3))
 6 print(arr_empty)
 7 print(arr_empty[0])
 8 # 这里默认分配了全一数组,还需要初始化
 9 print("="*40)
10 arr_zeros_like = np.zeros_like(arr_three)  # zeros_like(a, dtype=None, order='K', subok=True)
11 arr_ones_like = np.ones_like(arr_zero, dtype='i8')
12 arr_empty_like = np.empty_like(arr_zero)  # 按照arr_zero的大小对应生成一个空的数组
13 print(arr_zeros_like, '\n', arr_ones_like, '\n', arr_empty_like)

Out[3]:

  [[1. 1. 1.]
   [1. 1. 1.]
   [1. 1. 1.]]
  [[0 0 0]
   [0 0 0]
   [0 0 0]]
  [[1. 1. 1.]
   [1. 1. 1.]
   [1. 1. 1.]]
  [1. 1. 1.]
  ========================================
  [[[0. 0. 0.]
    [0. 0. 0.]]

   [[0. 0. 0.]
    [0. 0. 0.]]]
  [[1 1 1]
   [1 1 1]
   [1 1 1]]
  [[4607182418800017408 4607182418800017408 4607182418800017408]
   [4607182418800017408 4607182418800017408 4607182418800017408]
   [4607182418800017408 4607182418800017408 4607182418800017408]]

  创建一个序列数组:方法有:arange、logspace

 1 # 生成数值序列的数组,类似于python中的range
 2 n_1 = np.arange(0, 10)  # arange(start=None, stop=None, step=None, dtype=None)
 3 print(n_1)  # stop是取不到的,牢记左闭右开
 4 n_2 = np.arange(0, 10, .6)
 5 print(n_2)
 6 n_3 = np.linspace(0, 10, 5)  # 等差数组linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
 7 print(n_3)
 8 n_4 = np.logspace(0, 10, 5)  # 表示10^0到10^10之间等比的取5个数。logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None)
 9 print(n_4)  # 等比数组
10 n_5 = np.logspace(0, 1, 5, base=2)  # 表示2^0到2^1之间等比的取5个数。base=np.e 表示以e为低。
11 print(n_5)
12 # 将列表转化为数组
13 a_list = [1, 2, 3]
14 a_arr = np.array(a_list)
15 print(type(a_arr))

Out[4]:

  [0 1 2 3 4 5 6 7 8 9]
  [0. 0.6 1.2 1.8 2.4 3. 3.6 4.2 4.8 5.4 6. 6.6 7.2 7.8 8.4 9. 9.6]
  [ 0. 2.5 5. 7.5 10. ]
  [1.00000000e+00 3.16227766e+02 1.00000000e+05 3.16227766e+07
   1.00000000e+10]
  [1. 1.18920712 1.41421356 1.68179283 2. ]
  <class 'numpy.ndarray'>

  随机数生成:

1 arr_random = np.random.random(3)
2 print(arr_random, type(arr_random))
3 arr_randint = np.random.randint(2)
4 print(arr_randint, type(arr_randint))
5 # 注意np.random里面有很多随机数生成的方法,如卡方分布,伽马分布等等
Out[5]:

  [0.45389402 0.29585506 0.17442431] <class 'numpy.ndarray'>
  1 <class 'int'>

  数组生成方法 np.tile(A, reps) :

1 # 根据一个数组a,生成一个全a的新数组
2 a = np.arange(0, 10, 3)
3 print(a)
4 b = np.tile(a, (2, 2))  # tile(A, reps)
5 print(b)
Out[6]:

  [0 3 6 9]
  [[0 3 6 9 0 3 6 9]
   [0 3 6 9 0 3 6 9]]

  上面的结果用红色进行了区分,很明显如果将0 3 6 9看作一个整体,那么数组是的2*2的一个2维数组。

  将字符串、bytes数据导入数组:

1 # 导入字符串、bytes数据
2 print(np.fromstring("abcdef", dtype=np.int8))
3 s = b"abcdef"
4 arr_s = np.frombuffer(s, dtype=np.int8)
5 print(arr_s, type(arr_s))  # 输出的是对应的ascii值组成的一维数组
Out[7]:
   [ 97 98 99 100 101 102]    [ 97 98 99 100 101 102] <class 'numpy.ndarray'>

  从文件中导入数据用:np.fromfile(),经测试很不好用,以后再看有上面更好的方法。

  根据矩阵的行号列号生成矩阵的元素,方法为 np.fromfunction()

def func(i, j):
    return i + j
test = np.fromfunction(func, (3, 3))  # fromfunction(function, shape, **kwargs)
print(test)
Out[8]:
[[0. 1. 2.]
 [1. 2. 3.]
 [2. 3. 4.]]

猜你喜欢

转载自www.cnblogs.com/dan-baishucaizi/p/9381776.html
今日推荐