导入 numpy

约定俗成的以 np为numpy简称的导入方式

import numpy as np

numpy.array

创建一个数组

  • 参数:
    1. object:类数组
    2. dtype:数据类型(data-type),默认为 None
    3. copy:是否拷贝,默认为 True
    4. order:内存布局(’K’,’A’,’C’,’F’),默认为 ‘K’
    5. subok:传递子类还是基类,默认为 False,即传递基类
    6. ndmin:最小维度,默认为 0
  • 返回值:
    • out:ndarray
阅读全文

randint(随机整数)

Return random integers from low (inclusive) to high (exclusive).

从区间 [low, high) 中返回随机的整数数组

  • 参数
    1. low:最小值
    2. high:最大值,默认为 None
    3. size:个数,默认为None
    4. dtype:数据类型,默认为’l’
  • 返回值
    • out:整型或者整型数组

一些简单的🌰

  1. 0 到 50 中随机一个数
1
2
np.random.randint(50)
# 26
  1. 10 到 50 中随机一个数
1
2
np.random.randint(10, 50)
# 36
  1. 0 到 50 中随机 10 个数
1
2
np.random.randint(50, size=10)
# array([10, 25, 37, 34, 13, 15, 16, 12, 44, 4])
  1. 10 到 50 中随机 10 个数
1
2
np.random.randint(10, 50, 10)
# array([40, 46, 27, 44, 30, 46, 15, 41, 18, 23])
阅读全文