橘子味的心
标题:Python - 矩阵

矩阵是二维数组的特殊情况,其中每个数据元素具有严格相同的大小。所以每个矩阵也是一个二维数组,但反之亦然。矩阵是许多数学和科学计算中非常重要的数据结构。正如我们在前一章中已经讨论过的两个双维数组结构,我们将在本章中专注于矩阵特有的数据结构操作。

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

矩阵示例

考虑在早上,中午,晚上和深夜测量记录温度1周的情况。它可以使用数组以及numpy中可用的重塑方法呈现为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']]

从Matrix中删除一列

我们可以使用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']]

更新Matrix中的一行

要更新矩阵行中的值,我们只需在行的索引处重新分配值。在下面的例子中,星期几数据的所有值都标记为零。该行的索引是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']]

分类