Numpy的使用(2)

常用的数组及其变形

1、常用数组的创建

1、1全0的数组

Numpy的使用(2)_第1张图片
Numpy的使用(2)_第2张图片

1、2全1数组

b = np.ones(5) #默认float64型
b
c = np.ones((2,3),dtype=np.int)

1、3随机数组

np.random.rand(2,3) #返回[0, 1)之间的数
array([[0.1886071 , 0.11778213, 0.75613725],
       [0.55801559, 0.84307341, 0.03900024]])
       
       np.random.randint(1, 10, (3, 6)) #起始值,终止值(不包含),形状
       array([[1, 3, 9, 8, 2, 6],
       [2, 6, 5, 3, 2, 7],
       [3, 6, 9, 2, 2, 4]])
       
#生成一个三维数组
np.random.randint(1,5,(2,3,3))
array([[[4, 2, 1],
        [2, 4, 4],
        [1, 3, 3]],

       [[4, 3, 4],
        [2, 2, 2],
        [2, 1, 3]]])
        
np.random.normal(2,1,(2,2))#生成均值为2,标准差1的(2,2)的正态分布
array([[3.17793788, 1.40527582],
       [3.41286715, 2.30436796]])

np.random.normal(4, 2, (3,3)) 
array([[4.83284853, 2.89363485, 7.10305535],
       [1.83308336, 0.95330586, 4.52366048],
       [2.57295016, 7.14112992, 0.95837073]])

np.random.randn(4,2,3) #np.random.standard_normal的简写形式   标准正态分布
array([[[-1.75707328,  0.04405065, -0.71344579],
        [ 0.40162545, -1.07854554,  0.24608147]],

       [[ 0.8420804 ,  0.83717103, -2.90153057],
        [ 1.26795487,  0.0444246 , -0.46536408]],

       [[-1.09068471,  0.93762461, -0.29004083],
        [-0.36024306, -2.19659071,  0.51255556]],

       [[-0.47880211, -0.27406868,  0.10676188],
        [-0.07234616, -0.1863053 , -0.98849959]]])
        
np.random.standard_normal((4,2,3)
array([[[ 1.59106145,  1.2175568 ,  0.11270557],
        [ 0.67033589, -0.56639093, -0.11936274]],

       [[ 0.63298831,  0.49064045, -0.67077725],
        [-2.37431753,  1.4642621 ,  0.11996945]],

       [[-0.84237636,  1.01458359,  0.1398038 ],
        [ 0.56170129,  0.56296184, -0.50162535]],

       [[ 1.40019432,  0.62035531,  0.26629534],
        [ 1.1667477 ,  0.11741135, -0.30593312]]])

d = np.random.randn(2, 3)
d
array([[ 0.09004468, -1.39751121, -0.20283765],
       [-0.93946504,  0.39819292, -1.57714855]])

# 设置随机数种子, 拆开发现不好使, 必须放在一起才好使
np.random.seed(1)
d = np.random.randn(2, 3)
d

array([[ 1.62434536, -0.61175641, -0.52817175],
       [-1.07296862,  0.86540763, -2.3015387 ]])

1、4创建一个线性序列的数组

np.arange(16)
np.arange(0, 16, 2)
np.arange(0, 16, 4)

array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15])
array([ 0,  2,  4,  6,  8, 10, 12, 14])
array([ 0,  4,  8, 12])

#从运行结果可以看出0-16的range,实际在取数的时候只能0到15,三个参数中的第三个参数是表示步长。


1、5全为相同值的数组

np.full((5,4),6) 
#5行4列全部为6
array([[6, 6, 6, 6],
       [6, 6, 6, 6],
       [6, 6, 6, 6],
       [6, 6, 6, 6],
       [6, 6, 6, 6]])

np.full((3,2,4),[[1,2,3,4],[2,3,4,5]])
array([[[1, 2, 3, 4],
        [2, 3, 4, 5]],

       [[1, 2, 3, 4],
        [2, 3, 4, 5]],

       [[1, 2, 3, 4],
        [2, 3, 4, 5]]])

1、6单位矩阵

np.eye(5)  #默认float型,5行5列的单位矩阵
array([[1., 0., 0., 0., 0.],
       [0., 1., 0., 0., 0.],
       [0., 0., 1., 0., 0.],
       [0., 0., 0., 1., 0.],
       [0., 0., 0., 0., 1.]])

np.eye(3,6)   #3行六列的单位矩阵
array([[1., 0., 0., 0., 0., 0.],
       [0., 1., 0., 0., 0., 0.],
       [0., 0., 1., 0., 0., 0.]])

np.eye(5,k=-1,dtype='int')  #k代表平移量,正数代表对角线向右上角平移负数代表对角线向左下角平移
array([[0, 0, 0, 0, 0],
       [1, 0, 0, 0, 0],
       [0, 1, 0, 0, 0],
       [0, 0, 1, 0, 0],
       [0, 0, 0, 1, 0]])

1、7 创建对角矩阵

np.diag([1,2,3,4,5],k=2) #对角 Diagonal
array([[0, 0, 1, 0, 0, 0, 0],
       [0, 0, 0, 2, 0, 0, 0],
       [0, 0, 0, 0, 3, 0, 0],
       [0, 0, 0, 0, 0, 4, 0],
       [0, 0, 0, 0, 0, 0, 5],
       [0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0]])

np.diag([5,2,5,4.2])
array([[5. , 0. , 0. , 0. ],
       [0. , 2. , 0. , 0. ],
       [0. , 0. , 5. , 0. ],
       [0. , 0. , 0. , 4.2]])

