Python之numpy

numpy.random

numpy.random.rand()

  • numpy.random.rand(d0,d1,…,dn)
  • 根据给定维度生成[0,1)之间的数据,包含0,不包含1
  • 返回值为指定维度的array
1
2
3
4
np.random.rand(3,2)
# array([[0.77342926, 0.13062061],
# [0.04889282, 0.66763161],
# [0.72599757, 0.27826069]])

numpy.random.randn()

  • numpy.random.randn(d0,d1,…,dn)
  • 返回一个或一组样本,具有标准正态分布。
  • 返回值为指定维度的array
1
2
3
4
5
6
7
np.random.randn()   # 没有参数时,返回单个数
# -1.3281108519652927

np.random.randn(3,2)
# array([[ 2.11802305, 2.85387116],
# [-0.01947161, -2.09343863],
# [-1.96342724, 0.01979386]])

numpy.random.randint()

  • numpy.random.randint(low, high=None, size=None, dtype=’l’)
  • 返回随机整数,范围区间为[low,high),包含low,不包含high
  • dtype为数据类型,默认是np.int
  • 没有high时,默认的是[0,low)
1
2
3
4
5
6
np.random.randint(2, size = 10)
# array([1, 1, 1, 1, 0, 1, 1, 0, 1, 0])

np.random.randint(1, 5, size = (2,4))
# array([[3, 1, 2, 2],
# [3, 2, 4, 1]])

numpy.random.random_sample()

numpy.random.random()

numpy.random.ranf()

numpy.random.sample()

  • numpy.random.—(size = None)
  • 返回[0.0, 1.0]区间的浮点数
1
2
3
4
5
6
7
np.random.random_sample()
# 0.43535270552664374

np.random.ranf((3,2))
# array([[0.07730948, 0.19447818],
# [0.66644615, 0.41688169],
# [0.92430307, 0.78935612]])

numpy.random.choice()

  • numpy.random.choice(a, size = None, replace = True, p = None)
  • a:一位数组或一个int值,一维数组的话就是从数组中随机生成元素,int的话就类似于np.arange(a)
  • size:int值或者int元组,为输出形状
  • replace:是否可以出现重复值,默认为True,False的话就不允许有重复
  • p:a中的每个数出现的概率,默认为均匀分布的
  • 返回值为指定维度的array
1
2
3
4
5
6
7
8
9
10
11
12
13
# 生成大小为3,范围为np.arange(5)
np.random.choice(5,3)
# array([4, 3, 3])

# 生成大小为3,范围为np.arange(5)的非均匀的
np.random.choice(5,3,p = [0.1, 0, 0.6, 0.2, 0.1])
# array([3, 0, 2])

# 由数组生成
aa_milne_arr = ['pooh', 'rabbit', 'piglet', 'Christopher']
np.random.choice(aa_milne_arr, 5, p=[0.5, 0.1, 0.1, 0.3])
# array(['pooh', 'Christopher', 'piglet', 'Christopher', 'pooh'],
# dtype='<U11')

numpy.random.seed

  • numpy.random.seed(seed = None)
  • seed:指定seed值,将会得到相同的随机结果
1
2
3
4
5
6
7
np.random.seed(1000)
np.random.randint(5)
# 3

np.random.seed(1000)
np.random.randint(5)
# 3

其他

  • astype操作总是会返回一个新的数组
  • 两个数组之间的运算是对应位置元素的运算;数组与标量的运算是涉及到每一个数组元素的;数组与数组之间的比较是产生一个bool数组
  • python的list与numpy的array的重要区别:list的切片是产生了一个新的list,而array的切片是产生一view,即对list的切片进行值的修改,不影响原list,但是nrray会影响
1
2
3
4
5
6
7
8
9
10
11
list1 = [1,2,3,4,5,6]
arr1 = np.array(list1)
list2 = list1[:3]
arr2 = arr1[:3]
arr2[0] = 1234
list2[0] = 1234

# list1:[1,2,3,4,5,6]
# list2:[1234, 2, 3]
# arr1:array([1234, 2, 3, 4, 5, 6])
# arr2:array([1234, 2, 3])

numpy.where()

  • numpy.where(condition[, x, y])
  • condition:条件,数组的形式,满足时返回x的对应值,不满足时返回y
  • x,y:数组形式
1
2
3
4
5
6
x = np.arange(9.).reshape(3, 3)
np.where(x<5, x, 0)

# array([[0., 1., 2.],
# [3., 4., 0.],
# [0., 0., 0.]])
  • numpy.any检测数组中只要有一个ture返回就是true,而numpy.all检测数组中都是true才会返回true
1
2
3
bools = np.array([False, False, True, False])
np.any(bools) # True
np.all(bools) # False
  • numpy.unique 能返回排好序且不重复的值,相当于Python的sort(set(x))
1
2
3
ints = np.array([3, 3, 3, 2, 2, 1, 1, 4, 4])
np.unique(ints)
# array([1, 2, 3, 4])
  • numpy.in1d 测试一个数组的值是否在另一个数组里,返回一个布尔数组
1
2
3
values = np.array([6, 0, 0, 3, 2, 5, 6])
np.in1d(values, [2, 3, 6])
# array([ True, False, False, True, True, False, True], dtype=bool)

numpy.cumsum()

  • numpy.cumsum(a, axis=None, dtype=None, out=None)
  • 返回给定轴上的累加和
  • a:数组形式
  • axis:给定轴,默认是一维地去累加
  • 返回一个ndarray
1
2
3
4
5
6
7
8
9
10
a = np.array([[1,2,3], [4,5,6]])
# array([[1, 2, 3],
# [4, 5, 6]])

np.cumsum(a)
# array([ 1, 3, 6, 10, 15, 21])

np.cumsum(a,axis=0)
# array([[1, 2, 3],
# [5, 7, 9]])
-------------本文结束 感谢您的阅读-------------