Python Data Structure 简明教程

Python - Matrix

矩阵是二维数组的一种特殊情况,其中每个数据元素的大小完全相同。因此,每个矩阵均是二维数组,但反之不然。

矩阵是对许多数学和科学计算非常重要的数据结构。正如我们在上一章已经讨论过二维数组数据结构一样,本章将重点介绍针对矩阵的数据结构操作。

我们还会使用 numpy 包进行矩阵数据操作。

Matrix Example

考虑记录一周内在上午、中午、晚上和午夜测得的温度的情况。可以使用数组和 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)

Output

可以用二维数组表示上述数据,如下所示:

[
   ['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']
]

Accessing Values

可以使用索引访问矩阵中的数据元素。访问方法与在二维数组中访问数据的方法相同。

Example

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])

Output

执行上述代码后,将生成以下结果 −

['Wed', 15, 21, 20, 19]
23

Adding a row

使用以下提到的代码为矩阵添加一行。

Example

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)

Output

执行上述代码后,将生成以下结果 −

[
   ['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']
]

Adding a column

我们可以使用 insert() 方法为矩阵添加一列。在此,我们必须指定要添加列的位置,以及一个包含所添加列的新值的数组。在以下示例中,我们在起始位置添加了一列。

Example

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)

Output

执行上述代码后,将生成以下结果 −

[
   ['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 a row

我们可以使用 delete() 方法从矩阵中删除一行。我们必须指定该行的索引以及轴值,行的轴值为 0,而列的轴值为 1。

Example

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)

Output

执行上述代码后,将生成以下结果 −

[
   ['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 a column

我们可以使用 delete() 方法从矩阵中删除一列。我们必须指定该列的索引以及轴值,行的轴值为 0,而列的轴值为 1。

Example

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)

Output

执行上述代码后,将生成以下结果 −

[
   ['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']
]

Update a row

为了更新矩阵中行的值,我们只需重新指定行索引处的值。以下示例中,所有星期四的数据值都标记为零。该行的索引为 3。

Example

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)

Output

执行上述代码后,将生成以下结果 −

[
   ['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']
]