- Python 数据结构和算法教程
- Python - 数据结构教程
- Python - 数据结构简介
- Python - 数据结构环境
- Python - 二维数组的数据结构
- Python - 矩阵的数据结构
- Python - 地图的数据结构
- Python - 链表的数据结构
- Python - 堆栈的数据结构
- Python - 队列的数据结构
- Python - 取消排队
- Python - 高级链表
- Python - 哈希表的数据结构
- Python - 二叉树
- Python - 二叉搜索树
- Python - 堆数据结构
- Python - 图形数据结构
- Python - 算法设计
- Python - 分治算法
- Python - 回溯
- Python - 排序算法
- Python - 搜索算法
- Python - 图形算法
- Python - 算法分析
- Python - 算法类型
- Python - 算法类
- Python - 摊销分析
- Python - 算法理由
Python - 矩阵的数据结构
Python 矩阵是二维数组的一种特殊情况,其中每个数据元素的大小都严格相同。所以每个矩阵也是一个二维数组,但反之则不然。
矩阵是许多数学和科学计算中非常重要的数据结构。正如我们在上一章中已经讨论了二维数组数据结构,因此在本章中我们将重点介绍特定于矩阵的数据结构操作。
我们还使用 numpy 包进行矩阵数据操作。
矩阵示例
考虑在早上、中午、晚上和午夜测量的 1 周温度记录的情况。它可以使用 numpy 中提供的数组和 reshape 方法表示为 7X5 矩阵。
from numpy import *
a = array([['Mon',18,20,22,17],['Tue',11,18,21,18],
['Wed',15,21,20,19],['Thu',11,20,22,21],
['Fri',18,17,23,22],['Sat',12,22,20,18],
['Sun',13,15,19,16]])
m = reshape(a,(7,5))
print(m)
输出
上述数据可以表示为二维数组,如下所示 -
[
['Mon' '18' '20' '22' '17']
['Tue' '11' '18' '21' '18']
['Wed' '15' '21' '20' '19']
['Thu' '11' '20' '22' '21']
['Fri' '18' '17' '23' '22']
['Sat' '12' '22' '20' '18']
['Sun' '13' '15' '19' '16']
]
['Mon' '18' '20' '22' '17']
['Tue' '11' '18' '21' '18']
['Wed' '15' '21' '20' '19']
['Thu' '11' '20' '22' '21']
['Fri' '18' '17' '23' '22']
['Sat' '12' '22' '20' '18']
['Sun' '13' '15' '19' '16']
]
访问值
可以使用索引访问矩阵中的数据元素。访问方法与在二维数组中访问数据的方式相同。
from numpy import *
m = array([['Mon',18,20,22,17],['Tue',11,18,21,18],
['Wed',15,21,20,19],['Thu',11,20,22,21],
['Fri',18,17,23,22],['Sat',12,22,20,18],
['Sun',13,15,19,16]])
# Print data for Wednesday
print(m[2])
# Print data for friday evening
print(m[4][3])
输出
执行上述代码时,它会产生以下结果 -
['Wed', 15, 21, 20, 19]
23
23
添加行
使用下面提到的代码在矩阵中添加一行。
例
from numpy import *
m = array([['Mon',18,20,22,17],['Tue',11,18,21,18],
['Wed',15,21,20,19],['Thu',11,20,22,21],
['Fri',18,17,23,22],['Sat',12,22,20,18],
['Sun',13,15,19,16]])
m_r = append(m,[['Avg',12,15,13,11]],0)
print(m_r)
输出
执行上述代码时,它会产生以下结果 -
[
['Mon' '18' '20' '22' '17']
['Tue' '11' '18' '21' '18']
['Wed' '15' '21' '20' '19']
['Thu' '11' '20' '22' '21']
['Fri' '18' '17' '23' '22']
['Sat' '12' '22' '20' '18']
['Sun' '13' '15' '19' '16']
['Avg' '12' '15' '13' '11']
]
['Mon' '18' '20' '22' '17']
['Tue' '11' '18' '21' '18']
['Wed' '15' '21' '20' '19']
['Thu' '11' '20' '22' '21']
['Fri' '18' '17' '23' '22']
['Sat' '12' '22' '20' '18']
['Sun' '13' '15' '19' '16']
['Avg' '12' '15' '13' '11']
]
添加列
我们可以使用 insert() 方法向矩阵添加列。在这里,我们必须提到要添加列的索引和一个包含所添加列的新值的数组。在下面的示例中,我们在从开头开始的第五个位置添加 t 个新列。
例
from numpy import *
m = array([['Mon',18,20,22,17],['Tue',11,18,21,18],
['Wed',15,21,20,19],['Thu',11,20,22,21],
['Fri',18,17,23,22],['Sat',12,22,20,18],
['Sun',13,15,19,16]])
m_c = insert(m,[5],[[1],[2],[3],[4],[5],[6],[7]],1)
print(m_c)
输出
执行上述代码时,它会产生以下结果 -
[
['Mon' '18' '20' '22' '17' '1']
['Tue' '11' '18' '21' '18' '2']
['Wed' '15' '21' '20' '19' '3']
['Thu' '11' '20' '22' '21' '4']
['Fri' '18' '17' '23' '22' '5']
['Sat' '12' '22' '20' '18' '6']
['Sun' '13' '15' '19' '16' '7']
]
['Mon' '18' '20' '22' '17' '1']
['Tue' '11' '18' '21' '18' '2']
['Wed' '15' '21' '20' '19' '3']
['Thu' '11' '20' '22' '21' '4']
['Fri' '18' '17' '23' '22' '5']
['Sat' '12' '22' '20' '18' '6']
['Sun' '13' '15' '19' '16' '7']
]
删除行
我们可以使用 delete() 方法从矩阵中删除一行。我们必须指定行的索引以及轴值,一行为 0,一列为 1。
例
from numpy import *
m = array([['Mon',18,20,22,17],['Tue',11,18,21,18],
['Wed',15,21,20,19],['Thu',11,20,22,21],
['Fri',18,17,23,22],['Sat',12,22,20,18],
['Sun',13,15,19,16]])
m = delete(m,[2],0)
print(m)
输出
执行上述代码时,它会产生以下结果 -
[
['Mon' '18' '20' '22' '17']
['Tue' '11' '18' '21' '18']
['Thu' '11' '20' '22' '21']
['Fri' '18' '17' '23' '22']
['Sat' '12' '22' '20' '18']
['Sun' '13' '15' '19' '16']
]
['Mon' '18' '20' '22' '17']
['Tue' '11' '18' '21' '18']
['Thu' '11' '20' '22' '21']
['Fri' '18' '17' '23' '22']
['Sat' '12' '22' '20' '18']
['Sun' '13' '15' '19' '16']
]
删除列
我们可以使用 delete() 方法从矩阵中删除一列。我们必须指定列的索引以及轴值,一行为 0,一列为 1。
例
from numpy import *
m = array([['Mon',18,20,22,17],['Tue',11,18,21,18],
['Wed',15,21,20,19],['Thu',11,20,22,21],
['Fri',18,17,23,22],['Sat',12,22,20,18],
['Sun',13,15,19,16]])
m = delete(m,s_[2],1)
print(m)
输出
执行上述代码时,它会产生以下结果 -
[
['Mon' '18' '22' '17']
['Tue' '11' '21' '18']
['Wed' '15' '20' '19']
['Thu' '11' '22' '21']
['Fri' '18' '23' '22']
['Sat' '12' '20' '18']
['Sun' '13' '19' '16']
]
更新行
要更新矩阵行中的值,我们只需在行的索引处重新分配值。在下面的示例中,thrusday 数据的所有值都标记为零。此行的索引为 3。
例
from numpy import *
m = array([['Mon',18,20,22,17],['Tue',11,18,21,18],
['Wed',15,21,20,19],['Thu',11,20,22,21],
['Fri',18,17,23,22],['Sat',12,22,20,18],
['Sun',13,15,19,16]])
m[3] = ['Thu',0,0,0,0]
print(m)
输出
执行上述代码时,它会产生以下结果 -
[
['Mon' '18' '20' '22' '17']
['Tue' '11' '18' '21' '18']
['Wed' '15' '21' '20' '19']
['Thu' '0' '0' '0' '0']
['Fri' '18' '17' '23' '22']
['Sat' '12' '22' '20' '18']
['Sun' '13' '15' '19' '16']
]
['Mon' '18' '20' '22' '17']
['Tue' '11' '18' '21' '18']
['Wed' '15' '21' '20' '19']
['Thu' '0' '0' '0' '0']
['Fri' '18' '17' '23' '22']
['Sat' '12' '22' '20' '18']
['Sun' '13' '15' '19' '16']
]