📜  Python矩阵

📅  最后修改于: 2020-11-07 08:34:33             🧑  作者: Mango


矩阵是二维数组的特例,其中每个数据元素的大小都严格相同。因此,每个矩阵也是二维数组,反之亦然。对于许多数学和科学计算而言,矩阵是非常重要的数据结构。在上一章中,我们已经讨论了两个二维数组数据结构,因此在本章中,我们将专注于特定于矩阵的数据结构操作。

我们还将使用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']]

从矩阵删除列

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