矩陣是二維陣列的特殊情況,它的每個資料元素具有嚴格相同的大小。 所以每個矩陣也是一個二維陣列,但反之亦然。 矩陣是許多數學和科學計算中非常重要的資料結構。 正如在前一章中已經討論過的二維陣列結構,我們將在本章中專注於矩陣特有的資料結構操作。
也使用numpy包進行矩陣資料操作。
考慮在早上,中午,晚上和深夜測量記錄1週溫度的情況。 它可以使用陣列在numpy
中可用的重塑方法以7 x 5
矩陣的形式呈現。
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']]
要更新矩陣行中的值,只需在行的索引處重新分配值。 在下面的例子中,星期幾資料的所有值都標記為0
。 該行的索引是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']]