ravel
ravel
是用来将矩阵向量化的操作
import numpy as np #导入numpy模块
a, b = np.mgrid[1:4:1, 2:3:1] #用mgrid()方法生成等差数组a,b
#
print("a:",a,"\n","b:",b) #打印a,b
Aftera = a.ravel() #用ravel()方法将数组a拉成一维数组
print("Aftera:",Aftera) #打印a
结果:
a: [[1] [2] [3]] b: [[2] [2] [2]] Aftera: [1 2 3]
np.random.choice()
#numpy.random.choice(a, size=None, replace=True, p=None)
#从a(只要是ndarray都可以,但必须是一维的)中随机抽取数字,并组成指定大小(size)的数组
#replace:True表示可以取相同数字,False表示不可以取相同数字
#数组p:与数组a相对应,表示取数组a中每个元素的概率,默认为选取每个元素的概率相同。
np.insert()
a=np.insert(arr, obj, values, axis)
#arr原始数组,可一可多,obj插入元素位置,values是插入内容,axis是按行按列插入(0:行、1:列)。
----------------------------------------------------------------------------------------
a = np.array([[1,2],[3,4],[5,6]])
b=np.insert(a,1,11,axis = 1)
# 输出
array([[ 1, 11, 2],
[ 3, 11, 4],
[ 5, 11, 6]])
a = np.array([[1,2],[3,4],[5,6]])
b=np.insert(a,1,[2,6],axis = 0)
# 输出
array([[1, 2],
[2, 6],
[3, 4],
[5, 6]])
np.random.randint()
numpy.random.randint(low, high=None, size=None, dtype='l')
low: int 生成的数值最低要大于等于low。 (hign = None时,生成的数值要在[0, low)区间内)
high: int(可选) 如果使用这个值,则生成的数值在[low, high)区间。
size: int or tuple of ints(可选)输出随机数的尺寸,比如size = (m * n* k)则输出同规模即m * n* k个随机数。默认是None的,仅仅返回满足要求的单一随机数。
dtype: dtype(可选): 想要输出的格式。如int64、int等等
np.random.randint(1.75, 4, 11) ##11个随机整数数字
#array([3, 3, 2, 3, 1, 1, 2, 2, 2, 3, 2])
np.random.randint(5, size=(2, 4)) #二行四列,higt为none,取值是0到5之间的整数
#array([[4, 3, 0, 4],
[3, 1, 1, 3]])
np.random.rand()
np.random.rand(d0,d1,d2……dn)
注:使用方法与np.random.randn()函数相同
作用: 通过本函数可以返回一个或一组服从“0~1”均匀分布的随机样本值。随机样本取值范围是[0,1),不包括1。
应用:在深度学习的Dropout正则化方法中,可以用于生成dropout随机向量(dl),例如(keep_prob表示保留神经元的比例):dl = np.random.rand(al.shape[0],al.shape[1]) < keep_prob
random.shuffle()
用于将一个列表中的元素打乱顺序,值得注意的是使用这个方法不会生成新的列表,只是将原列表的次序打乱。