之前已经学过如何用reshape()方法改变举证的形状,现在学习更多的一些方法处理矩阵,比如矩阵的转置(T)和返回矩阵的所有元素(ravel())
import numpy as np
print("生成一个二维数组")
c = np.arange(1, 17).reshape(4, 4)
print(c)
print("返回转置矩阵")
print(c.T)
print("返回矩阵的所有元素")
print(c.ravel())
print("生成一个三维数组")
c1 = np.arange(1, 19).reshape(2, 3, 3)
print(c1)
print("返回矩阵的所有元素")
print(c1.ravel())
"""
结果为:
生成一个二维数组
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]
[13 14 15 16]]
返回转置矩阵
[[ 1 5 9 13]
[ 2 6 10 14]
[ 3 7 11 15]
[ 4 8 12 16]]
返回矩阵的所有元素
[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16]
生成一个三维数组
[[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]]
[[10 11 12]
[13 14 15]
[16 17 18]]]
返回矩阵的所有元素
[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18]
"""
矩阵的拼合分为行拼合(将行拼在一起使列数变多,使用np.hstack()方法)和列拼合(将列拼合在一起使用np.vstack()方法)
import numpy as np
print("打印初始二维数组")
a = np.arange(1, 17).reshape(4, 4)
print(a)
b = np.arange(17, 33).reshape(4, 4)
print(b)
print("列拼合,扩增行")
print(np.vstack((a, b)))
print("行拼合,列增行")
print(np.hstack((a, b)))
"""
打印初始二维数组
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]
[13 14 15 16]]
[[17 18 19 20]
[21 22 23 24]
[25 26 27 28]
[29 30 31 32]]
列拼合,扩增行
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]
[13 14 15 16]
[17 18 19 20]
[21 22 23 24]
[25 26 27 28]
[29 30 31 32]]
行拼合,列增行
[[ 1 2 3 4 17 18 19 20]
[ 5 6 7 8 21 22 23 24]
[ 9 10 11 12 25 26 27 28]
[13 14 15 16 29 30 31 32]]
"""
矩阵有拼合方法,当然也有划分方法vsplit()和hsplit()。注意,使用划分方法时必须将矩阵均等划分,不然会报错。
import numpy as np
print("打印初始二位数组")
a = np.arange(1, 33).reshape(8, 4)
print(a)
print("vsplit例子一:按整数均等划分")
print(np.vsplit(a, 4)) # 按行把矩阵划均等分成四份
# print(np.vsplit(a, 5)),这一句会报错,因为没有均等划分矩阵
print("vsplit例子二:按行号划分")
print(np.vsplit(a, (3, 4))) # 按行号3、4为界进行划分(从0行开始)划分出3个数组
print(np.vsplit(a, (3, 5, 7))) # 按行号3、5、7为界进行划分(从0行开始)划分出4个数组
print("打印初始二位数组")
a1 = np.arange(1, 33).reshape(4, 8)
print(a1)
print("hsplit例子一:按整数均等划分")
print(np.hsplit(a1, 4)) # 按列把矩阵划均等分成四份
print("vsplit例子二:按列号划分")
print(np.hsplit(a1, (3, 4))) # 按列号3、4为界进行划分(从0列开始)划分出3个数组
print(np.hsplit(a1, (3, 5, 7))) # 按列号3、5、7为界进行划分(从0列开始)划分出4个数组
"""
结果为:
打印初始二位数组
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]
[13 14 15 16]
[17 18 19 20]
[21 22 23 24]
[25 26 27 28]
[29 30 31 32]]
vsplit例子一:按整数均等划分
[array([[1, 2, 3, 4],
[5, 6, 7, 8]]), array([[ 9, 10, 11, 12],
[13, 14, 15, 16]]), array([[17, 18, 19, 20],
[21, 22, 23, 24]]), array([[25, 26, 27, 28],
[29, 30, 31, 32]])]
vsplit例子二:按行号划分
[array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12]]), array([[13, 14, 15, 16]]), array([[17, 18, 19, 20],
[21, 22, 23, 24],
[25, 26, 27, 28],
[29, 30, 31, 32]])]
[array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12]]), array([[13, 14, 15, 16],
[17, 18, 19, 20]]), array([[21, 22, 23, 24],
[25, 26, 27, 28]]), array([[29, 30, 31, 32]])]
打印初始二位数组
[[ 1 2 3 4 5 6 7 8]
[ 9 10 11 12 13 14 15 16]
[17 18 19 20 21 22 23 24]
[25 26 27 28 29 30 31 32]]
hsplit例子一:按整数均等划分
[array([[ 1, 2],
[ 9, 10],
[17, 18],
[25, 26]]), array([[ 3, 4],
[11, 12],
[19, 20],
[27, 28]]), array([[ 5, 6],
[13, 14],
[21, 22],
[29, 30]]), array([[ 7, 8],
[15, 16],
[23, 24],
[31, 32]])]
vsplit例子二:按列号划分
[array([[ 1, 2, 3],
[ 9, 10, 11],
[17, 18, 19],
[25, 26, 27]]), array([[ 4],
[12],
[20],
[28]]), array([[ 5, 6, 7, 8],
[13, 14, 15, 16],
[21, 22, 23, 24],
[29, 30, 31, 32]])]
[array([[ 1, 2, 3],
[ 9, 10, 11],
[17, 18, 19],
[25, 26, 27]]), array([[ 4, 5],
[12, 13],
[20, 21],
[28, 29]]), array([[ 6, 7],
[14, 15],
[22, 23],
[30, 31]]), array([[ 8],
[16],
[24],
[32]])]
"""
(不仅数学的矩阵有趣,这代码也颇为有趣...)