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'] ]
-
访问值
可以使用索引访问矩阵中的数据元素。访问方式与二维数组中数据的访问方式相同。例子
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
-
添加一行
使用下面提到的代码在矩阵中添加一行。例子
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'] ]
-
添加列
我们可以使用 insert() 方法将列添加到矩阵中。这里我们必须提到我们要添加列的索引和一个包含添加列的新值的数组。在下面的示例中,我们在从头开始的第五个位置添加一个新列。例子
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'] ]
-
删除一行
我们可以使用 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'] ]
-
删除列
我们可以使用 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'] ]
-
更新一行
要更新矩阵行中的值,我们只需重新分配行索引处的值。在下面的示例中,星期四数据的所有值都标记为零。此行的索引为 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'] ]