np.diag([1,2,3,'a'],2) #最后一个参数表示偏移量,正数向右上角偏移,负数向最下角偏移
array([['', '', '1', '', '', ''],
       ['', '', '', '2', '', ''],
       ['', '', '', '', '3', ''],
       ['', '', '', '', '', 'a'],
       ['', '', '', '', '', ''],
       ['', '', '', '', '', '']], dtype=')

np.diag([1,2,3,'a'],-2)
array([['', '', '', '', '', ''],
       ['', '', '', '', '', ''],
       ['1', '', '', '', '', ''],
       ['', '2', '', '', '', ''],
       ['', '', '3', '', '', ''],
       ['', '', '', 'a', '', '']], dtype=')
       
np.diag(np.array([1, 2, 3, 4]))
array([[1, 0, 0, 0],
       [0, 2, 0, 0],
       [0, 0, 3, 0],
       [0, 0, 0, 4]])

1、8按照某一数组的形状生成ndarray

e = [[2,1,3],[4,3,2]]
np.array(e)
array([[2, 1, 3],
       [4, 3, 2]])

np.ones_like(e)
np.zeros_like(e)
np.full_like(e,5)
array([[1, 1, 1],
       [1, 1, 1]])
array([[0, 0, 0],
       [0, 0, 0]])
array([[5, 5, 5],
       [5, 5, 5]])
       

1、9创建一个初始化的数组

np.empty(6)  #为什么这里会有数字显示呢?  为了占位
array([1.62434536, 0.61175641, 0.52817175, 1.07296862, 0.86540763,
       2.3015387 ])

f = np.empty((2,3))
f
array([[1.62434536, 0.61175641, 0.52817175],
       [1.07296862, 0.86540763, 2.3015387 ]])

f.fill(5.5)
f
array([[5.5, 5.5, 5.5],
       [5.5, 5.5, 5.5]])

np.random.randint(5, 15, (3, 6)) 
np.random.randn(3,4)
array([[ 0.90085595, -0.68372786, -0.12289023, -0.93576943],
       [-0.26788808,  0.53035547, -0.69166075, -0.39675353],
       [-0.6871727 , -0.84520564, -0.67124613, -0.0126646 ]])

np.random.normal(10,5,(7,3))
np.arange(12,22,2)
np.linspace(12,22,num=100,endpoint=False)

2、数组的变形

函数 意义
ndarray.reshape 返回调整shape后的数组(不改变原数组)
ndarray.resize 返回调整shape后的数组(改变原数组)
ndarray.flatten 对数组进行降维,返回折叠后的一维数组
ndarray.ravel 降维成一维
ndarray.swapaxes 返回调换两个维度后的数组
m = np.random.randint(1, 100,(2, 3, 4))
m

array([[[41, 79, 46, 88],
        [17, 29, 46, 68],
        [67, 79, 47,  1]],

       [[30, 64, 76, 36],
        [54, 94, 34,  3],
        [85, 84, 49, 55]]])

m.shape
(2, 3, 4)
m.reshape(1,3,8)
array([[[41, 79, 46, 88, 17, 29, 46, 68],
        [67, 79, 47,  1, 30, 64, 76, 36],
        [54, 94, 34,  3, 85, 84, 49, 55]]])
        m.reshape(6,4) 
        array([[41, 79, 46, 88],
       [17, 29, 46, 68],
       [67, 79, 47,  1],
       [30, 64, 76, 36],
       [54, 94, 34,  3],
       [85, 84, 49, 55]])

m.reshape(24)
m                        #没有改变原数组
array([41, 79, 46, 88, 17, 29, 46, 68, 67, 79, 47,  1, 30, 64, 76, 36, 54,
       94, 34,  3, 85, 84, 49, 55])
array([[[41, 79, 46, 88],
        [17, 29, 46, 68],
        [67, 79, 47,  1]],

       [[30, 64, 76, 36],
        [54, 94, 34,  3],
        [85, 84, 49, 55]]])

m.reshape(1,-1) #-1用来站位 1在行的位置,即把数据变成一行
array([[41, 79, 46, 88, 17, 29, 46, 68, 67, 79, 47,  1, 30, 64, 76, 36,
        54, 94, 34,  3, 85, 84, 49, 55]])

m.flatten() 
array([41, 79, 46, 88, 17, 29, 46, 68, 67, 79, 47,  1, 30, 64, 76, 36, 54,
       94, 34,  3, 85, 84, 49, 55])

m.ravel() #与flatten作用相似
array([41, 79, 46, 88, 17, 29, 46, 68, 67, 79, 47,  1, 30, 64, 76, 36, 54,
       94, 34,  3, 85, 84, 49, 55])
       
m=m.reshape(3,4,2)

n = m.swapaxes(0,1) #将两个维度调换,不改变原数组
n

array([[[41, 79],
        [67, 79],
        [54, 94]],

       [[46, 88],
        [47,  1],
        [34,  3]],

       [[17, 29],
        [30, 64],
        [85, 84]],

       [[46, 68],
        [76, 36],
        [49, 55]]])

3、转置

Numpy 的转置可以按照你的需要对数组的轴进行转换。

a = np.arange(15).reshape((3, 5))
print(a)

b = np.arange(7)
print(b.shape)
print(b.ndim)
print(b)

c=np.array(np.arange(24).reshape((3,2,4)))
c.shape

np.array(np.arange(24).reshape((3,2,4))).T.shape

你可能感兴趣的:(python-numpy,python,tensorflow,机器学习,人工智能)